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

Fix used-before-assignment false positive for TYPE_CHECKING elif branch imports #8441

Merged
merged 38 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6b77468
Fix used-before-assignment TYPE_CHECKING elif false positive
zenlyj Mar 12, 2023
4d672ed
Add functional test for bug fix
zenlyj Mar 12, 2023
e103816
Add changelog
zenlyj Mar 12, 2023
66305e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 12, 2023
63b69cb
Ignore return value type warning
zenlyj Mar 12, 2023
82fdb4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 12, 2023
b3a4738
Change TYPE_CHECKING used-before-assignment evaluation flow
zenlyj Mar 22, 2023
34f658c
Update functional tests
zenlyj Mar 22, 2023
3acffd5
Remove redundant method
zenlyj Mar 22, 2023
c16f437
Merge branch 'main' of https://github.com/PyCQA/pylint into type-chec…
zenlyj Mar 22, 2023
8366cf8
Merge branch 'type-checking-elif-fp' of https://github.com/zenlyj/pyl…
zenlyj Mar 22, 2023
77fd218
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
ad84b85
Resolve CI errors
zenlyj Mar 22, 2023
776993b
Resolve scoping issue
zenlyj Mar 22, 2023
c5b5a8f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
7e30946
Work around TryFinally node unexpected behavior
zenlyj Mar 22, 2023
e82c9ac
Merge branch 'type-checking-elif-fp' of https://github.com/zenlyj/pyl…
zenlyj Mar 22, 2023
6bf1e01
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
b70834c
Remove redundant check
zenlyj Mar 23, 2023
782fcee
Enable import alias name detection
zenlyj Mar 23, 2023
6a0a735
Add better control flow in name defines method
zenlyj Mar 23, 2023
f9afa35
Merge branch 'main' of https://github.com/PyCQA/pylint into type-chec…
zenlyj Mar 23, 2023
af0632a
Merge branch 'type-checking-elif-fp' of https://github.com/zenlyj/pyl…
zenlyj Mar 23, 2023
77ab3a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 23, 2023
f96024a
Resolve CI check fails
zenlyj Mar 24, 2023
e8bd3e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2023
df51459
Relax check criteria
zenlyj Mar 24, 2023
b4bde2a
Merge branch 'main' of https://github.com/PyCQA/pylint into type-chec…
zenlyj Mar 24, 2023
c2c2491
Merge branch 'type-checking-elif-fp' of https://github.com/zenlyj/pyl…
zenlyj Mar 24, 2023
8220d3d
Revert tests restructuring
zenlyj Mar 25, 2023
ccb7a67
Remove unnecessary suppression
zenlyj Mar 25, 2023
e09fabd
Clean up and add comments
zenlyj Mar 25, 2023
36da654
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2023
873572b
Apply suggested changes from reviews
zenlyj Mar 29, 2023
258981a
Merge branch 'main' of https://github.com/PyCQA/pylint into type-chec…
zenlyj Mar 29, 2023
a0a5893
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 29, 2023
5969d5e
Merge branch 'main' of https://github.com/PyCQA/pylint into type-chec…
zenlyj Mar 30, 2023
05c7f78
Update tests
zenlyj Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8437.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a ``used-before-assignment`` false positive when imports
are made under the ``TYPE_CHECKING`` else if branch.

Closes #8437
47 changes: 0 additions & 47 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2120,53 +2120,6 @@ def is_class_attr(name: str, klass: nodes.ClassDef) -> bool:
return False


def is_defined(name: str, node: nodes.NodeNG) -> bool:
"""Searches for a tree node that defines the given variable name."""
is_defined_so_far = False

if isinstance(node, nodes.NamedExpr):
is_defined_so_far = node.target.name == name

if isinstance(node, (nodes.Import, nodes.ImportFrom)):
is_defined_so_far = any(node_name[0] == name for node_name in node.names)

if isinstance(node, nodes.With):
is_defined_so_far = any(
isinstance(item[1], nodes.AssignName) and item[1].name == name
for item in node.items
)

if isinstance(node, (nodes.ClassDef, nodes.FunctionDef)):
is_defined_so_far = node.name == name

if isinstance(node, nodes.AnnAssign):
is_defined_so_far = (
node.value
and isinstance(node.target, nodes.AssignName)
and node.target.name == name
)

if isinstance(node, nodes.Assign):
is_defined_so_far = any(
any(
(
(
isinstance(elt, nodes.Starred)
and isinstance(elt.value, nodes.AssignName)
and elt.value.name == name
)
or (isinstance(elt, nodes.AssignName) and elt.name == name)
)
for elt in get_all_elements(target)
)
for target in node.targets
)

return is_defined_so_far or any(
is_defined(name, child) for child in node.get_children()
)


def get_inverse_comparator(op: str) -> str:
"""Returns the inverse comparator given a comparator.

Expand Down
Loading