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 bug caused by assigning value to attr #181

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading