Skip to content

Commit 7b2af62

Browse files
authored
Merge pull request #306 from jakkdl/b027_ignore_overload
B027 ignore @overload decorator
2 parents 7a0b1e4 + 93cd15b commit 7b2af62

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
@@ -669,6 +669,14 @@ def is_abstract_decorator(expr):
669669
isinstance(expr, ast.Attribute) and expr.attr[:8] == "abstract"
670670
)
671671

672+
def is_overload(expr):
673+
return (isinstance(expr, ast.Name) and expr.id == "overload") or (
674+
isinstance(expr, ast.Attribute)
675+
and isinstance(expr.value, ast.Name)
676+
and expr.value.id == "typing"
677+
and expr.attr == "overload"
678+
)
679+
672680
def empty_body(body) -> bool:
673681
def is_str_or_ellipsis(node):
674682
# ast.Ellipsis and ast.Str used in python<3.8
@@ -712,7 +720,11 @@ def is_str_or_ellipsis(node):
712720

713721
has_abstract_method |= has_abstract_decorator
714722

715-
if not has_abstract_decorator and empty_body(stmt.body):
723+
if (
724+
not has_abstract_decorator
725+
and empty_body(stmt.body)
726+
and not any(map(is_overload, stmt.decorator_list))
727+
):
716728
self.errors.append(
717729
B027(stmt.lineno, stmt.col_offset, vars=(stmt.name,))
718730
)

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)