-
Notifications
You must be signed in to change notification settings - Fork 179
track if typing.TYPE_CHECKING
to warn about non runtime bindings
#622
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
base: main
Are you sure you want to change the base?
track if typing.TYPE_CHECKING
to warn about non runtime bindings
#622
Conversation
#530 predates this and has similar goals / problems -- perhaps collaborate with that PR? |
ac90967
to
a795822
Compare
@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. |
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. |
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. |
Merged it with master again now. |
a795822
to
117d5de
Compare
117d5de
to
3bbd41a
Compare
3bbd41a
to
53c3070
Compare
53c3070
to
58506b3
Compare
58506b3
to
6dcf6a7
Compare
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the tests cover all the cases you intend to demonstrate here
pyflakes/checker.py
Outdated
nodeDepth = 0 | ||
offset = None | ||
_in_annotation = AnnotationState.NONE | ||
_in_type_check_guard = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are actually all incorrect -- should be assigned in __init__
since they are not class vars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
isinstance(parent.op, ast.RShift)): | ||
self.report(messages.InvalidPrintSyntax, node) | ||
|
||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
LIST = TUPLE | ||
|
||
def IMPORT(self, node): | ||
runtime = not self._in_type_check_guard |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
Thanks for the review, I'll see when I have time to address it. |
6dcf6a7
to
8c4da4a
Compare
0480c35
to
0039d2b
Compare
When 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
faab0f8
Compare
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_CHECKING
blocks 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.