-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
argparse: Use Literal for action & nargs add_argument parameter types #7329
argparse: Use Literal for action & nargs add_argument parameter types #7329
Conversation
This comment has been minimized.
This comment has been minimized.
86741ac
to
1b736f8
Compare
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cac51e4
to
c2aba3a
Compare
This comment has been minimized.
This comment has been minimized.
c2aba3a
to
25ad2b2
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. The cwltool code found by mypy-primer can just add explicit annotations.
We could try |
42eaca5
to
e43176e
Compare
This comment has been minimized.
This comment has been minimized.
1 similar comment
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
These were introduced in python#7329 and they cause false positives in code that used to be accepted before. There was a false positive in https://github.com/pycqa/pylint (encountered in python/mypy#12321) and I also saw false positives in an internal codebase. I suggest not using literal types here, since they can be kind of awkward to annotate in callers that don't pass a literal string. Rrequiring callers to write annotations like `Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="]` is not very user friendly.
These were introduced in #7329 and they cause false positives in code that used to be accepted before.
As has previously been discussed in #6826 (comment) the
argparse
module sometimes comparesargparse.SUPPRESS
withis
meaningLiteral["==SUPPRESS=="]
is not suited. This PR solves this usingNewType
.As reported by mypy_primer the following fails with mypy:
because of:
This however seems to be due to a limitation in mypy not understanding that a newtype is backwards compatible with the original type as can be observed in the following example:
Mypy complains whereas Pyright says it's ok. With that in mind I think that the advantages of this PR (type safety against typos) outweigh the caused inconvenience (when reassigning SUPPRESS with mypy).