You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm wondering if the reasoning behind this choice has been discussed before. Return types of library functions generally avoid Union (python/mypy#1693). To me, the parameters of a user-provided callback are similar to the return type of a library function—the annotations reflect objects passed to user code, and the user may have runtime control of their types. It is, however, more removed from the runtime context because the class must be first defined and then referenced.
If I override __call__ with an implementation that will only ever see str:
pyright complains, based on what appears to be a correct interpretation of the stub:
repr.py:5:51 - error: Cannot access member "split" for type "Sequence[Any]"
Member "split" is unknown (reportGeneralTypeIssues)
repr.py:5:51 - error: "split" is not a known member of "None" (reportOptionalMemberAccess)
Would the appropriate solution be to add assert isinstance(values, str) at the beginning of every callback? Or is this actually abuse of action=..., which should be replaced with type=..., like this?
Since these examples are based on 10-year-old optparse code that was ported 1:1 to argparse 2 years ago, I'm not considering reimplementing them like that—they aren't broken, and the less trivial examples reference parser.error and the previous value of self.dest. But I'd rather avoid the asserts, so for new code I would probably prefer that pattern.
The text was updated successfully, but these errors were encountered:
cebtenzzre
changed the title
Implications of Union in argparse.Action.\_\_call\_\_ parameter types
Implications of Union in argparse.Action.__call__ parameter types
Mar 27, 2022
I believe the best solution is to introduce a _ActionValue (or similar) type alias to Any and use that as the values type. A comment could then be added about the possible types. PR welcome.
typeshed/stdlib/argparse.pyi
Lines 364 to 366 in c41034c
I'm wondering if the reasoning behind this choice has been discussed before. Return types of library functions generally avoid Union (python/mypy#1693). To me, the parameters of a user-provided callback are similar to the return type of a library function—the annotations reflect objects passed to user code, and the user may have runtime control of their types. It is, however, more removed from the runtime context because the class must be first defined and then referenced.
If I override __call__ with an implementation that will only ever see str:
pyright complains, based on what appears to be a correct interpretation of the stub:
Would the appropriate solution be to add
assert isinstance(values, str)
at the beginning of every callback? Or is this actually abuse ofaction=...
, which should be replaced withtype=...
, like this?Since these examples are based on 10-year-old optparse code that was ported 1:1 to argparse 2 years ago, I'm not considering reimplementing them like that—they aren't broken, and the less trivial examples reference
parser.error
and the previous value ofself.dest
. But I'd rather avoid the asserts, so for new code I would probably prefer that pattern.The text was updated successfully, but these errors were encountered: