Skip to content

Commit 1093e5d

Browse files
cdce8pPierre-Sassoulas
authored andcommitted
Fix issue with nested PEP 604 syntax
1 parent b5f21f4 commit 1093e5d

File tree

7 files changed

+68
-32
lines changed

7 files changed

+68
-32
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Pylint's ChangeLog
5151

5252
* Fix issue with nested PEP 585 syntax
5353

54+
* Fix issue with nested PEP 604 syntax
55+
5456
What's New in Pylint 2.6.1?
5557
===========================
5658
Release date: TBA

pylint/checkers/typecheck.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,17 +1662,32 @@ def visit_binop(self, node: astroid.BinOp):
16621662
astroid.Tuple,
16631663
astroid.Set,
16641664
astroid.List,
1665+
astroid.BinOp,
16651666
),
16661667
)
16671668
):
1668-
for n in (node.left, node.right):
1669-
n = helpers.object_type(n)
1670-
if isinstance(n, astroid.ClassDef):
1671-
if is_classdef_type(n):
1672-
self.add_message(
1673-
"unsupported-binary-operation", args=msg, node=node
1674-
)
1669+
allowed_nested_syntax = False
1670+
if is_postponed_evaluation_enabled(node):
1671+
parent_node = node.parent
1672+
while True:
1673+
if isinstance(
1674+
parent_node,
1675+
(astroid.AnnAssign, astroid.Arguments, astroid.FunctionDef),
1676+
):
1677+
allowed_nested_syntax = True
1678+
break
1679+
parent_node = parent_node.parent
1680+
if isinstance(parent_node, astroid.Module):
16751681
break
1682+
if allowed_nested_syntax is False:
1683+
for n in (node.left, node.right):
1684+
n = helpers.object_type(n)
1685+
if isinstance(n, astroid.ClassDef):
1686+
if is_classdef_type(n):
1687+
self.add_message(
1688+
"unsupported-binary-operation", args=msg, node=node
1689+
)
1690+
break
16761691

16771692
@check_messages("unsupported-binary-operation")
16781693
def _visit_binop(self, node):

tests/functional/a/alternative_union_syntax.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
Alias = str | list[int]
1010
lst = [typing.Dict[str, int] | None,]
1111

12+
var1: typing.Dict[str, int | None]
13+
var2: int | str | None
14+
var3: int | list[str | int]
15+
var4: typing.Dict[typing.Tuple[int, int] | int, None]
16+
1217
cast_var = 1
1318
cast_var = typing.cast(str | int, cast_var)
1419

tests/functional/a/alternative_union_syntax_error.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
Alias = str | typing.List[int] # [unsupported-binary-operation]
1515
lst = [typing.Dict[str, int] | None,] # [unsupported-binary-operation]
1616

17+
var1: typing.Dict[str, int | None] # [unsupported-binary-operation]
18+
var2: int | str | None # [unsupported-binary-operation]
19+
var3: int | typing.List[str | int] # [unsupported-binary-operation]
20+
var4: typing.Dict[typing.Tuple[int, int] | int, None] # [unsupported-binary-operation]
21+
1722
cast_var = 1
1823
cast_var = typing.cast(str | int, cast_var) # [unsupported-binary-operation]
1924

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
unsupported-binary-operation:14:8::unsupported operand type(s) for |
22
unsupported-binary-operation:15:7::unsupported operand type(s) for |
3-
unsupported-binary-operation:18:23::unsupported operand type(s) for |
4-
unsupported-binary-operation:20:24::unsupported operand type(s) for |
5-
unsupported-binary-operation:22:14::unsupported operand type(s) for |
6-
unsupported-binary-operation:24:5::unsupported operand type(s) for |
7-
unsupported-binary-operation:26:14:func:unsupported operand type(s) for |
8-
unsupported-binary-operation:29:15:func2:unsupported operand type(s) for |
9-
unsupported-binary-operation:35:9::unsupported operand type(s) for |
10-
unsupported-binary-operation:42:36::unsupported operand type(s) for |
11-
unsupported-binary-operation:45:12:CustomNamedTuple2:unsupported operand type(s) for |
12-
unsupported-binary-operation:48:12:CustomNamedTuple3:unsupported operand type(s) for |
13-
unsupported-binary-operation:52:54::unsupported operand type(s) for |
14-
unsupported-binary-operation:54:60::unsupported operand type(s) for |
15-
unsupported-binary-operation:57:12:CustomTypedDict3:unsupported operand type(s) for |
16-
unsupported-binary-operation:60:12:CustomTypedDict4:unsupported operand type(s) for |
17-
unsupported-binary-operation:71:12:CustomDataClass:unsupported operand type(s) for |
18-
unsupported-binary-operation:75:12:CustomDataClass2:unsupported operand type(s) for |
19-
unsupported-binary-operation:79:12:CustomDataClass3:unsupported operand type(s) for |
20-
unsupported-binary-operation:84:12:CustomDataClass4:unsupported operand type(s) for |
3+
unsupported-binary-operation:17:23::unsupported operand type(s) for |
4+
unsupported-binary-operation:18:6::unsupported operand type(s) for |
5+
unsupported-binary-operation:19:6::unsupported operand type(s) for |
6+
unsupported-binary-operation:20:18::unsupported operand type(s) for |
7+
unsupported-binary-operation:23:23::unsupported operand type(s) for |
8+
unsupported-binary-operation:25:24::unsupported operand type(s) for |
9+
unsupported-binary-operation:27:14::unsupported operand type(s) for |
10+
unsupported-binary-operation:29:5::unsupported operand type(s) for |
11+
unsupported-binary-operation:31:14:func:unsupported operand type(s) for |
12+
unsupported-binary-operation:34:15:func2:unsupported operand type(s) for |
13+
unsupported-binary-operation:40:9::unsupported operand type(s) for |
14+
unsupported-binary-operation:47:36::unsupported operand type(s) for |
15+
unsupported-binary-operation:50:12:CustomNamedTuple2:unsupported operand type(s) for |
16+
unsupported-binary-operation:53:12:CustomNamedTuple3:unsupported operand type(s) for |
17+
unsupported-binary-operation:57:54::unsupported operand type(s) for |
18+
unsupported-binary-operation:59:60::unsupported operand type(s) for |
19+
unsupported-binary-operation:62:12:CustomTypedDict3:unsupported operand type(s) for |
20+
unsupported-binary-operation:65:12:CustomTypedDict4:unsupported operand type(s) for |
21+
unsupported-binary-operation:76:12:CustomDataClass:unsupported operand type(s) for |
22+
unsupported-binary-operation:80:12:CustomDataClass2:unsupported operand type(s) for |
23+
unsupported-binary-operation:84:12:CustomDataClass3:unsupported operand type(s) for |
24+
unsupported-binary-operation:89:12:CustomDataClass4:unsupported operand type(s) for |

tests/functional/a/alternative_union_syntax_py37.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
Alias = str | typing.List[int] # [unsupported-binary-operation]
1616
lst = [typing.Dict[str, int] | None,] # [unsupported-binary-operation]
1717

18+
var1: typing.Dict[str, int | None]
19+
var2: int | str | None
20+
var3: int | list[str | int]
21+
var4: typing.Dict[typing.Tuple[int, int] | int, None]
22+
1823
cast_var = 1
1924
cast_var = typing.cast(str | int, cast_var) # [unsupported-binary-operation]
2025

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
unsupported-binary-operation:15:8::unsupported operand type(s) for |
22
unsupported-binary-operation:16:7::unsupported operand type(s) for |
3-
unsupported-binary-operation:19:23::unsupported operand type(s) for |
4-
unsupported-binary-operation:21:24::unsupported operand type(s) for |
5-
unsupported-binary-operation:23:14::unsupported operand type(s) for |
6-
unsupported-binary-operation:36:9::unsupported operand type(s) for |
7-
unsupported-binary-operation:43:36::unsupported operand type(s) for |
8-
unsupported-binary-operation:53:54::unsupported operand type(s) for |
9-
unsupported-binary-operation:55:60::unsupported operand type(s) for |
3+
unsupported-binary-operation:24:23::unsupported operand type(s) for |
4+
unsupported-binary-operation:26:24::unsupported operand type(s) for |
5+
unsupported-binary-operation:28:14::unsupported operand type(s) for |
6+
unsupported-binary-operation:41:9::unsupported operand type(s) for |
7+
unsupported-binary-operation:48:36::unsupported operand type(s) for |
8+
unsupported-binary-operation:58:54::unsupported operand type(s) for |
9+
unsupported-binary-operation:60:60::unsupported operand type(s) for |

0 commit comments

Comments
 (0)