Uh oh!
There was an error while loading. Please reload this page.
track if typing.TYPE_CHECKING to warn about non runtime bindings - #622
track if typing.TYPE_CHECKING to warn about non runtime bindings#622terencehonles wants to merge 1 commit into
if typing.TYPE_CHECKING to warn about non runtime bindings#622Conversation
Uh oh!
There was an error while loading. Please reload this page.
asottile
commented
Mar 23, 2021
#530 predates this and has similar goals / problems -- perhaps collaborate with that PR? |
ac90967 to
a795822Compare
@asottile It looks like I actually ended up writing about the same code 😅 however there are a few things I have which the other one doesn't:
It also looks like the code was waiting review for almost a year. Is there something you wanted changed from that PR? I'm willing to work with @PetterS, but I didn't realize there was an PR that old for this already opened. |
asottile
commented
Mar 23, 2021
ah shoot, that's probably on me -- I might've missed the last round of reviews on there and then it lapsed (and now conflicts) |
I can revive that PR if we agree that it it is likely to be merged. This is a problem that has been observed for real code, so would be good to fix. |
terencehonles
commented
Mar 24, 2021
It should be pretty easy to xref this PR in order to see what might also need to change since I based my PR off the most recent release (I was not aware of your PR when starting). We're 99% inline with each other's implementations. |
PetterS
commented
Mar 25, 2021
Merged it with master again now. |
a795822 to
117d5deCompare117d5de to
3bbd41aCompare3bbd41a to
53c3070Compare53c3070 to
58506b3Compare58506b3 to
6dcf6a7Compareterencehonles
commented
Sep 27, 2023
@asottile since the PR in comment above is not going to be updated by its author any chance you'd be willing to review this PR? As mentioned above #622 (comment) it had used negated logic compared to the other PR in order to minimize the number of changes to the code base, and I've continued to both use and update this PR for changes on the development branch. I can seek a 2nd approver if we can move forward with this PR. |
asottile
left a comment
There was a problem hiding this comment.
I don't think the tests cover all the cases you intend to demonstrate here
Uh oh!
There was an error while loading. Please reload this page.
| nodeDepth = 0 | ||
| offset = None | ||
| _in_annotation = AnnotationState.NONE | ||
| _in_type_check_guard = False |
There was a problem hiding this comment.
these are actually all incorrect -- should be assigned in __init__ since they are not class vars
There was a problem hiding this comment.
This is a pretty common pattern, and as you can see it already existed here. I've moved all of these into the __init__ to match your request.
Uh oh!
There was an error while loading. Please reload this page.
| @@ -1073,12 +1078,18 @@ def handleNodeLoad(self, node, parent): | |||
| self.report(messages.InvalidPrintSyntax, node) | |||
| try: | |||
There was a problem hiding this comment.
this try is now way too broad -- it originally only guarded the name lookup but now has a whole bunch of unrelated code in it
There was a problem hiding this comment.
I only added a little bit here, and the exception was already broader than it needed to be. I didn't realize this was a problem, and I moved all the unrelated code (including the existing code) out of the exception to satisfy this request.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| LIST = TUPLE | ||
| def IMPORT(self, node): | ||
| runtime = not self._in_type_check_guard |
There was a problem hiding this comment.
if we're going to invert this maybe it should just be _runtime ? but then that's not a great name so maybe runtime is not a great name?
terencehonles
commented
Oct 6, 2023
Thanks for the review, I'll see when I have time to address it. |
6dcf6a7 to
8c4da4aCompare0480c35 to
0039d2bCompareWhen importing or defining values in ``if typing.TYPE_CHECKING`` blocks the bound names will not be available at runtime and may cause errors when used in the following way:: import typing if typing.TYPE_CHECKING: from module import Type # some slow import or circular reference def method(value) -> Type: # the import is needed by the type checker assert isinstance(value, Type) # this is a runtime error This change allows pyflakes to track what names are bound for runtime use, and allows it to warn when a non runtime name is used in a runtime context.
0039d2b to
faab0f8Compareterencehonles
commented
Sep 23, 2025
It has taken me awhile to revisit this PR in order to address the comments (rather than just to update it), but I have addressed most of the comments. Responding to @jayvdb's comment
I was avoiding calling it
I agree this might make sense, but it would require a bigger re-work and I believed I was following how I hope these changes are satisfactory. |
When importing or defining values in
if typing.TYPE_CHECKINGblocks the bound names will not be available at runtime and may cause errors when used in the following way:This change allows pyflakes to track what names are bound for runtime use, and allows it to warn when a non runtime name is used in a runtime context.