hauntsaninja / no_implicit_optional

A codemod to make your implicit optional type hints PEP 484 compliant.
MIT License
79 stars 8 forks source link

Leave Any type annotation unchanged #1

Closed robertschweizer closed 1 year ago

hauntsaninja commented 1 year ago

Thanks for this PR! I opted to leave this unchanged because I think it rewriting to Optional[Any] preserves mypy's behaviour better. For instance, in the following example, if you don't rewrite, you get a false negative when you go from --implicit-optional to --no-implicit-optional:

λ cat test.py
from typing import Any, Optional

def old_implicit(x: Any = None):
    reveal_type(x)
    x + 1

λ mypy --implicit-optional test.py   
test.py:4: note: Revealed type is "Union[Any, None]"
test.py:5: error: Unsupported operand types for + ("None" and "int")
test.py:5: note: Left operand is of type "Optional[Any]"
Found 1 error in 1 file (checked 1 source file)

λ mypy --no-implicit-optional test.py
test.py:4: note: Revealed type is "Any"
Success: no issues found in 1 source file

Optional[Any] is a meaningful type and provides a lot more type safety than just Any.

Curious for your thoughts? If there is some use case I could make it a disabled-by-default flag?

robertschweizer commented 1 year ago

Thanks for the explanation! I didn't realize that mypy provides this extra safety when combining Any with other types using Union.

I agree with you that Any = None should also be upgraded to Optional[Any] = None in this case.