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
{{ message }}
Repository navigation
Reason for re.Match.group and re.Match.__getitem__ returning AnyStr | Any? #9465
Trying to piece together a precise history at this point is quite honestly a little difficult! I think it's likely that the stricter, more correct signature causes an unacceptable number of false positives. But you're welcome to submit a PR and see what the results of mypy_primer are in 2023 :-) (No promises that it will necessarily be merged.)
(Thanks for the PR, by the way! It's always good to check whether these decisions made years ago still apply. Type checkers are always getting better with these things, and experimenting is definitely encouraged in this repo :)
Thanks! I get annoyed with Any creeping its way into my code, so I try to stamp that out when I can. Problem is, I still don't have a good intuition of when I should use cast() vs. an explicit type annotation for a variable vs. some sort of assertion to narrow the inferred type, especially as it pertains when something has type Any or some union with Any.
Taking an example right from the re docs,
importrem=re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
assertmisnotNonefirst_name=m[1] # Has type `str | Any`# Do this?g1: str=m[1]
# Or this?g1=cast(str, m[1])
# Or this?g1=m[1]
assertisinstance(g1, str)
The first two make disallow-any-expr directives happy whereas the third one does not.
@AlexWaygood Do you have any personal philosophy of when to use what? If so, I'd love to hear it!
Activity
AlexWaygood commentedon Jan 6, 2023
There's been a lot of discussion of the signature of
Match.group()over the years:Trying to piece together a precise history at this point is quite honestly a little difficult! I think it's likely that the stricter, more correct signature causes an unacceptable number of false positives. But you're welcome to submit a PR and see what the results of
mypy_primerare in 2023 :-) (No promises that it will necessarily be merged.)AlexWaygood commentedon Jan 9, 2023
Well, yup, looks like
AnyStr | Anyis still the best we can do here for now! #9482 (comment)AlexWaygood commentedon Jan 9, 2023
(Thanks for the PR, by the way! It's always good to check whether these decisions made years ago still apply. Type checkers are always getting better with these things, and experimenting is definitely encouraged in this repo :)
willfrey commentedon Jan 9, 2023
Thanks! I get annoyed with
Anycreeping its way into my code, so I try to stamp that out when I can. Problem is, I still don't have a good intuition of when I should usecast()vs. an explicit type annotation for a variable vs. some sort of assertion to narrow the inferred type, especially as it pertains when something has typeAnyor some union withAny.Taking an example right from the
redocs,The first two make
disallow-any-exprdirectives happy whereas the third one does not.@AlexWaygood Do you have any personal philosophy of when to use what? If so, I'd love to hear it!
hauntsaninja commentedon Jan 9, 2023
The first is more type safe,
castwill let you liewillfrey commentedon Jan 9, 2023
@hauntsaninja Yeah, agreed about
cast. It's the BFG of shutting up the type checker.KotlinIsland commentedon Jan 18, 2024
Basedmypy works correctly in these cases: