-
-
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
Fix #5399: Fix false negatives for further variable messages for invalid type annotations or default arguments #5727
Fix #5399: Fix false negatives for further variable messages for invalid type annotations or default arguments #5727
Conversation
undefined-variable
when a type annotation is accessed multiple times
Pull Request Test Coverage Report for Build 1764446244
π - Coveralls |
β¦ a type annotation is accessed multiple times
1c1ed91
to
ba0a8ac
Compare
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.
LGTM. I like the simplification when simply removing the add_message makes the code simpler and removes an inconsistency.
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.
Thank you @jacobtylerwalls !
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.
Just some questions. Change itself is very nice!
Stuff like this make me happy about all the work we've been putting in typing and make the code more readable over the last couple of months. The change to _is_first_level_self_reference
shows how because of earlier simplification we can now actually improve the messages by building on that previous groundwork. π
augvar += 1 # [undefined-variable] | ||
del vardel # [undefined-variable] | ||
augvar += 1 # [undefined-variable, unused-variable] | ||
del vardel # [undefined-variable, unused-variable] |
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.
Isn't this a false positive? Why are we reporting unused-variable
on a del
operation with a undefined-variable
.
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.
Thanks, thinking more deeply about this I now see why the existing code did what it did. I might be able to handle both cases by using the consumed_uncertain
functionality to delay consuming this until it's raised all the undefined errors it needs to raise. I'll revisit over the next week or so and see if that works.
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 I understand correctly we're going to merge as it is and then open another MR to handle this comment ? I'm ok with this plan.
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 so, and if @jacobtylerwalls wanted to do so I would advise against.
Nothing personal, but we never know what might come up in any of our personal lives and I don't think it's good to add code we know raises a false-positive instead of correctly warning previously. I have 100% confidence @jacobtylerwalls will get to this, but I don't think we should merge the "faulty" code.
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.
Addressed this in 53c5200, happy to hear your thoughts. These are statements used and defined in the same line, and we have a variable already to track that, so the approach is to just check that condition and mark as consumed.
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.
π
augvar += 1 # [undefined-variable] | ||
del vardel # [undefined-variable] | ||
augvar += 1 # [undefined-variable, unused-variable] | ||
del vardel # [undefined-variable, unused-variable] |
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 I understand correctly we're going to merge as it is and then open another MR to handle this comment ? I'm ok with this plan.
#5727 (comment) need to be taken care off before merging.
return (VariableVisitConsumerAction.CONSUME, found_nodes) | ||
if defined_by_stmt: | ||
current_consumer.mark_as_consumed(node.name, [node]) | ||
return (VariableVisitConsumerAction.CONTINUE, None) |
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'm probably missing something here, but why can't we return Consume
with [node]
instead of found_nodes
?
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.
CONTINUE is the only action that will allow further messages to be emitted in the same scope (the original issue being fixed), but when leaving the scope, if it's a defined_by_stmt
line we want to make sure the name is consumed and does not emit unused-variable
.
Otherwise, we would need a fourth VariableVisitConsumerAction action along the lines of CONSUME_AND_CONTINUE.
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.
Hm, what do you thinks makes most sense? CONSUME_AND_CONTINUE
sounds the most future-proof to me? I feel like having a mark_as_consumed
somewhere in this method makes the consumption harder to comprehend again.
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.
An alternative might be to just reduce the actions to CONTINUE and RETURN and then permit the second element of the tuple to have elements to mark as consumed no matter the first value?
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.
Hmm that's not bad actually. Performance wise it would be similar, as if action == CONSUME
or if consume_nodes
should be quite similar.
Perhaps that should be done in another PR though?
Look at me thinking this PR showed how good and future-proof our previous code was π
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.
Follow up PR sounds right.
Just making sure I follow: did you want to avoid adding a fourth consumer action for now?
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.
Yes, if we know we're going to reduce down to two actions in the foreseeable future we might as well keep it like this.
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.
Approving, on the condition that we don't forget about that follow up PR π
Easy! Will rebase #5745 on this when merged. |
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.
Thank you @jacobtylerwalls !
Type of Changes
Description
There were two short-circuit-and-consume-names paths in the variables checker preventing reporting further errors:
Removing some of these "special early consume" messages has the benefit that going through the regular path raises the correct flavor of
used-before-assignment
versusundefined-variable
.Closes #5399
Will need a rebase after #5709. (edit: done!)