Skip to content

Commit

Permalink
Merge pull request #306 from jakkdl/b027_ignore_overload
Browse files Browse the repository at this point in the history
B027 ignore @overload decorator
  • Loading branch information
Zac-HD committed Oct 26, 2022
2 parents 7a0b1e4 + 93cd15b commit 7b2af62
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
14 changes: 13 additions & 1 deletion bugbear.py
Expand Up @@ -669,6 +669,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 @@ -712,7 +720,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]):
...

0 comments on commit 7b2af62

Please sign in to comment.