Repository navigation
Fix Match.group problems - #3172
Conversation
* group() may return None values * group() allows to mix int and str arguments * Use concrete return type * Merge overloads
|
Why do you say it is "clearly wrong" to return AnyStr? As far as I can tell, the only problem with that is that we don't handle the Could you also make the arguments positional-only? |
|
Well, "clearly wrong" -> "not type-safe in the general case". Will change them to positional only. |
|
|
||
| def expand(self, template: AnyStr) -> AnyStr: ... | ||
|
|
||
| def group(self, __group: Literal[0] = ...) -> AnyStr: ... |
There was a problem hiding this comment.
Missing the @overload decorator
|
This causes 19 errors in mypy self-check and 45 errors in the Python 3 part of our internal code base (which is actually relatively small). TBH, I am not sure the |
|
I wonder why our tests did not catch this, since the mypy self-test is run as part of the build. I'm open to changing it back, although just removing if m.group(12) is None:
...While I don't think such a tool exists yet, I'd prefer to use |
|
Actually I was wrong about number of errors internally, it is not 45 it is 110. So I think it makes sense reverting this in some form. As for the |
This partially reverts #3172
Fixes #3171
Returning
Optionalis potentially problematic. It is often obvious from a regexp whether a certain group exists (every time it is non-optional). Many users may rely ongroup()not returning anOptional. Nevertheless, returningAnyStris clearly wrong. One alternative is returningAny, but that would lose any type checking ability. Personally, I believe this patch is the best solution, despite the problems it may cause.