Closed jolaf closed 2 months ago
Here's the test demonstrating the issue.
test.py
:
from typeguard import install_import_hook
with install_import_hook(('A',)):
import A
A.py
:
try:
from typeguard import typeguard_ignore
except ImportError:
from typing import no_type_check as typeguard_ignore
@typeguard_ignore
def f() -> None:
a: int = "OK"
print(a)
f()
$ python3 A.py
OK
$ python3 test.py
OK
$ mypy A.py
A.py:4:5: error: Incompatible import of "typeguard_ignore" (imported name has type "Callable[[_F@no_type_check], _F@no_type_check]", local name has type "Callable[[_F@typeguard_ignore], _F@typeguard_ignore]") [assignment]
from typing import no_type_check as typeguard_ignore
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A.py: note: In function "f":
A.py:8:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
a: int = "OK"
^~~~
Found 2 errors in 1 file (checked 1 source file)
The second error is expected, but the first one is not. It goes away with this patch.
$ python --version
Python 3.12.3
$ pip list | grep typeguard
typeguard 4.3.0
$ mypy --version
mypy 1.11.2 (compiled: yes)
Thanks!
Trivial fix to make sure
typeguard_ignore()
has the same type signature in bothTYPE_CHECKING
and runtime branches.