diff --git a/bugbear.py b/bugbear.py index c512fd3..ad241fd 100644 --- a/bugbear.py +++ b/bugbear.py @@ -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 @@ -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,)) ) diff --git a/tests/b027.py b/tests/b027.py index 0e814a5..eb09d99 100644 --- a/tests/b027.py +++ b/tests/b027.py @@ -5,7 +5,7 @@ """ Should emit: -B025 - on lines 13, 16, 19, 23, 31 +B027 - on lines 13, 16, 19, 23, 31 """ @@ -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]): + ...