Skip to content

Commit

Permalink
Better handling of backticks in type hints (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored Aug 21, 2023
1 parent 014c244 commit 3ba5d65
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 4 deletions.
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

0 comments on commit 3ba5d65

Please sign in to comment.