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

avoid raising DCO010 for overloads #21

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 5 additions & 1 deletion flake8_docstrings_complete/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _skip_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
"""Check whether to skip a function.

A function is skipped if it is a test function in a test file, if it is a fixture in a test
or fixture file or if it is a property.
or fixture file, if it is a property or if it is an overload.

Args:
node: The function to check
Expand All @@ -321,6 +321,10 @@ def _skip_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
if self._file_type in {types_.FileType.TEST, types_.FileType.FIXTURE}:
return any(self._is_fixture_decorator(decorator) for decorator in node.decorator_list)

# Check for overload
if any(attrs.is_overload_decorator(decorator) for decorator in node.decorator_list):
return True

return False

def visit_any_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
Expand Down
21 changes: 21 additions & 0 deletions flake8_docstrings_complete/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ def is_property_decorator(node: ast.expr) -> bool:
return False # pragma: nocover


def is_overload_decorator(node: ast.expr) -> bool:
"""Determine whether an expression is an overload decorator.

Args:
node: The node to check.

Returns:
Whether the node is an overload decorator.
"""
if isinstance(node, ast.Name):
return node.id == "overload"

# Handle attr
if isinstance(node, ast.Attribute):
value = node.value
return node.attr == "overload" and isinstance(value, ast.Name) and value.id == "typing"

# There is no valid syntax that gets to here
return False # pragma: nocover


def _get_class_target_name(target: ast.expr) -> ast.Name | None:
"""Get the name of the target for an assignment on the class.

Expand Down
69 changes: 69 additions & 0 deletions tests/integration/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from flake8_docstrings_complete import (
DOCSTR_MISSING_CODE,
DOCSTR_MISSING_MSG,
FIXTURE_DECORATOR_PATTERN_ARG_NAME,
FIXTURE_DECORATOR_PATTERN_DEFAULT,
FIXTURE_FILENAME_PATTERN_ARG_NAME,
Expand Down Expand Up @@ -569,6 +570,32 @@ class Class1:
"",
id=f"{DUPLICATE_ATTR_CODE} disabled",
),
pytest.param(
"""
from typing import overload


@overload
def foo():
...
""",
"source.py",
"",
id="overload",
),
pytest.param(
"""
import typing


@typing.overload
def foo():
...
""",
"source.py",
"",
id="typing.overload",
),
],
)
def test_pass(code: str, filename: str, extra_args: str, tmp_path: Path):
Expand All @@ -594,6 +621,48 @@ def test_pass(code: str, filename: str, extra_args: str, tmp_path: Path):
assert not proc.returncode


def test_fail_overload_import_from_bar(tmp_path: Path):
"""
given: files with Python code that passes the linting
when: flake8 is run against the code
then: the process exits with zero code and empty stdout
"""
code_file = create_code_file(
"""
import bar


@bar.overload
def foo():
...
""",
"foo.py",
tmp_path,
)
create_code_file(
"""
def overload(func):
return func
""",
"bar.py",
tmp_path,
)
(config_file := tmp_path / ".flake8").touch()

with subprocess.Popen(
(
f"{sys.executable} -m flake8 {code_file} --ignore D205,D400,D103 "
f"--config {config_file}"
),
stdout=subprocess.PIPE,
shell=True,
) as proc:
stdout = proc.communicate()[0].decode(encoding="utf-8")

assert DOCSTR_MISSING_MSG in stdout
assert proc.returncode


def test_self():
"""
given: working linter
Expand Down
Loading