-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
if TYPE_CHECKING
considered harmful by runtime typecheckers
#9581
Comments
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
Do you have suggestions for how to avoid this? Happy to take a PR which removes some of these if it the proposed code still works. Generally this is done to avoid circular or optional imports. (I don't think "spurious" is an accurate description) (note #9561 as a case for more of this...) Edit:
OK, I don't think we have any objection to this, would be good to see a PR with a couple of examples. I looked at beartype/beartype#456, seems like Beartype's normative views are stronger than I expected. Is there any direction from the python typing community on using To get ahead of it: I'm less looking to get into an object-level discussion on the merits, just pointing out that my mental model for whether |
...heh. @leycec has been summoned. Thanks so much to @JWCS for getting this
|
OK — keeping this brief — do you see a reasonable way to convert the imports to work? We don't want |
I have the same question as @max-sixty. How would you actually replace if TYPE_CHECKING:
...
from xarray.core.dataarray import DataArray
def x(da: DataArray) ... Do you really have to do: if TYPE_CHECKING:
...
import xarray
def x(da: xarray.core.dataarray.DataArray) ... ? Edit: maybe this is actually ok to do?: if TYPE_CHECKING:
...
import xarray
def x(da: xarray.DataArray) ... That would not be too bad though. |
Surely that last suggestion wouldn't work either, for the same reason? When I have to say this seems like the sort of thing that should be resolved at the level of linter conventions and standards rather than by going around to individual codebases... |
One idea — but maybe beartype have already considered this — is for beartype to do some heuristics. So for example, (Yes agree not sure we're going to solve it all here) I think from xarray's perspective, we are happy to make changes which are:
Having Assuming we can meet those, we're happy to make the change and do our bit for the ecosystem. We're supporters-in-spirit of projects like beartype! Is that reasonable? |
...heh. @headtr1ck has the right of it on both accounts. That's an @airbus scientist for you, huh? What won't those guys come up with next!? Basically, you have two sane choices: tl;dr: This Is What I'm SayingThis suffices for @beartype. This should suffice for everyone else, too: def x(da: 'xarray.DataArray'): ... # <-- @beartype loves this If you hate quotes, ...no shame this is also totally fine: from __future__ import annotations
def x(da: xarray.DataArray): ... # <-- @beartype even loves this Note the distinct lack of @headtr1ck Is Right: Just Use the Short FormIf you hate reiterating In fact, I'm unclear why you even need to tell static type-checkers how to import attributes. What's the point of redundant boilerplate like this, anyway? if TYPE_CHECKING:
import xarray No idea, honestly. But What If You Hate Even the Short Form?If you hate even # In "xarray._forwardrefs":
DataArray = 'xarray.DataArray'
# In every other "xarray" submodule:
from xarray._forwardrefs import DataArray
def x(da: DataArray): ... # <-- @beartype actually even loves this Literally anything other than what you're currently doing is something that @beartype loves. 🥲 Heuristics: A Great Way to Blow Up PyTorch
...heh. Heuristics, huh? So, hacks. Actually, I usually adore hacks. @beartype is one giant hack resting on top of a pile of kludges cobbled together with vicious one-liners.
Sure. In this specific case, ad-hoc heuristics like that definitely work. In the general case, they... don't. Runtime type-checking is really all about definitive knowledge. "Intuitively guessing" where ambiguous types reside inevitably breaks down in common edge cases, invites non-portable fragility, and basically destroys the Python ecosystem. @beartype has been integrated into PyTorch. You know? I used to be cavalier about this sort of thing. Then I repeatedly broke PyTorch. Microsoft was understandably unhappy. Now, I exercise caution in all things. I grit my teeth. 😬 Explicit is better than implicit. Encouraging ambiguity just makes other people's code blow up. But Maybe It's Better to Just Give UpGiven that the If "your type hints are busted at runtime, so your package fundamentally violates CPython semantics including upcoming standards like PEP 649" isn't a compelling argument, it's probably best I bow out and just hack on open @beartype issues. I sigh. I sigh and collapse into bed. 😮💨 |
The reason we have the
FWIW I'm marking it plan-to-close because I'm not sure we're going to make progress on getting something that meets the three criteria above, and I also don't want to lose anyone's time, especially yours. If there is a way of making progress on those three criteria then let's make a plan and fix it. Do you think there is a way of meeting those criteria? Is that reasonable? |
By now I didn't have time to play around with that. The proposal of using forward refs as strings is not too much work to implement. Probably we can use our But I agree that without some automated way of checking this it is not very useful. |
Let's reopen when we find a way that meets the three criteria in #9581 (comment). @leycec we want to be good citizens in the python ecosystem, and we want to support projects like beartype — so please feel free to reopen / comment if there's some solution, thank you for commenting so far (& for building beartype!). |
Also note that the TCH rules of ruff might not do the right thing, at least not by default:
@max-sixty Would adding pytest-beartype to xarray CI tests qualify as a kind of "automated way of checking"? |
Yes that would! |
What happened?
This is a cross-post from the @beartype issue tracker, in which a downstream user tried to apply runtime type checking while using xarray... only to discover all the types, at runtime, were non-existent. The chief cause of this is spurious usage of
if TYPE_CHECKING
used throughout the xarray repo, which is undefined at runtime, like the types it provides.beartype/beartype#456
The below bug report is merely copied from that post.
The relevant issue (with respect to the MCVE below) seems to be the below example from the code base:
What did you expect to happen?
For the types to be visible at runtime, for runtime type checking.
From @leycec, the only real change needed should be:
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
MVCE Versions
beartype: 0.19.0
xarray: 2024.9.0
Python: 3.11.10
Environment
Omitted the environment, because not relevant.
The text was updated successfully, but these errors were encountered: