Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentLoader is not able to handle type #13

Open
pengzhengyi opened this issue Sep 23, 2021 · 0 comments
Open

ArgumentLoader is not able to handle type #13

pengzhengyi opened this issue Sep 23, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@pengzhengyi
Copy link
Owner

pengzhengyi commented Sep 23, 2021

Description

When retrieving type from field.type, type is a string literal indicating the type rather than the specific type class. Therefore, it cannot be added as type argument for add_argument in argparse as it expects a Callable.

Explanation

This issue arises because type evaluation is deferred when from __future__ import annotations is used.

For example,

  • when import is not used

    class A:
      foo: str
    
    A.__annotatiions__
    # {'foo': <class 'str'>}
  • when import is used

    from __future__ import annotations
    
    
    class A:
      foo: str
    
    A.__annotatiions__
    # {'foo': 'str'}

Therefore, if from __future__ import annotations is used at the module where the dataclass inheriting ArgumentLoader is used, it will cause field.type evaluates to string name of types like 'str', 'int' when deciding type during adding class field as an argument..

Workaround

A temporary workaround is avoiding using from __future__ import annotations when defining a class extending ArgumentLoader.

@pengzhengyi pengzhengyi added the bug Something isn't working label Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant