Skip to content

Python: Two fixes regarding annotated assignments#6336

Merged
RasmusWL merged 5 commits into
github:mainfrom
tausbn:python-make-annotated-assignment-a-definitionnode
Sep 14, 2021
Merged

Python: Two fixes regarding annotated assignments#6336
RasmusWL merged 5 commits into
github:mainfrom
tausbn:python-make-annotated-assignment-a-definitionnode

Conversation

@tausbn
Copy link
Copy Markdown
Contributor

@tausbn tausbn commented Jul 20, 2021

Fixes #2652 and a bunch of other FPs seen in the wild.

Should not require a change note.

Will probably need performance testing, although I expect the number of new DefinitionNodes to be rather low.

tausbn added 3 commits July 20, 2021 11:57
This was a source of false positives for the
`py/uninitialized-local-variable` query, as exemplified by the test
case.
This is only a temporary fix, as indicated by the TODO comment.

The real underlying issue is the fact that `isUnused` is defined in
terms of the underlying SSA variables (as these are only created
for variables that are actually used), and the fact that annotated
assignments are always considered to redefine their targets, which may
not actually be the case.

Thus, the correct fix would be to change the extractor to _disregard_
mere type annotations for the purposes of figuring out whether an
SSA variable should be created or not.

However, in the short term the present fix is likely sufficient.
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Jul 20, 2021
@tausbn tausbn requested a review from a team as a code owner July 20, 2021 12:21
RasmusWL
RasmusWL previously approved these changes Jul 20, 2021
Copy link
Copy Markdown
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Should we care to add a test-case such as

def func():
    foo = 5
    foo: int
    return "foo is unused"

which I think should be marked by py/unused-local-variable, but probably won't since it now has a type-hint. I don't think it's super important, just thinking out loud.

@tausbn
Copy link
Copy Markdown
Contributor Author

tausbn commented Jul 20, 2021

Sure, why not. 👍

Copy link
Copy Markdown
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks 💪 besides performance test, this looks good to me 👍

@tausbn tausbn requested a review from RasmusWL September 13, 2021 17:41
Copy link
Copy Markdown
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 (performance also looks fine)

@RasmusWL RasmusWL merged commit 49f5f1e into github:main Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LGTM.com - false positive - unused python locals with typehints incorrectly

2 participants