-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
regex group class should return AnyStr | None
instead of AnyStr | Any
#10526
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
This is the right repository for this issue :) Just to confirm -- your issue title mentions "regex", and there's a third-party library called |
Assuming you are discussing the |
Sorry, I didn't know about this ambiguity. I was referring to the stdlib |
I didn't find any issues like mine when I searched the tracker, but it seems i missed some. Can anybody explain to me what the lower issue here is? In the diff from the
error messages. In code that seems like someone has forgot to check if the regex group actually produced a matching capturing group or On the worst case scenario the |
Basedmypy works correctly with these cases: if m := re.match("a(b)?(c)", s):
reveal_type(m.groups()) # (str | None, str) |
Every time when using the regex library I'm stumbling over this when extracting the group text from a regex match. From what I read the function returns the match as an AnyStr which corresponds to the type variable of the
Match[AnyStr]
. But it also returns Any. I don't possibly know how to return anything else from this method.If a match was successful the group method will do one of the following:
(?P<groupname>.+)
None
if the capturing group was part of a regex branch that did not match(\d+)|(\w+)
IndexError("no such group")
if a group was referred that is not part of the pattern.This is the method and here is the comment that argues similar.
The same problem can be observed in the following places:
Note: I'm not a professional and I don't even know if this is the right repository for this issue, but I didn't find any better guidance on where to post this.
The advantages of the
AnyStr | None
annotation would be:text: str = match.group(1) or "default"
if match.group(1): print("Match group 1 has matched something")
The text was updated successfully, but these errors were encountered: