You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
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)?
The text was updated successfully, but these errors were encountered:
@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.
The
typeguard
package enables run-time type checks. One of the drawbacks of run-time type checks is that type annotations behindif 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
One workaround that I am investigating is to use a project-local
typing_
module from which I import my ownTYPE_CHECKING
, like this:typing_.py
workaround.py
Then,
python workaround.py
fails as expected: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 lettingenable_runtime_typing_checks
returnFalse
- anyway, I guess this is enough to make the following main point.)When I apply this concept more generally, though:
code.py
ruff check --isolated --select=TCH003 code.py
gives me
So, long story short: can I make
ruff
understand my owntyping_.TYPE_CHECKING
as a type-checking block, similar totyping.TYPE_CHECKING
andtyping_extensions.TYPE_CHECKING
(compare #8429)?The text was updated successfully, but these errors were encountered: