Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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/10664.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a false positive for ``invalid-name`` on exclusive module-level assignments
composed of three or more branches.

Closes #10664
22 changes: 14 additions & 8 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,22 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
if (
(iattrs := tuple(node.frame().igetattr(node.name)))
and util.Uninferable not in iattrs
and len(iattrs) == 2
and astroid.are_exclusive(*iattrs)
and len(iattrs) > 1
and all(
astroid.are_exclusive(*combo)
for combo in itertools.combinations(iattrs, 2)
)
):
node_type = "const"
self._check_name(
node_type,
node.name,
node,
disallowed_check_only=redefines_import,
)
if not self._meets_exception_for_non_consts(
inferred_assign_type, node.name
):
self._check_name(
node_type,
node.name,
node,
disallowed_check_only=redefines_import,
)

# Check names defined in function scopes
elif isinstance(frame, nodes.FunctionDef):
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/d/disallowed_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def baz(): # [disallowed-name]
class foo(): # [disallowed-name]
pass

foo = {}.keys() # [disallowed-name]
foo = {}.keys() # Should raise disallowed-name once _check_name() is refactored.
foo = 42 # [disallowed-name]
aaa = 42 # [invalid-name]
1 change: 0 additions & 1 deletion tests/functional/d/disallowed_name.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
disallowed-name:3:0:3:7:baz:"Disallowed name ""baz""":HIGH
disallowed-name:6:0:6:9:foo:"Disallowed name ""foo""":HIGH
disallowed-name:9:0:9:3::"Disallowed name ""foo""":HIGH
disallowed-name:10:0:10:3::"Disallowed name ""foo""":HIGH
invalid-name:11:0:11:3::"Constant name ""aaa"" doesn't conform to UPPER_CASE naming style":HIGH
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ def A(): # [invalid-name]
ASSIGNMENT_THAT_CRASHED_PYLINT = type(float.__new__.__code__)


# Exclusive assignment: uses const regex
if CONST:
OTHER_CONST = 1
else:
elif CONSTA:
OTHER_CONST = 2
else:
OTHER_CONST = 3


# Lists, sets, and objects can pass against the variable OR const regexes.
if CONST:
other_const = [1]
elif CONSTA:
other_const = [2]
else:
other_const = [3]
Loading