Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 21, 2024

Conversation

jolaf
Copy link
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

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
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
10 checks passed
@agronholm
Copy link
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