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
# 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?
Your Environment
mypy 1.6.1 (compiled: yes)
Python 3.10.12
The text was updated successfully, but these errors were encountered:
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.
Why doesn't
m.group(2)
raise an [arg-type] error here?Your Environment
mypy 1.6.1 (compiled: yes)
Python 3.10.12
The text was updated successfully, but these errors were encountered: