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

used-before-assignment false negative with TYPE_CHECKING #8198

Closed
DanielNoord opened this issue Feb 6, 2023 · 8 comments · Fixed by #8431 or #9093
Closed

used-before-assignment false negative with TYPE_CHECKING #8198

DanielNoord opened this issue Feb 6, 2023 · 8 comments · Fixed by #8431 or #9093
Assignees
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Negative 🦋 No message is emitted but something is wrong with the code
Milestone

Comments

@DanielNoord
Copy link
Collaborator

DanielNoord commented Feb 6, 2023

Bug description

Within functions used-before-assignment doesn't get raised for some edge cases.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from datetime import datetime

# pylint: disable=missing-function-docstring, missing-module-docstring


def func_two():
    second = datetime.now()
    return second


def func():
    first: datetime
    first = datetime.now()
    second = datetime.now()
    return first, second

Configuration

Default config.

Command used

pylint

Pylint output

************* Module test
test.py:10: [E0601(used-before-assignment), func_two] Using variable 'datetime' before assignment


-------------------------------------------------------------------
Your code has been rated at 4.55/10 (previous run: 10.00/10, -5.45)

Expected behavior

************* Module test
test.py:10: [E0601(used-before-assignment), func_two] Using variable 'datetime' before assignment
test.py:16: [E0601(used-before-assignment), func_two] Using variable 'datetime' before assignment


-------------------------------------------------------------------
Your code has been rated at 4.55/10 (previous run: 10.00/10, -5.45)

Pylint version

2.16.1

OS / Environment

/

Additional dependencies

No response

@DanielNoord DanielNoord added False Negative 🦋 No message is emitted but something is wrong with the code C: used-before-assignment Issues related to 'used-before-assignment' check labels Feb 6, 2023
@jacobtylerwalls
Copy link
Member

Is this still a problem as of #8071?

@MaximMukhametov
Copy link

Is this still a problem as of #8071?

yes, I have the same problem with:

from typing import TYPE_CHECKING
# It's needed just to avoid the PyCharm's static analyzer bug
# https://stackoverflow.com/questions/53276766/how-to-avoid-unexpected-argument-warning-using-data-classes
if TYPE_CHECKING:
    from dataclasses import dataclass  # noqa
else:
    from pydantic.dataclasses import dataclass  # noqa


@dataclass
class Item:
    """Item domain model."""

    user_id: int
    title: str
    id: int | None = None

@DanielNoord
Copy link
Collaborator Author

@jacobtylerwalls Yeah, on main this is still an issue.. I wonder if we could expand on that PR?

@zenlyj
Copy link
Contributor

zenlyj commented Feb 19, 2023

@DanielNoord if no one has started on this, could you assign it to me? Thanks! 🙂

@zenlyj
Copy link
Contributor

zenlyj commented Feb 19, 2023

@MaximMukhametov i am struggling to understand why used-before-assignment should be emitted in your example. Isn't dataclass defined by this line: from pydantic.dataclasses import dataclass? Please let me know if I am missing something here.

@jacobtylerwalls
Copy link
Member

@zenlyj pylint is taking into account that if TYPE_CHECKING: will be false at runtime. Add the line func() at the bottom of Daniël's example, and execute it. You will see:

    first = datetime.now()
            ^^^^^^^^
NameError: name 'datetime' is not defined

@zenlyj
Copy link
Contributor

zenlyj commented Feb 19, 2023

@jacobtylerwalls thanks for clarifying! Any thoughts on the other case posted by @MaximMukhametov? I am unsure if the dataclass decorator should raise used-before-assignment, since it is defined in the else block.

@jacobtylerwalls
Copy link
Member

Ah, sorry! You have a point; doesn't seem like we should emit warnings in that case (and we don't currently.)

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 Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
5 participants