Skip to content

Commit 93cd15b

Browse files
committed
B027 ignore @overload decorator
1 parent e16ad24 commit 93cd15b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

bugbear.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,14 @@ def is_abstract_decorator(expr):
666666
isinstance(expr, ast.Attribute) and expr.attr[:8] == "abstract"
667667
)
668668

669+
def is_overload(expr):
670+
return (isinstance(expr, ast.Name) and expr.id == "overload") or (
671+
isinstance(expr, ast.Attribute)
672+
and isinstance(expr.value, ast.Name)
673+
and expr.value.id == "typing"
674+
and expr.attr == "overload"
675+
)
676+
669677
def empty_body(body) -> bool:
670678
def is_str_or_ellipsis(node):
671679
# ast.Ellipsis and ast.Str used in python<3.8
@@ -709,7 +717,11 @@ def is_str_or_ellipsis(node):
709717

710718
has_abstract_method |= has_abstract_decorator
711719

712-
if not has_abstract_decorator and empty_body(stmt.body):
720+
if (
721+
not has_abstract_decorator
722+
and empty_body(stmt.body)
723+
and not any(map(is_overload, stmt.decorator_list))
724+
):
713725
self.errors.append(
714726
B027(stmt.lineno, stmt.col_offset, vars=(stmt.name,))
715727
)

tests/b027.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"""
77
Should emit:
8-
B025 - on lines 13, 16, 19, 23, 31
8+
B027 - on lines 13, 16, 19, 23, 31
99
"""
1010

1111

@@ -57,3 +57,22 @@ def empty_1(self): # safe
5757

5858
def empty_2(self): # safe
5959
pass
60+
61+
62+
# ignore @overload, fixes issue #304
63+
import typing
64+
from typing import Union, overload
65+
66+
67+
class AstractClass(ABC):
68+
@overload
69+
def empty_1(self, foo: str):
70+
...
71+
72+
@typing.overload
73+
def empty_1(self, foo: int):
74+
...
75+
76+
@abstractmethod
77+
def empty_1(self, foo: Union[str, int]):
78+
...

0 commit comments

Comments
 (0)