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
re.Match.group() output doesn't raise type error #16441
# example.pyimportredeffoo(s: str) ->str:
print(s)
returnsm: re.Match[str] |None=re.match('(a)(b)?', 'a')
ifmisnotNone:
foo(None) # Argument 1 to "foo" has incompatible type "None"; expected "str" [arg-type] # as expectedreveal_type(m.group(2)) # Revealed type is "Union[builtins.str, Any]"foo(m.group(2)) # does not raise an [arg-type] errorassertm.group(2) isNone# passes
$ mypy --strict example.py
example.py:12: error: Argument 1 to "foo" has incompatible type"None"; expected "str" [arg-type]
example.py:14: note: Revealed type is "Union[builtins.str, Any]"
Found 1 error in 1 file (checked 1 source file)
Why doesn't m.group(2) raise an [arg-type] error here?
There's a balance between false negatives (like this) and false positives. Typeshed aims to avoid false positives, which is why the return type uses a union containing Any. For many — maybe even most — regular expressions, group can't actually be None, so this is a good decision.
Activity
hauntsaninja commentedon Nov 12, 2023
See python/typeshed#9465 (comment) / python/typeshed#9482 (comment)
There's a balance between false negatives (like this) and false positives. Typeshed aims to avoid false positives, which is why the return type uses a union containing Any. For many — maybe even most — regular expressions, group can't actually be None, so this is a good decision.
KotlinIsland commentedon Jan 18, 2024
Basedmypy works correctly in these cases: