argparse: Use Literal for action & nargs add_argument parameter types#7329
Merged
JelleZijlstra merged 2 commits intopython:masterfrom Feb 21, 2022
Merged
argparse: Use Literal for action & nargs add_argument parameter types#7329JelleZijlstra merged 2 commits intopython:masterfrom
JelleZijlstra merged 2 commits intopython:masterfrom
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.
JelleZijlstra
approved these changes
Feb 20, 2022
Member
JelleZijlstra
left a comment
There was a problem hiding this comment.
Looks good. The cwltool code found by mypy-primer can just add explicit annotations.
Collaborator
|
We could try |
42eaca5 to
e43176e
Compare
This comment has been minimized.
This comment has been minimized.
1 similar comment
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
JukkaL
added a commit
to JukkaL/typeshed
that referenced
this pull request
Apr 12, 2022
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.
srittau
pushed a commit
that referenced
this pull request
Apr 13, 2022
These were introduced in #7329 and they cause false positives in code that used to be accepted before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As has previously been discussed in #6826 (comment) the
argparsemodule sometimes comparesargparse.SUPPRESSwithismeaningLiteral["==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).