swansonk14 / typed-argument-parser

Typed argument parser for Python
MIT License
497 stars 40 forks source link

exclude variables annotated with ClassVar #75

Open adrianmgg opened 2 years ago

adrianmgg commented 2 years ago

ClassVar is (quoting from here in typing docs, emphasis mine):

Special type construct to mark class variables.

[...] a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class.

I think it would make sense to exclude any variables whose annotations are wrapped in ClassVar from the list of arguments.

RadixSeven commented 2 years ago

It would be nice to have an annotation specifically to make Tap ignore variables, e.g., @tap_ignore. Since ClassVar would be less common, asking users to use both might be sufficient. My use case is that I have a complex type that I create from multiple arguments in process_args (right now, I just say not to use it in the comment).

class ArgParser(Tap):
    params: List[float] = []
    """ Even number of entries. Each pair is the mean and 
    standard deviation of the Gaussian underlying a 
    log-normal distribution """
    @tap_ignore
    distributions: List[LogNormal] = []
    """ process_args will construct this from params after 
    checking params for validity """
martinjm97 commented 2 years ago

Hi @adrianmgg and @RadixSeven,

We like both of these ideas! Thank you so much for bringing up this issue. We'll try to get to this soon, but any PRs are welcome and appreciated.

Thanks, Kyle and Jesse