From a5e24e6971dfffff3481b09d12f6703d90792435 Mon Sep 17 00:00:00 2001 From: jsh9 <25124332+jsh9@users.noreply.github.com> Date: Sun, 23 Jun 2024 11:56:06 -0700 Subject: [PATCH] Fix annotation unparsing bug (#132) --- CHANGELOG.md | 5 +++++ pydoclint/utils/arg.py | 2 +- tests/data/google/class_attributes/cases.py | 8 ++++---- tests/data/numpy/class_attributes/cases.py | 8 ++++---- tests/data/sphinx/class_attributes/cases.py | 8 ++++---- tests/test_main.py | 4 ++-- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60c45ff..2ae35cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [unpublished] + +- Fixed + - Fixed a bug in unparsing annotations when checking class attributes + ## [0.5.0] - 2024-06-22 - Added diff --git a/pydoclint/utils/arg.py b/pydoclint/utils/arg.py index 061f466..79e6efa 100644 --- a/pydoclint/utils/arg.py +++ b/pydoclint/utils/arg.py @@ -112,7 +112,7 @@ def fromAstAnnAssign(cls, astAnnAssign: ast.AnnAssign) -> 'Arg': """Construct an Arg object from a Python ast.AnnAssign object""" return Arg( name=astAnnAssign.target.id, - typeHint=astAnnAssign.annotation.id, + typeHint=unparseAnnotation(astAnnAssign.annotation), ) @classmethod diff --git a/tests/data/google/class_attributes/cases.py b/tests/data/google/class_attributes/cases.py index 84377a2..c359e65 100644 --- a/tests/data/google/class_attributes/cases.py +++ b/tests/data/google/class_attributes/cases.py @@ -3,15 +3,15 @@ class MyClass1: A class that holds some things. Attributes: - name (str): The name - indices (int): The indices + name (str | bool | None): The name + indices (pd.DataFrame): The indices Args: arg1 (float): The information """ - name: str - index: int + name: str | bool | None + index: pd.DataFrame hello: int = 1 world: dict diff --git a/tests/data/numpy/class_attributes/cases.py b/tests/data/numpy/class_attributes/cases.py index 759e248..aa0d3f2 100644 --- a/tests/data/numpy/class_attributes/cases.py +++ b/tests/data/numpy/class_attributes/cases.py @@ -4,9 +4,9 @@ class MyClass1: Attributes ---------- - name : str + name : str | bool | None The name - indices : int + indices : pd.DataFrame The indices Parameters @@ -15,8 +15,8 @@ class MyClass1: The information """ - name: str - index: int + name: str | bool | None + index: pd.DataFrame hello: int = 1 world: dict diff --git a/tests/data/sphinx/class_attributes/cases.py b/tests/data/sphinx/class_attributes/cases.py index 6157dbc..5af1972 100644 --- a/tests/data/sphinx/class_attributes/cases.py +++ b/tests/data/sphinx/class_attributes/cases.py @@ -3,15 +3,15 @@ class MyClass1: A class that holds some things. :attr name: The name - :type name: str + :type name: str | bool | None :attr indices: The indices - :type indices: int + :type indices: pd.DataFrame :param arg1: The information :type arg1: float """ - name: str - index: int + name: str | bool | None + index: pd.DataFrame hello: int = 1 world: dict diff --git a/tests/test_main.py b/tests/test_main.py index 89cda8f..38d7c0d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -169,8 +169,8 @@ def testClassAttributes( 'actual class attributes. (Or could be other formatting issues: ' 'https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). ' 'Attributes in the class definition but not in the docstring: [hello: int, ' - 'index: int, world: dict]. Arguments in the docstring but not in the actual ' - 'class attributes: [indices: int].', + 'index: pd.DataFrame, world: dict]. Arguments in the docstring but not in the ' + 'actual class attributes: [indices: pd.DataFrame].', 'DOC105: Method `MyClass1.__init__`: Argument names match, but type hints in ' 'these args do not match: arg1', 'DOC105: Method `MyClass1.do_something`: Argument names match, but type hints '