Move [...] import [...] into a type checking block. #460
-
|
When importing from __future__ import annotationsto be able to use | instead of Union[...] I get TCH003 warning raise by ruff/flake/etc. In other words: "Move [...] import [...] into a type checking block." But using TYPE_CHECKING seems to break runtime type checking. So should I just ignore this rule and consider is spurious when doing dynamic type checking? I think many people use TYPE_CHECKING to reduce import time while using static type checker. Moreover this warnings by ruff and flake maybe explain why so many library do so and why beartype have trouble ? (e.g. xarray, pydantic, etc.) Note: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
|
Okay I discovered some answers in pydata/xarray#9581 |
Beta Was this translation helpful? Give feedback.
-
|
Further confirmation that Ruff is fundamentally insane, huh? Literally everything the Ruff documentation claims at
Negligible. CPython trivially caches imported modules at
No. There is no such thing as "typing-only contexts." Due to type aliases (e.g., There are no "typing-only contexts." There is only the CPython runtime. Increasingly, CPython itself is beginning to perform runtime type-checking. Inappropriate use of Consider PEP 649 – Deferred Evaluation Of Annotations Using Descriptors, recently adopted by CPython 3.14. Under PEP 649, CPython ≥ 3.14 dynamically resolves forward references in type hints at runtime. Something that only runtime type-checkers like @beartype and This is the most destructive advice that anyone has ever given in the history of humanity.
CPython itself requires type hints at runtime. CPython itself is not a "class." Neither are hybrid runtime-static type-checkers, which can be trivially applied to an entire application stack at runtime (e.g., via the Go away, Ruff! You have no idea of what you pontificate.
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the answer! I would like to nuance a bit the usage of types at runtime. This can be seen as a waste in some programs that use type checking solely for annotation purpose (and static type checking). For this programs types are indeed "useless" at runtime in the sense that they do not modify the behavior of the app/library of the author. So they are just seen as wasted import time (which sadly can be quite high with Python). So I think beyond ruff's/flake recommendations, people will want to use TYPE_CHECKING for this reason as typing seems to be a waste of computation time (initial import time mainly) for only static usage. Note: You may have answered here:
However I am not sure I understand how it could break CPython as we are still supposed to be able to use untyped Python? Meanwhile I'm gonna spend some time on PEP 649 😃 |
Beta Was this translation helpful? Give feedback.
-
|
...lol. I neglected to actually explain "How to type-check circular imports with @beartype." I kinda just ranted and talked at you a lot instead, @RomainBrault. It must be that time of year. 😅 Happily, type-checking circular imports is a trivial one-liner with @beartype. You don't have to import anything; indeed, you usually shouldn't import anything. That's the whole point of circular imports, right? Here we go. Blink and you'll miss it: def muh_func(muh_arg: 'some_package.some_submodule.SomeClass'): ...And... that's it. Seriously. Avoid circular imports by annotating circular types with absolute forward references. Supah-easy. Note the lack of Have a great weekend! Let's get ready for Canadian Thanksgiving, everybody. 🦃 🪓 🍽️ |
Beta Was this translation helpful? Give feedback.
Further confirmation that Ruff is fundamentally insane, huh? Literally everything the Ruff documentation claims at
TCH003is unsubstantiated bald-faced lies that harm the CPython runtime. Notably, this:Negligible. CPython trivially caches imported modules at
sys.modules. Ruff, you are spouting nonsensical gibberish to support your spurious claims – and we ain't havin' none of it.No. There is no such thing as "typing-only contexts." Due to type aliases (e.g.,
MuhTypeAlias = li…