Skip to content

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

Merged
agronholm merged 3 commits intoagronholm:masterfrom
jolaf:typeguard_ignore_annotation
Sep 21, 2024
Merged

Fixed annotation for typeguard_ignore() to match one for typing.no_type_check()#485
agronholm merged 3 commits intoagronholm:masterfrom
jolaf:typeguard_ignore_annotation

Conversation

@jolaf
Copy link
Copy Markdown
Contributor

@jolaf jolaf commented Sep 8, 2024

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

@coveralls
Copy link
Copy Markdown

coveralls commented Sep 8, 2024

Coverage Status

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

@jolaf
Copy link
Copy Markdown
Contributor Author

jolaf commented Sep 8, 2024

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)

@jolaf jolaf changed the title Fixed annotation for typeguard_ignore() to match one for typing.no_type_check() Fixed annotation for typeguard_ignore() to match one for typing.no_type_check() Sep 8, 2024
@agronholm agronholm merged commit cf25d56 into agronholm:master Sep 21, 2024
@agronholm
Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants