Skip to content

Commit

Permalink
Fix bug caused by assigning value to attr (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored Dec 7, 2024
1 parent d9da0fa commit 1a96b26
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Changed
- Command line message about loading config file is now hidden with config
option `--quiet`
- Fixed
- Fixed a bug where assigning a value to an attribute caused pydoclint to
crash

## [0.5.9] - 2024-09-29

Expand Down
6 changes: 5 additions & 1 deletion pydoclint/utils/arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def fromAstArg(cls, astArg: ast.arg) -> 'Arg':
def fromAstAnnAssign(cls, astAnnAssign: ast.AnnAssign) -> 'Arg':
"""Construct an Arg object from a Python ast.AnnAssign object"""
return Arg(
name=astAnnAssign.target.id,
name=unparseAnnotation(astAnnAssign.target),
typeHint=unparseAnnotation(astAnnAssign.annotation),
)

Expand Down Expand Up @@ -220,6 +220,10 @@ def fromAstAssign(cls, astAssign: ast.Assign) -> 'ArgList':
infoList.append(Arg(name=item.id, typeHint=''))
elif isinstance(target, ast.Name): # such as `a = 1` or `a = b = 2`
infoList.append(Arg(name=target.id, typeHint=''))
elif isinstance(target, ast.Attribute): # e.g., uvw.xyz = 1
infoList.append(
Arg(name=unparseAnnotation(target), typeHint='')
)
else:
raise InternalError(
f'astAssign.targets[{i}] is of type {type(target)}'
Expand Down
12 changes: 12 additions & 0 deletions tests/data/edge_cases/16_assign_to_attr/cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# From https://github.com/jsh9/pydoclint/issues/180


class MyClass:
def large_drawing(self, obj):
return self.drawing(obj, size=500, center=False)

large_drawing.descr_1: str = 'Drawing'

# Non-self attribute should not be type hinted, because this could lead to
# potential ambiguities. See more: https://stackoverflow.com/a/77831273
large_drawing.descr_2: str = 'Drawing'
3 changes: 3 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,9 @@ def testNonAscii() -> None:
('15_very_long_annotations/sphinx.py', {'style': 'sphinx'}, []),
('15_very_long_annotations/google.py', {'style': 'google'}, []),
('15_very_long_annotations/numpy.py', {'style': 'numpy'}, []),
('16_assign_to_attr/cases.py', {'style': 'sphinx'}, []),
('16_assign_to_attr/cases.py', {'style': 'google'}, []),
('16_assign_to_attr/cases.py', {'style': 'numpy'}, []),
],
)
def testEdgeCases(
Expand Down

0 comments on commit 1a96b26

Please sign in to comment.