Using an explicit return type unnecessarily widens the types in filter():
defreturn_a(strings: list[str]) ->Optional[str]:
# Item "None" of "Optional[str]" has no attribute "strip"returnnext(filter(lambdas: s.strip() =="a", strings), None)
and a comparable error when the filter function is explicitly typed:
defmatches_a(s: str) ->bool:
returns.strip() =="a"defreturn_a(strings: list[str]) ->Optional[str]:
# Argument 1 to "filter" has incompatible type "Callable[[str], Any]"; expected "Callable[[Optional[str]], Any]"returnnext(filter(matches_a, strings), None)
No error occurs when the explicit return type is removed:
# return_a is inferred to return str | Nonedefreturn_a(strings: list[str]):
# No errorreturnnext(filter(lambdas: s.strip() =="a", strings), None)
Mypy 0.931, Python 3.8.6.
Using an explicit return type unnecessarily widens the types in
filter():and a comparable error when the filter function is explicitly typed:
No error occurs when the explicit return type is removed:
Mypy 0.931, Python 3.8.6.