-
-
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
Reason for re.Match.group
and re.Match.__getitem__
returning AnyStr | Any
?
#9465
Comments
There's been a lot of discussion of the signature of
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 |
Well, yup, looks like |
(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 Taking an example right from the import re
m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
assert m is not None
first_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]
assert isinstance(g1, str) The first two make @AlexWaygood Do you have any personal philosophy of when to use what? If so, I'd love to hear it! |
The first is more type safe, |
@hauntsaninja Yeah, agreed about |
Basedmypy works correctly in these cases:
|
Is there a reason why the return annotation is
AnyStr | Any
instead ofAnyStr | None
in the snippets below?typeshed/stdlib/re.pyi
Lines 72 to 77 in b1cb9c8
typeshed/stdlib/re.pyi
Lines 96 to 99 in b1cb9c8
If not, I'd like to submit a PR to change the return type hints accordingly.
Thank you!
The text was updated successfully, but these errors were encountered: