Skip to content

Commit 6535fe8

Browse files
committed
fix: Fix parsing tuple unpacking assignment
References: #43
1 parent 105fe08 commit 6535fe8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/pytkdocs/parsers/attributes.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def get_pairs(nodes):
5656
return
5757

5858
index = 0
59-
while index < len(nodes) - 1:
59+
while index < len(nodes):
6060
node1 = nodes[index]
61-
node2 = nodes[index + 1]
61+
if index < len(nodes) - 1:
62+
node2 = nodes[index + 1]
63+
else:
64+
node2 = None
6265
if isinstance(node1, (ast.Assign, ast.AnnAssign)):
6366
if isinstance(node2, ast.Expr) and isinstance(node2.value, ast.Str):
6467
yield node1, node2.value
@@ -82,7 +85,12 @@ def get_module_or_class_attributes(nodes):
8285
for assignment, string_node in get_pairs(nodes):
8386
string = dedent(string_node.s) if string_node else None
8487
if isinstance(assignment, ast.Assign):
85-
names = [target.id for target in assignment.targets]
88+
names = []
89+
for target in assignment.targets:
90+
if isinstance(target, ast.Name):
91+
names.append(target.id)
92+
elif isinstance(target, ast.Tuple):
93+
names.extend([name.id for name in target.elts])
8694
else:
8795
names = [assignment.target.id]
8896
for name in names:

tests/fixtures/parsing/attributes.py

+3
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,6 @@ class Model(BaseModel):
246246

247247
model_field: Optional[datetime] = None
248248
"""A model field."""
249+
250+
251+
OK, WARNING, CRITICAL, UNKNOWN = 0, 0, 0, 0

tests/test_parsers/test_attributes.py

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
from pytkdocs.parsers.attributes import get_class_attributes, get_instance_attributes, get_module_attributes
66

77

8+
class TestParsing:
9+
"""Test the parser in general."""
10+
11+
def setup(self):
12+
"""Setup reusable attributes."""
13+
self.attributes = get_module_attributes(attr_module)
14+
15+
def test_parse_tuple_target(self):
16+
"""Assert can parse `a, b, c = 0, 0, 0`."""
17+
assert "OK" in self.attributes
18+
assert "WARNING" in self.attributes
19+
assert "CRITICAL" in self.attributes
20+
assert "UNKNOWN" in self.attributes
21+
22+
823
class TestModuleAttributes:
924
"""Test the parser for module attributes."""
1025

0 commit comments

Comments
 (0)