Skip to content

Commit

Permalink
Fix a bug in parsing annotations like "Callable[[int], str]"
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed May 25, 2023
1 parent 30eedbd commit 64f6250
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- A new option to allow `__init__()` methods to have docstring (and when
users activate this option, check arguments and "Raises" in the docstring
of `__init__()` instead of in the class docstring)
- Fixed
- A bug when parsing type annotations such as `Callable[[int], str]`

## [0.0.3] - 2023-05-18

Expand Down
5 changes: 4 additions & 1 deletion pydoclint/utils/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydoclint.utils.astTypes import AnnotationType


def parseAnnotation(node: Optional[AnnotationType]) -> Optional[str]:
def parseAnnotation(node: Optional[AnnotationType]) -> Optional[str]: # noqa: C901
"""Parse type annotations from argument list or return annotation."""
if node is None:
return None
Expand All @@ -23,6 +23,9 @@ def parseAnnotation(node: Optional[AnnotationType]) -> Optional[str]:
if isinstance(node, ast.Tuple):
return ', '.join(map(parseAnnotation, node.elts))

if isinstance(node, ast.List):
return '[' + ', '.join(map(parseAnnotation, node.elts)) + ']'

if isinstance(node, ast.Constant):
if isinstance(node, ast.Ellipsis): # Ellipsis is Constant's subclass
return '...'
Expand Down
18 changes: 18 additions & 0 deletions tests/utils/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def foo(
arg12: Dict[str, Tuple[ast.arg, np.ndarray]],
arg13: Tuple[str, ...],
arg14: Tuple[None, int, float, None],
arg15: Callable[[T], U],
arg16: Callable[[int, str, bool], float],
arg17: Callable[..., ReturnType],
arg18: Callable[ParamSpecVariable, ReturnType],
arg19: Callable[Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable], ReturnType],
arg20: Type[User],
arg21: Type[BasicUser | ProUser],
arg22: Literal[True],
arg23: ClassVar[dict[str, int]],
):
pass
"""
Expand All @@ -48,6 +57,15 @@ def foo(
'arg12': 'Dict[str, Tuple[ast.arg, np.ndarray]]',
'arg13': 'Tuple[str, ...]',
'arg14': 'Tuple[None, int, float, None]',
'arg15': 'Callable[[T], U]',
'arg16': 'Callable[[int, str, bool], float]',
'arg17': 'Callable[..., ReturnType]',
'arg18': 'Callable[ParamSpecVariable, ReturnType]',
'arg19': 'Callable[Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable], ReturnType]',
'arg20': 'Type[User]',
'arg21': 'Type[BasicUser | ProUser]',
'arg22': 'Literal[True]',
'arg23': 'ClassVar[dict[str, int]]',
}

assert result == expected
Expand Down

0 comments on commit 64f6250

Please sign in to comment.