agronholm / typeguard

Run-time type checker for Python
Other
1.56k stars 114 forks source link

Fixed annotation for `typeguard_ignore()` to match one for `typing.no_type_check()` #485

Closed jolaf closed 2 months ago

jolaf commented 2 months ago

Trivial fix to make sure typeguard_ignore() has the same type signature in both TYPE_CHECKING and runtime branches.

coveralls commented 2 months ago

Coverage Status

coverage: 94.217%. remained the same when pulling 0b47f7783ffe20c1e231598ea6c2f5ca548a2a1e on jolaf:typeguard_ignore_annotation into 604b08d5ba7c1b6e3d2f2ddd50dcf020f7e2794a on agronholm:master.

jolaf commented 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)
agronholm commented 2 months ago

Thanks!