Skip to content

Commit

Permalink
Improve handling of long type annotations (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored Sep 29, 2024
1 parent 27ff769 commit 8170436
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [0.5.9] - 2024-09-29

- Fixed

- Fixed an edge case where type annotations are very long

- Full diff
- https://github.com/jsh9/pydoclint/compare/0.5.8...0.5.9

## [0.5.8] - 2024-09-23

- Fixed
Expand Down
6 changes: 5 additions & 1 deletion pydoclint/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,15 @@ def appendArgsToCheckToV105(
def specialEqual(str1: str, str2: str) -> bool:
"""
Check string equality but treat any single quotes as the same as
double quotes.
double quotes, and also ignore line breaks in either strings.
"""
if str1 == str2:
return True # using shortcuts to speed up evaluation

if '\n' in str1 or '\n' in str2:
str1 = str1.replace(' ', '').replace('\n', '')
str2 = str2.replace(' ', '').replace('\n', '')

if len(str1) != len(str2):
return False # using shortcuts to speed up evaluation

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pydoclint
version = 0.5.8
version = 0.5.9
description = A Python docstring linter that checks arguments, returns, yields, and raises sections
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
32 changes: 32 additions & 0 deletions tests/data/edge_cases/15_very_long_annotations/google.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This edge case was reported in https://github.com/jsh9/pydoclint/issues/164

# fmt: off
import numpy as np


def func(
arg1: tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
],
) -> tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray]:
"""
The docstring parser for the Google style does not support line breaking
in type hints. Therefore, in order to pass pydoclint's checks, we can only
put long type hints in one line.
Args:
arg1 (tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]): A parameter
Returns:
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]: The return value
"""
return (
np.array([]), np.array([]), np.array([]), np.array([]), np.array([]),
np.array([]), np.array([]), np.array([]), np.array([]),
)

# fmt: on
37 changes: 37 additions & 0 deletions tests/data/edge_cases/15_very_long_annotations/numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This edge case was reported in https://github.com/jsh9/pydoclint/issues/164

# fmt: off

import numpy as np


def func(
arg1: tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
],
) -> tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray]:
"""
The docstring parser for the numpy style does not support line breaking
in type hints. Therefore, in order to pass pydoclint's checks, we can only
put long type hints in one line.
Parameters
----------
arg1 : tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]
A parameter
Returns
-------
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]
The return value
"""
return (
np.array([]), np.array([]), np.array([]), np.array([]), np.array([]),
np.array([]), np.array([]), np.array([]), np.array([]),
)

# fmt: on
36 changes: 36 additions & 0 deletions tests/data/edge_cases/15_very_long_annotations/sphinx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This edge case was reported in https://github.com/jsh9/pydoclint/issues/164

# fmt: off

import numpy as np


def func(
arg1: tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
],
) -> tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray]:
"""Something
:param arg1: A parameter
:type arg1: tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
]
:returns: Numpy arrays
:rtype: tuple[
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray]
"""
return (
np.array([]), np.array([]), np.array([]), np.array([]), np.array([]),
np.array([]), np.array([]), np.array([]), np.array([]),
)

# fmt: on
3 changes: 3 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,9 @@ def testNonAscii() -> None:
'args do not match: arg1'
],
),
('15_very_long_annotations/sphinx.py', {'style': 'sphinx'}, []),
('15_very_long_annotations/google.py', {'style': 'google'}, []),
('15_very_long_annotations/numpy.py', {'style': 'numpy'}, []),
],
)
def testEdgeCases(
Expand Down

0 comments on commit 8170436

Please sign in to comment.