From 93cd15b19e3981831c821a80c7fab71110292aca Mon Sep 17 00:00:00 2001 From: jakkdl Date: Wed, 26 Oct 2022 11:59:22 +0200 Subject: [PATCH] B027 ignore @overload decorator --- bugbear.py | 14 +++++++++++++- tests/b027.py | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bugbear.py b/bugbear.py index 4b3f981..052e31b 100644 --- a/bugbear.py +++ b/bugbear.py @@ -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 @@ -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,)) ) 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]): + ...