diff options
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | pylint/checkers/typecheck.py | 29 | ||||
-rw-r--r-- | tests/functional/a/alternative_union_syntax.py | 5 | ||||
-rw-r--r-- | tests/functional/a/alternative_union_syntax_error.py | 5 | ||||
-rw-r--r-- | tests/functional/a/alternative_union_syntax_error.txt | 40 | ||||
-rw-r--r-- | tests/functional/a/alternative_union_syntax_py37.py | 5 | ||||
-rw-r--r-- | tests/functional/a/alternative_union_syntax_py37.txt | 14 |
7 files changed, 68 insertions, 32 deletions
@@ -51,6 +51,8 @@ Pylint's ChangeLog * Fix issue with nested PEP 585 syntax +* Fix issue with nested PEP 604 syntax + What's New in Pylint 2.6.1? =========================== Release date: TBA diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index 38ebfde6f..5967c6efe 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -1662,17 +1662,32 @@ accessed. Python regular expressions are accepted.", astroid.Tuple, astroid.Set, astroid.List, + astroid.BinOp, ), ) ): - for n in (node.left, node.right): - n = helpers.object_type(n) - if isinstance(n, astroid.ClassDef): - if is_classdef_type(n): - self.add_message( - "unsupported-binary-operation", args=msg, node=node - ) + allowed_nested_syntax = False + if is_postponed_evaluation_enabled(node): + parent_node = node.parent + while True: + if isinstance( + parent_node, + (astroid.AnnAssign, astroid.Arguments, astroid.FunctionDef), + ): + allowed_nested_syntax = True + break + parent_node = parent_node.parent + if isinstance(parent_node, astroid.Module): break + if allowed_nested_syntax is False: + for n in (node.left, node.right): + n = helpers.object_type(n) + if isinstance(n, astroid.ClassDef): + if is_classdef_type(n): + self.add_message( + "unsupported-binary-operation", args=msg, node=node + ) + break @check_messages("unsupported-binary-operation") def _visit_binop(self, node): diff --git a/tests/functional/a/alternative_union_syntax.py b/tests/functional/a/alternative_union_syntax.py index 175e5a9ba..cb5995efa 100644 --- a/tests/functional/a/alternative_union_syntax.py +++ b/tests/functional/a/alternative_union_syntax.py @@ -9,6 +9,11 @@ from typing import NamedTuple, TypedDict Alias = str | list[int] lst = [typing.Dict[str, int] | None,] +var1: typing.Dict[str, int | None] +var2: int | str | None +var3: int | list[str | int] +var4: typing.Dict[typing.Tuple[int, int] | int, None] + cast_var = 1 cast_var = typing.cast(str | int, cast_var) diff --git a/tests/functional/a/alternative_union_syntax_error.py b/tests/functional/a/alternative_union_syntax_error.py index 54f9f8f52..4dbeacafc 100644 --- a/tests/functional/a/alternative_union_syntax_error.py +++ b/tests/functional/a/alternative_union_syntax_error.py @@ -14,6 +14,11 @@ from typing import NamedTuple, TypedDict Alias = str | typing.List[int] # [unsupported-binary-operation] lst = [typing.Dict[str, int] | None,] # [unsupported-binary-operation] +var1: typing.Dict[str, int | None] # [unsupported-binary-operation] +var2: int | str | None # [unsupported-binary-operation] +var3: int | typing.List[str | int] # [unsupported-binary-operation] +var4: typing.Dict[typing.Tuple[int, int] | int, None] # [unsupported-binary-operation] + cast_var = 1 cast_var = typing.cast(str | int, cast_var) # [unsupported-binary-operation] diff --git a/tests/functional/a/alternative_union_syntax_error.txt b/tests/functional/a/alternative_union_syntax_error.txt index 2149b3285..d6c1bca78 100644 --- a/tests/functional/a/alternative_union_syntax_error.txt +++ b/tests/functional/a/alternative_union_syntax_error.txt @@ -1,20 +1,24 @@ unsupported-binary-operation:14:8::unsupported operand type(s) for | unsupported-binary-operation:15:7::unsupported operand type(s) for | -unsupported-binary-operation:18:23::unsupported operand type(s) for | -unsupported-binary-operation:20:24::unsupported operand type(s) for | -unsupported-binary-operation:22:14::unsupported operand type(s) for | -unsupported-binary-operation:24:5::unsupported operand type(s) for | -unsupported-binary-operation:26:14:func:unsupported operand type(s) for | -unsupported-binary-operation:29:15:func2:unsupported operand type(s) for | -unsupported-binary-operation:35:9::unsupported operand type(s) for | -unsupported-binary-operation:42:36::unsupported operand type(s) for | -unsupported-binary-operation:45:12:CustomNamedTuple2:unsupported operand type(s) for | -unsupported-binary-operation:48:12:CustomNamedTuple3:unsupported operand type(s) for | -unsupported-binary-operation:52:54::unsupported operand type(s) for | -unsupported-binary-operation:54:60::unsupported operand type(s) for | -unsupported-binary-operation:57:12:CustomTypedDict3:unsupported operand type(s) for | -unsupported-binary-operation:60:12:CustomTypedDict4:unsupported operand type(s) for | -unsupported-binary-operation:71:12:CustomDataClass:unsupported operand type(s) for | -unsupported-binary-operation:75:12:CustomDataClass2:unsupported operand type(s) for | -unsupported-binary-operation:79:12:CustomDataClass3:unsupported operand type(s) for | -unsupported-binary-operation:84:12:CustomDataClass4:unsupported operand type(s) for | +unsupported-binary-operation:17:23::unsupported operand type(s) for | +unsupported-binary-operation:18:6::unsupported operand type(s) for | +unsupported-binary-operation:19:6::unsupported operand type(s) for | +unsupported-binary-operation:20:18::unsupported operand type(s) for | +unsupported-binary-operation:23:23::unsupported operand type(s) for | +unsupported-binary-operation:25:24::unsupported operand type(s) for | +unsupported-binary-operation:27:14::unsupported operand type(s) for | +unsupported-binary-operation:29:5::unsupported operand type(s) for | +unsupported-binary-operation:31:14:func:unsupported operand type(s) for | +unsupported-binary-operation:34:15:func2:unsupported operand type(s) for | +unsupported-binary-operation:40:9::unsupported operand type(s) for | +unsupported-binary-operation:47:36::unsupported operand type(s) for | +unsupported-binary-operation:50:12:CustomNamedTuple2:unsupported operand type(s) for | +unsupported-binary-operation:53:12:CustomNamedTuple3:unsupported operand type(s) for | +unsupported-binary-operation:57:54::unsupported operand type(s) for | +unsupported-binary-operation:59:60::unsupported operand type(s) for | +unsupported-binary-operation:62:12:CustomTypedDict3:unsupported operand type(s) for | +unsupported-binary-operation:65:12:CustomTypedDict4:unsupported operand type(s) for | +unsupported-binary-operation:76:12:CustomDataClass:unsupported operand type(s) for | +unsupported-binary-operation:80:12:CustomDataClass2:unsupported operand type(s) for | +unsupported-binary-operation:84:12:CustomDataClass3:unsupported operand type(s) for | +unsupported-binary-operation:89:12:CustomDataClass4:unsupported operand type(s) for | diff --git a/tests/functional/a/alternative_union_syntax_py37.py b/tests/functional/a/alternative_union_syntax_py37.py index 79bc28fc4..95e463fc1 100644 --- a/tests/functional/a/alternative_union_syntax_py37.py +++ b/tests/functional/a/alternative_union_syntax_py37.py @@ -15,6 +15,11 @@ from typing import NamedTuple, TypedDict Alias = str | typing.List[int] # [unsupported-binary-operation] lst = [typing.Dict[str, int] | None,] # [unsupported-binary-operation] +var1: typing.Dict[str, int | None] +var2: int | str | None +var3: int | list[str | int] +var4: typing.Dict[typing.Tuple[int, int] | int, None] + cast_var = 1 cast_var = typing.cast(str | int, cast_var) # [unsupported-binary-operation] diff --git a/tests/functional/a/alternative_union_syntax_py37.txt b/tests/functional/a/alternative_union_syntax_py37.txt index c7172808e..e97793af6 100644 --- a/tests/functional/a/alternative_union_syntax_py37.txt +++ b/tests/functional/a/alternative_union_syntax_py37.txt @@ -1,9 +1,9 @@ unsupported-binary-operation:15:8::unsupported operand type(s) for | unsupported-binary-operation:16:7::unsupported operand type(s) for | -unsupported-binary-operation:19:23::unsupported operand type(s) for | -unsupported-binary-operation:21:24::unsupported operand type(s) for | -unsupported-binary-operation:23:14::unsupported operand type(s) for | -unsupported-binary-operation:36:9::unsupported operand type(s) for | -unsupported-binary-operation:43:36::unsupported operand type(s) for | -unsupported-binary-operation:53:54::unsupported operand type(s) for | -unsupported-binary-operation:55:60::unsupported operand type(s) for | +unsupported-binary-operation:24:23::unsupported operand type(s) for | +unsupported-binary-operation:26:24::unsupported operand type(s) for | +unsupported-binary-operation:28:14::unsupported operand type(s) for | +unsupported-binary-operation:41:9::unsupported operand type(s) for | +unsupported-binary-operation:48:36::unsupported operand type(s) for | +unsupported-binary-operation:58:54::unsupported operand type(s) for | +unsupported-binary-operation:60:60::unsupported operand type(s) for | |