From 8572248cce3081f1abf6706035f2b0b827cb5f65 Mon Sep 17 00:00:00 2001 From: jakkdl Date: Mon, 7 Nov 2022 13:29:57 +0100 Subject: [PATCH] B027 now ignores @[anything].overload to reduce false positives. --- README.rst | 4 ++++ bugbear.py | 5 +---- tests/b027.py | 13 ++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index d2accaf..15ced9a 100644 --- a/README.rst +++ b/README.rst @@ -297,6 +297,10 @@ MIT Change Log ---------- +Future +~~~~~~~~~ +* B027: ignore @overload when typing is import with other names + 22.10.27 ~~~~~~~~~ diff --git a/bugbear.py b/bugbear.py index 6e12c15..db4e0c0 100644 --- a/bugbear.py +++ b/bugbear.py @@ -671,10 +671,7 @@ def is_abstract_decorator(expr): 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" + isinstance(expr, ast.Attribute) and expr.attr == "overload" ) def empty_body(body) -> bool: diff --git a/tests/b027.py b/tests/b027.py index eb09d99..ef8e9a2 100644 --- a/tests/b027.py +++ b/tests/b027.py @@ -60,7 +60,10 @@ def empty_2(self): # safe # ignore @overload, fixes issue #304 +# ignore overload with other imports, fixes #308 import typing +import typing as t +import typing as anything from typing import Union, overload @@ -73,6 +76,14 @@ def empty_1(self, foo: str): def empty_1(self, foo: int): ... + @t.overload + def empty_1(self, foo: list): + ... + + @anything.overload + def empty_1(self, foo: float): + ... + @abstractmethod - def empty_1(self, foo: Union[str, int]): + def empty_1(self, foo: Union[str, int, list, float]): ...