Skip to content

Commit

Permalink
Fix outdate wording in type-hints-in-xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed Jul 20, 2023
1 parent 86c38c7 commit c93d829
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 141 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [0.1.2] - 2023-07-20

- Fixed
- Fixed outdated naming of options `--type-hints-in-docstring` and
`--type-hints-in-signature`
- Full diff
- https://github.com/jsh9/pydoclint/compare/0.1.1...0.1.2

## [0.1.1] - 2023-07-18

- Fixed
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ _pydoclint_ currently has the following style violation codes:
| `DOC103` | Docstring arguments are different from function arguments. (Or could be other formatting issues: https://github.com/jsh9/pydoclint#notes-on-doc103) |
| `DOC104` | Arguments are the same in the docstring and the function signature, but are in a different order. |
| `DOC105` | Argument names match, but type hints do not match |
| `DOC106` | The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature |
| `DOC107` | The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints |
| `DOC108` | The option `--arg-type-hints-in-signature` is `False` but there are argument type hints in the signature |
| `DOC109` | The option `--arg-type-hints-in-docstring` is `True` but there are no type hints in the docstring arg list |
| `DOC110` | The option `--arg-type-hints-in-docstring` is `True` but not all args in the docstring arg list have type hints |
| `DOC111` | The option `--arg-type-hints-in-docstring` is `False` but there are type hints in the docstring arg list |

#### Notes on `DOC103`:

Expand Down Expand Up @@ -247,26 +253,28 @@ or
flake8 --style=google <FILE_OR_FOLDER>
```
### 4.4. `--type-hints-in-docstring` and `--type-hints-in-signature`
### 4.4. `--arg-type-hints-in-docstring` and `--arg-type-hints-in-signature`
- `--type-hints-in-docstring`
- `--arg-type-hints-in-docstring`
- Shortform: `-thd`
- Default: `True`
- Meaning:
- If `True`, there need to be type hints in the argument list of a
docstring
- If `False`, there cannot be any type hints in the argument list of a
docstring
- `--type-hints-in-signature`
- `--arg-type-hints-in-signature`
- Shortform: `-ths`
- Default: `True`
- Meaning:
- If `True`, there need to be type hints in the function/method signature
- If `False`, there cannot be any type hints in the function/method
- If `True`, there need to be argument type hints in the function/method
signature
- If `False`, there cannot be any argument type hints in the
function/method signature
Note: if users choose `True` for both options, the type hints in the signature
and in the docstring need to match, otherwise there will be a style violation.
Note: if users choose `True` for both options, the argument type hints in the
signature and in the docstring need to match, otherwise there will be a style
violation.
### 4.5. `--check-arg-order` (shortform: `-ao`, default: `True`)
Expand Down
48 changes: 39 additions & 9 deletions pydoclint/flake8_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,30 @@ def add_options(cls, parser): # noqa: D102
'-ths',
'--type-hints-in-signature',
action='store',
default='None',
parse_from_config=True,
help='(Deprecated) Please use --arg-type-hints-in-signature instead',
)
parser.add_option(
'-aths',
'--arg-type-hints-in-signature',
action='store',
default='True',
parse_from_config=True,
help='Whether to require type hints in function signatures',
help='Whether to require argument type hints in function signatures',
)
parser.add_option(
'-thd',
'--type-hints-in-docstring',
action='store',
default='None',
parse_from_config=True,
help='(Deprecated) Please use --arg-type-hints-in-docstring instead',
)
parser.add_option(
'-athd',
'--arg-type-hints-in-docstring',
action='store',
default='True',
parse_from_config=True,
help='Whether to require type hints in the argument list in docstrings',
Expand Down Expand Up @@ -102,6 +118,8 @@ def add_options(cls, parser): # noqa: D102
def parse_options(cls, options): # noqa: D102
cls.type_hints_in_signature = options.type_hints_in_signature
cls.type_hints_in_docstring = options.type_hints_in_docstring
cls.arg_type_hints_in_signature = options.arg_type_hints_in_signature
cls.arg_type_hints_in_docstring = options.arg_type_hints_in_docstring
cls.check_arg_order = options.check_arg_order
cls.skip_checking_short_docstrings = (
options.skip_checking_short_docstrings
Expand All @@ -115,13 +133,25 @@ def parse_options(cls, options): # noqa: D102

def run(self) -> Generator[Tuple[int, int, str, Any], None, None]:
"""Run the linter and yield the violation information"""
typeHintsInSignature = self._bool(
'--type-hints-in-signature',
self.type_hints_in_signature,
if self.type_hints_in_docstring != 'None': # user supplies this option
raise ValueError(
'The option `--type-hints-in-docstring` has been renamed;'
' please use `--arg-type-hints-in-docstring` instead'
)

if self.type_hints_in_signature != 'None': # user supplies this option
raise ValueError(
'The option `--type-hints-in-signature` has been renamed;'
' please use `--arg-type-hints-in-signature` instead'
)

argTypeHintsInSignature = self._bool(
'--arg-type-hints-in-signature',
self.arg_type_hints_in_signature,
)
typeHintsInDocstring = self._bool(
'--type-hints-in-docstring',
self.type_hints_in_docstring,
argTypeHintsInDocstring = self._bool(
'--arg-type-hints-in-docstring',
self.arg_type_hints_in_docstring,
)
checkArgOrder = self._bool('--check-arg-order', self.check_arg_order)
skipCheckingShortDocstrings = self._bool(
Expand All @@ -147,8 +177,8 @@ def run(self) -> Generator[Tuple[int, int, str, Any], None, None]:
)

v = Visitor(
typeHintsInSignature=typeHintsInSignature,
typeHintsInDocstring=typeHintsInDocstring,
argTypeHintsInSignature=argTypeHintsInSignature,
argTypeHintsInDocstring=argTypeHintsInDocstring,
checkArgOrder=checkArgOrder,
skipCheckingShortDocstrings=skipCheckingShortDocstrings,
skipCheckingRaises=skipCheckingRaises,
Expand Down
70 changes: 57 additions & 13 deletions pydoclint/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,28 @@ def validateStyleValue(
@click.option(
'-ths',
'--type-hints-in-signature',
show_default=True,
default='None',
help='(Deprecated) Please use --arg-type-hints-in-signature instead',
)
@click.option(
'-aths',
'--arg-type-hints-in-signature',
type=bool,
show_default=True,
default=True,
help='Whether to require type hints in function signatures',
help='Whether to require argument type hints in function signatures',
)
@click.option(
'-thd',
'--type-hints-in-docstring',
show_default=True,
default='None',
help='(Deprecated) Please use --arg-type-hints-in-docstring instead',
)
@click.option(
'-athd',
'--arg-type-hints-in-docstring',
type=bool,
show_default=True,
default=True,
Expand Down Expand Up @@ -179,8 +193,10 @@ def main( # noqa: C901
style: str,
src: Optional[str],
paths: Tuple[str, ...],
type_hints_in_signature: bool,
type_hints_in_docstring: bool,
type_hints_in_signature: str,
type_hints_in_docstring: str,
arg_type_hints_in_signature: bool,
arg_type_hints_in_docstring: bool,
check_arg_order: bool,
skip_checking_short_docstrings: bool,
skip_checking_raises: bool,
Expand All @@ -192,6 +208,34 @@ def main( # noqa: C901
"""Command-line entry point of pydoclint"""
ctx.ensure_object(dict)

if type_hints_in_docstring != 'None': # it means users supply this option
click.echo(
click.style(
''.join([
'The option `--type-hints-in-docstring` has been renamed;',
' please use `--arg-type-hints-in-docstring` instead',
]),
fg='red',
bold=True,
),
err=echoAsError,
)
ctx.exit(1)

if type_hints_in_signature != 'None': # it means users supply this option
click.echo(
click.style(
''.join([
'The option `--type-hints-in-signature` has been renamed;',
' please use `--arg-type-hints-in-signature` instead',
]),
fg='red',
bold=True,
),
err=echoAsError,
)
ctx.exit(1)

if paths and src is not None:
click.echo(
main.get_usage(ctx)
Expand All @@ -212,8 +256,8 @@ def main( # noqa: C901
exclude=exclude,
style=style,
paths=paths,
typeHintsInSignature=type_hints_in_signature,
typeHintsInDocstring=type_hints_in_docstring,
argTypeHintsInSignature=arg_type_hints_in_signature,
argTypeHintsInDocstring=arg_type_hints_in_docstring,
checkArgOrder=check_arg_order,
skipCheckingShortDocstrings=skip_checking_short_docstrings,
skipCheckingRaises=skip_checking_raises,
Expand Down Expand Up @@ -268,8 +312,8 @@ def main( # noqa: C901
def _checkPaths(
paths: Tuple[str, ...],
style: str = 'numpy',
typeHintsInSignature: bool = True,
typeHintsInDocstring: bool = True,
argTypeHintsInSignature: bool = True,
argTypeHintsInDocstring: bool = True,
checkArgOrder: bool = True,
skipCheckingShortDocstrings: bool = True,
skipCheckingRaises: bool = False,
Expand Down Expand Up @@ -310,8 +354,8 @@ def _checkPaths(
violationsInThisFile: List[Violation] = _checkFile(
filename,
style=style,
typeHintsInSignature=typeHintsInSignature,
typeHintsInDocstring=typeHintsInDocstring,
argTypeHintsInSignature=argTypeHintsInSignature,
argTypeHintsInDocstring=argTypeHintsInDocstring,
checkArgOrder=checkArgOrder,
skipCheckingShortDocstrings=skipCheckingShortDocstrings,
skipCheckingRaises=skipCheckingRaises,
Expand All @@ -327,8 +371,8 @@ def _checkPaths(
def _checkFile(
filename: Path,
style: str = 'numpy',
typeHintsInSignature: bool = True,
typeHintsInDocstring: bool = True,
argTypeHintsInSignature: bool = True,
argTypeHintsInDocstring: bool = True,
checkArgOrder: bool = True,
skipCheckingShortDocstrings: bool = True,
skipCheckingRaises: bool = False,
Expand All @@ -342,8 +386,8 @@ def _checkFile(
tree: ast.Module = ast.parse(src)
visitor = Visitor(
style=style,
typeHintsInSignature=typeHintsInSignature,
typeHintsInDocstring=typeHintsInDocstring,
argTypeHintsInSignature=argTypeHintsInSignature,
argTypeHintsInDocstring=argTypeHintsInDocstring,
checkArgOrder=checkArgOrder,
skipCheckingShortDocstrings=skipCheckingShortDocstrings,
skipCheckingRaises=skipCheckingRaises,
Expand Down
12 changes: 6 additions & 6 deletions pydoclint/utils/violation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
),
104: 'Arguments are the same in the docstring and the function signature, but are in a different order.',
105: 'Argument names match, but type hints do not match',
106: 'The option `--type-hints-in-signature` is `True` but there are no type hints in the signature',
107: 'The option `--type-hints-in-signature` is `True` but not all args in the signature have type hints',
108: 'The option `--type-hints-in-signature` is `False` but there are type hints in the signature',
109: 'The option `--type-hints-in-docstring` is `True` but there are no type hints in the docstring arg list',
110: 'The option `--type-hints-in-docstring` is `True` but not all args in the docstring arg list have type hints',
111: 'The option `--type-hints-in-docstring` is `False` but there are type hints in the docstring arg list',
106: 'The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature',
107: 'The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints',
108: 'The option `--arg-type-hints-in-signature` is `False` but there are argument type hints in the signature',
109: 'The option `--arg-type-hints-in-docstring` is `True` but there are no type hints in the docstring arg list',
110: 'The option `--arg-type-hints-in-docstring` is `True` but not all args in the docstring arg list have type hints',
111: 'The option `--arg-type-hints-in-docstring` is `False` but there are type hints in the docstring arg list',

201: 'does not have a return section in docstring',
202: 'has a return section in docstring, but there are no return statements or annotations',
Expand Down
28 changes: 17 additions & 11 deletions pydoclint/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Visitor(ast.NodeVisitor):
def __init__(
self,
style: str = 'numpy',
typeHintsInSignature: bool = True,
typeHintsInDocstring: bool = True,
argTypeHintsInSignature: bool = True,
argTypeHintsInDocstring: bool = True,
checkArgOrder: bool = True,
skipCheckingShortDocstrings: bool = True,
skipCheckingRaises: bool = False,
Expand All @@ -46,8 +46,8 @@ def __init__(
requireReturnSectionWhenReturningNone: bool = False,
) -> None:
self.style: str = style
self.typeHintsInSignature: bool = typeHintsInSignature
self.typeHintsInDocstring: bool = typeHintsInDocstring
self.argTypeHintsInSignature: bool = argTypeHintsInSignature
self.argTypeHintsInDocstring: bool = argTypeHintsInDocstring
self.checkArgOrder: bool = checkArgOrder
self.skipCheckingShortDocstrings: bool = skipCheckingShortDocstrings
self.skipCheckingRaises: bool = skipCheckingRaises
Expand Down Expand Up @@ -346,22 +346,25 @@ def checkArguments( # noqa: C901
if docArgs.length > funcArgs.length:
violations.append(v102)

if self.typeHintsInSignature and funcArgs.noTypeHints():
if self.argTypeHintsInSignature and funcArgs.noTypeHints():
violations.append(v106)

if self.typeHintsInSignature and not funcArgs.hasTypeHintInAllArgs():
if (
self.argTypeHintsInSignature
and not funcArgs.hasTypeHintInAllArgs()
):
violations.append(v107)

if not self.typeHintsInSignature and funcArgs.hasTypeHintInAnyArg():
if not self.argTypeHintsInSignature and funcArgs.hasTypeHintInAnyArg():
violations.append(v108)

if self.typeHintsInDocstring and docArgs.noTypeHints():
if self.argTypeHintsInDocstring and docArgs.noTypeHints():
violations.append(v109)

if self.typeHintsInDocstring and not docArgs.hasTypeHintInAllArgs():
if self.argTypeHintsInDocstring and not docArgs.hasTypeHintInAllArgs():
violations.append(v110)

if not self.typeHintsInDocstring and docArgs.hasTypeHintInAnyArg():
if not self.argTypeHintsInDocstring and docArgs.hasTypeHintInAnyArg():
violations.append(v111)

if not docArgs.equals(
Expand All @@ -380,7 +383,10 @@ def checkArguments( # noqa: C901
checkTypeHint=False,
orderMatters=self.checkArgOrder,
):
if self.typeHintsInSignature and self.typeHintsInDocstring:
if (
self.argTypeHintsInSignature
and self.argTypeHintsInDocstring
):
violations.append(v105)
elif docArgs.equals(
funcArgs,
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.1.1
version = 0.1.2
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
Loading

0 comments on commit c93d829

Please sign in to comment.