Skip to content
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

False positive used-before-assignment for variable annotations when using TYPE_CHECKING #7609

Closed
mew1033 opened this issue Oct 11, 2022 · 5 comments · Fixed by #7810
Closed
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Milestone

Comments

@mew1033
Copy link

mew1033 commented Oct 11, 2022

Bug description

I know this has been looked at before, but I just ran into it again with pylint 2.15.4. If an import is guarded behind TYPE_CHECKING, I'm getting an used-before-assignment error when using that import in a type definition.

Configuration

No response

Command used

pylint file.py

Pylint output

file.py:7:19: E0601: Using variable 'PublishBatchRequestEntryTypeDef' before assignment (used-before-assignment)

Expected behavior

It shouldn't error

Pylint version

pylint 2.15.4
astroid 2.12.11
Python 3.9.14 (main, Sep  7 2022, 23:43:29) 
[GCC 9.4.0]

OS / Environment

No response

Additional dependencies

No response

@mew1033 mew1033 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 11, 2022
@mbyrnepr2 mbyrnepr2 added Cannot reproduce 🤷 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 12, 2022
@mbyrnepr2
Copy link
Member

Thanks for reporting this @mew1033. Can you please provide a minimal reproducible example to help us dig into this one?

@mbyrnepr2 mbyrnepr2 added the Waiting on author Indicate that maintainers are waiting for a message of the author label Oct 12, 2022
@mew1033
Copy link
Author

mew1033 commented Oct 13, 2022

@mbyrnepr2 So sorry, I got the example ready and then completely forgot to add it to the issue. Here's a snippet that shows the issue. If you remove the if TYPE_CHECKING: line, the issues goes away.

from typing import TYPE_CHECKING
import boto3

if TYPE_CHECKING:
    from mypy_boto3_sns.type_defs import PublishBatchRequestEntryTypeDef


def publish_to_sns():
    """
    Create SNS messages and publish them
    """
    sns_client = boto3.client("sns")
    messages: list[PublishBatchRequestEntryTypeDef] = []

@mbyrnepr2
Copy link
Member

No worries - thank you!

@mbyrnepr2 mbyrnepr2 added Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Cannot reproduce 🤷 Waiting on author Indicate that maintainers are waiting for a message of the author labels Oct 13, 2022
@jacobtylerwalls
Copy link
Member

Thanks! Per PEP 526, there is a difference in the runtime effect of variable annotations versus function annotations. Given that, the requirement in 3159b17 that both branches of an if TYPE_CHECKING: case define the name is too strict for variable annotations.

@jacobtylerwalls jacobtylerwalls added False Positive 🦟 A message is emitted but nothing is wrong with the code C: used-before-assignment Issues related to 'used-before-assignment' check and removed Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning labels Oct 14, 2022
@jacobtylerwalls jacobtylerwalls changed the title False positive used-before-assignment when using TYPE_CHECKING False positive used-before-assignment for variable annotations when using TYPE_CHECKING Oct 14, 2022
@jacobtylerwalls jacobtylerwalls added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Oct 14, 2022
@jacobtylerwalls
Copy link
Member

Some helpful test cases given in #7773

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants