Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Preventing the D103 error when the function is decorated with @overload. #511

Merged
merged 17 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
18 changes: 17 additions & 1 deletion src/pydocstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ def check_docstring_missing(self, definition, docstring):
else violations.D102()
)
),
Function: violations.D103,
NestedFunction: violations.D103,
theyuvalraz marked this conversation as resolved.
Show resolved Hide resolved
Function: (
theyuvalraz marked this conversation as resolved.
Show resolved Hide resolved
lambda: violations.D103()
if not definition.is_overload
else None
),
Package: violations.D104,
}
return codes[type(definition)]()
Expand Down Expand Up @@ -544,6 +548,18 @@ def check_capitalized(self, function, docstring):
if first_word != first_word.capitalize():
return violations.D403(first_word.capitalize(), first_word)

@check_for(Function)
def check_if_needed(self, function, docstring):
"""D418: Function decorated with @overload shouldn't contain a docstring.

Functions that are decorated with @overload are definitions,
and are for the benefit of the type checker only,
since they will be overwritten by the non-@overload-decorated definition.

"""
if docstring and function.is_overload:
return violations.D418()

@check_for(Definition)
def check_starts_with_this(self, function, docstring):
"""D404: First word of the docstring should not be `This`.
Expand Down
8 changes: 8 additions & 0 deletions src/pydocstyle/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ def is_public(self):
else:
return not self.name.startswith('_')

@property
def is_overload(self):
"""Return True iff the method decorated with overload."""
for decorator in self.decorators:
if decorator.name == "overload":
return True
return False

@property
def is_test(self):
"""Return True if this function is a test function/method.
Expand Down
6 changes: 6 additions & 0 deletions src/pydocstyle/violations.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ def to_rst(cls) -> str:
'argument(s) {0} are missing descriptions in {1!r} docstring',
)

D418 = D4xx.create_error(
'D418',
'Function decorated with @overload shouldn\'t contain a docstring',
)


class AttrDict(dict):
def __getattr__(self, item: str) -> Any:
Expand Down Expand Up @@ -441,6 +446,7 @@ def __getattr__(self, item: str) -> Any:
'D415',
'D416',
'D417',
'D418',
},
'numpy': all_errors
- {
Expand Down
18 changes: 18 additions & 0 deletions src/tests/test_cases/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from .expected import Expectation
from typing import overload


expectation = Expectation()
Expand Down Expand Up @@ -53,6 +54,23 @@ def nested():
''


@overload
def overloaded_func(a: int) -> str:
...


@overload
def overloaded_func(a: str) -> str:
theyuvalraz marked this conversation as resolved.
Show resolved Hide resolved
# TODO(Find a way to test D418
theyuvalraz marked this conversation as resolved.
Show resolved Hide resolved
# as @overload can't be passed to the @expect function)
...


def overloaded_func(a):
"""Foo bar documentation."""
return str(a)


@expect('D200: One-line docstring should fit on one line with quotes '
'(found 3)')
@expect('D212: Multi-line docstring summary should start at the first line')
Expand Down