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

Better handling of backticks in type hints #72

Merged
merged 1 commit into from
Aug 21, 2023
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ repos:
- id: isort

- repo: https://github.com/jsh9/cercis
rev: 0.1.7
rev: 0.2.0
hooks:
- id: cercis

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
rev: v3.0.2
hooks:
- id: prettier

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change Log

## [0.2.1] - 2023-08-21

- Improved

- Improved handling of backticks or double backticks being used in type hints
in docstrings

- Full diff
- https://github.com/jsh9/pydoclint/compare/0.2.0...0.2.1

## [0.2.0] - 2023-08-18

- Added
Expand Down
13 changes: 12 additions & 1 deletion pydoclint/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,21 @@ def stringStartsWith(string: str, substrings: Tuple[str, ...]) -> bool:


def stripQuotes(string: Optional[str]) -> Optional[str]:
"""Strip quote (both double and single quote) from the given string"""
"""
Strip quotes (both double and single quotes) from the given string.
Also, strip backticks (`) or double backticks (``) from the beginning
and the end of the given string. (Some people use backticks around
type hints so that they show up more nicely on the HTML documentation
page.)
"""
if string is None:
return None

if string.startswith('``') and string.endswith('``') and len(string) > 4:
string = string[2:-2]
elif string.startswith('`') and string.endswith('`') and len(string) > 3:
string = string[1:-1]

return re.sub(r'Literal\[[^\]]+\]|[^L]+', _replacer, string)


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.2.0
version = 0.2.1
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
12 changes: 12 additions & 0 deletions tests/data/edge_cases/04_backticks/google.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def someFunc(arg1: int, arg2: bool) -> str:
"""
Do something.

Args:
arg1 (`int`): Arg 1
arg2 (``bool``): Arg 2

Returns:
`str`: Return value
"""
pass
17 changes: 17 additions & 0 deletions tests/data/edge_cases/04_backticks/numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def someFunc(arg1: int, arg2: bool) -> str:
"""
Do something.

Parameters
----------
arg1 : `int`
Arg 1
arg2 : ``bool``
Arg 2

Returns
-------
`str`
Return value
"""
pass
12 changes: 12 additions & 0 deletions tests/data/edge_cases/04_backticks/sphinx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def someFunc(arg1: int, arg2: bool) -> str:
"""
Do something.

:param arg1: Arg 1
:type arg1: `int`
:param arg2: Arg 2
:type arg2: ``bool``
:return: Return value
:rtype: `str`
"""
pass
6 changes: 6 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
("''''''''''''''''", ''),
('""" """ """', ' '),
('List["Something", \'Else\']', 'List[Something, Else]'),
('`something`', 'something'),
('``something``', 'something'),
('`List["Something", \'Else\']`', 'List[Something, Else]'),
('``List["Something", \'Else\']``', 'List[Something, Else]'),
('`""" """ """`', ' '),
('``""" """ """``', ' '),
],
)
def testStripQuotes(inputStr: str, expected: str) -> None:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,21 @@ def testNonAscii() -> None:
"docstring return section types: ['str | bool | float']"
],
),
(
'04_backticks/google.py',
{'style': 'google'},
[],
),
(
'04_backticks/numpy.py',
{'style': 'numpy'},
[],
),
(
'04_backticks/numpy.py',
{'style': 'numpy'},
[],
),
],
)
def testEdgeCases(
Expand Down