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

Feature request: make typing_modules configurable #11142

Closed
bersbersbers opened this issue Apr 25, 2024 · 2 comments · Fixed by #11144
Closed

Feature request: make typing_modules configurable #11142

bersbersbers opened this issue Apr 25, 2024 · 2 comments · Fixed by #11144
Labels
configuration Related to settings and configuration question Asking for support or clarification

Comments

@bersbersbers
Copy link
Contributor

The typeguard package enables run-time type checks. One of the drawbacks of run-time type checks is that type annotations behind if typing.TYPE_CHECKING are not visible (https://typeguard.readthedocs.io/en/stable/userguide.html#notes-on-forward-reference-handling; compare also agronholm/typeguard#456).

So this passes in python bug.py while it shouldn't (ideally):

bug.py

from typing import TYPE_CHECKING

from typeguard import typechecked

if TYPE_CHECKING:
    A = int

@typechecked
def bug() -> None:
    a: A = 1.5
    print(a)

bug()

One workaround that I am investigating is to use a project-local typing_ module from which I import my own TYPE_CHECKING, like this:

typing_.py

from typing import TYPE_CHECKING as _TYPE_CHECKING

def enable_runtime_typing_checks() -> bool:
    return True  # more complicated, obviously

TYPE_CHECKING = _TYPE_CHECKING or enable_runtime_typing_checks()

workaround.py

from typeguard import typechecked

from typing_ import TYPE_CHECKING

if TYPE_CHECKING:
    A = int

@typechecked
def bug() -> None:
    a: A = 1.5
    print(a)

bug()

Then, python workaround.py fails as expected:

Traceback (most recent call last):
  File "C:\Code\project\workaround.py", line 15, in <module>
    bug()
  File "C:\Code\project\workaround.py", line 11, in bug
    a: A = 1.5
  File "c:\Code\project\.venv\Lib\site-packages\typeguard\_functions.py", line 251, in check_variable_assignment
    check_type_internal(value, annotation, memo)
  File "c:\Code\project\.venv\Lib\site-packages\typeguard\_checkers.py", line 784, in check_type_internal
    raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: value assigned to a (float) is not an instance of int

but A is not used at runtime. Nice!

(Obviously, my real A is more complicated than that, and my example above is too simplistic as it fails on letting enable_runtime_typing_checks return False - anyway, I guess this is enough to make the following main point.)

When I apply this concept more generally, though:

code.py

from typing_ import TYPE_CHECKING

if TYPE_CHECKING:
    from pathlib import Path

    from typing_ import A

def bug() -> None:
    a: A = 1.5
    print(a)
    p: Path

bug()

ruff check --isolated --select=TCH003 code.py

gives me

code.py:4:25: TCH003 Move standard library import `pathlib.Path` into a type-checking block

So, long story short: can I make ruff understand my own typing_.TYPE_CHECKING as a type-checking block, similar to typing.TYPE_CHECKING and typing_extensions.TYPE_CHECKING (compare #8429)?

@AlexWaygood
Copy link
Member

Hi, thanks for the issue! We already have a typing-modules configuration option: https://docs.astral.sh/ruff/settings/#lint_typing-modules. Does that do what you're asking for here?

@AlexWaygood AlexWaygood added question Asking for support or clarification configuration Related to settings and configuration labels Apr 25, 2024
@bersbersbers
Copy link
Contributor Author

@AlexWaygood absolutely, thank you! I feel somewhat embarrassed since I remember asking a similar question before, but could not find it in my emails or otherwise (I now know it's #5400). I also searched the repository for typing_modules and did not find much, and I checked ruff check . --show-settings and could not find it there (although I clearly see it now).

I think the main reason why I went looking is because I checked https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/ first, and the rule did not mention the configuration option. Maybe it should be added there (any in other TCH rules) to make it more discoverable.

charliermarsh pushed a commit that referenced this issue Apr 25, 2024
…11144)

## Summary

Mention `lint.typing-modules` in `TCH001`, `TCH002`, `TCH003`; close
#11142.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration question Asking for support or clarification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants