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

B027 ignore @overload decorator #306

Merged
merged 1 commit into from Oct 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion bugbear.py
Expand Up @@ -666,6 +666,14 @@ def is_abstract_decorator(expr):
isinstance(expr, ast.Attribute) and expr.attr[:8] == "abstract"
)

def is_overload(expr):
return (isinstance(expr, ast.Name) and expr.id == "overload") or (
isinstance(expr, ast.Attribute)
and isinstance(expr.value, ast.Name)
and expr.value.id == "typing"
and expr.attr == "overload"
)

def empty_body(body) -> bool:
def is_str_or_ellipsis(node):
# ast.Ellipsis and ast.Str used in python<3.8
Expand Down Expand Up @@ -709,7 +717,11 @@ def is_str_or_ellipsis(node):

has_abstract_method |= has_abstract_decorator

if not has_abstract_decorator and empty_body(stmt.body):
if (
not has_abstract_decorator
and empty_body(stmt.body)
and not any(map(is_overload, stmt.decorator_list))
):
self.errors.append(
B027(stmt.lineno, stmt.col_offset, vars=(stmt.name,))
)
Expand Down
21 changes: 20 additions & 1 deletion tests/b027.py
Expand Up @@ -5,7 +5,7 @@

"""
Should emit:
B025 - on lines 13, 16, 19, 23, 31
B027 - on lines 13, 16, 19, 23, 31
"""


Expand Down Expand Up @@ -57,3 +57,22 @@ def empty_1(self): # safe

def empty_2(self): # safe
pass


# ignore @overload, fixes issue #304
import typing
from typing import Union, overload


class AstractClass(ABC):
@overload
def empty_1(self, foo: str):
...

@typing.overload
def empty_1(self, foo: int):
...

@abstractmethod
def empty_1(self, foo: Union[str, int]):
...