Skip to content

Commit

Permalink
Fix violations in return_anno.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed Dec 15, 2024
1 parent 74031d7 commit 44ad2ee
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pydoclint/utils/return_anno.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def decompose(self) -> List[str]:
When the annotation string has strange values
"""
if self._isTuple(): # noqa: R506
assert self.annotation is not None # to help mypy understand type

if not self.annotation.endswith(']'):
raise EdgeCaseError('Return annotation not ending with `]`')

Expand All @@ -49,23 +51,25 @@ def decompose(self) -> List[str]:

insideTuple: str = self.annotation[6:-1]
if insideTuple.endswith('...'): # like this: Tuple[int, ...]
return [self.annotation] # b/c we don't know the tuple's length
# because we don't know the tuple's length
return [self.annotation]

parsedBody0: ast.Expr = ast.parse(insideTuple).body[0]
parsedBody0: ast.Expr = ast.parse(insideTuple).body[0] # type:ignore[assignment]
if isinstance(parsedBody0.value, ast.Name): # like this: Tuple[int]
return [insideTuple]

if isinstance(parsedBody0.value, ast.Tuple): # like Tuple[int, str]
elts: List = parsedBody0.value.elts
return [unparseName(_) for _ in elts]
elts: List[ast.expr] = parsedBody0.value.elts
return [unparseName(_) for _ in elts] # type:ignore[misc]

raise EdgeCaseError('decompose(): This should not have happened')
else:
return self.putAnnotationInList()

def _isTuple(self) -> bool:
try:
annoHead = ast.parse(self.annotation).body[0].value.value.id
assert self.annotation is not None # to help mypy understand type
annoHead = ast.parse(self.annotation).body[0].value.value.id # type:ignore[attr-defined]
return annoHead in {'tuple', 'Tuple'}
except Exception:
return False
Expand Down

0 comments on commit 44ad2ee

Please sign in to comment.