-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
re.match(), re.search() and re.fullmatch() cannot be used with AnyStr #9591
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
Comments
Interesting, thanks for opening the issue! I'll try making a PR. |
Hmm, there is a snag here: the overload @erictraut suggests would be unsafe, as I believe it would mean type checkers would no longer emit errors for code such as the following, which raises import re
pat = re.compile("foo")
string = bytearray(b"foo") # ReadableBuffer
pat.search(string) I believe this is the very reason why we use overloads here in the first place. Our general policy in typeshed is to prefer false negatives over false positives, but the lack of type safety here would be pretty unfortunate. Possibly we could compromise here by just using @overload
def search(self, string: AnyStr, pos: int = ..., endpos: int = ...) -> Match[AnyStr] | None: ... |
Yes, I think AnyStr | ReadableBuffer is wrong. We'd need a separate overload for ReadableBuffer that returns Match[bytes]. IIRC I didn't do it that way before because of a mypy bug, but maybe that's been fixed. |
Thanks @AlexWaygood. I agree that my suggestion would lead to potential false negatives. I like your suggestion. |
Indeed, it looks like the mypy bug is still very much with us: #9593 (comment). |
Given this test file:
pyright reports the following error:
The same code works fine if one replaces all
AnyStr
with eitherstr
orbytes
.I originally reported this on pyright issue tracker (microsoft/pyright#4534) but it seems the issue is actually here (see microsoft/pyright#4534 (comment))
Quoting erictraut comment:
The text was updated successfully, but these errors were encountered: