Skip to content

Commit 1b5bba4

Browse files
authored
Fix issues with new typing Union syntax (Py310) (#8122)
* Fix issues with new typing Union syntax (Py310) * Upgrade astroid to 2.14.1
1 parent c70285d commit 1b5bba4

12 files changed

+60
-3
lines changed

doc/whatsnew/fragments/8119.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix issue with new typing Union syntax in runtime context for Python 3.10+.
2+
3+
Closes #8119

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies = [
3939
# Also upgrade requirements_test_min.txt.
4040
# Pinned to dev of second minor update to allow editable installs and fix primer issues,
4141
# see https://github.com/PyCQA/astroid/issues/1341
42-
"astroid>=2.13.3,<=2.15.0-dev0",
42+
"astroid>=2.14.1,<=2.16.0-dev0",
4343
"isort>=4.2.5,<6",
4444
"mccabe>=0.6,<0.8",
4545
"tomli>=1.1.0;python_version<'3.11'",

requirements_test_min.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-e .[testutils,spelling]
22
# astroid dependency is also defined in pyproject.toml
3-
astroid==2.13.3 # Pinned to a specific version for tests
3+
astroid==2.14.1 # Pinned to a specific version for tests
44
typing-extensions~=4.4
55
py~=1.11.0
66
pytest~=7.2

tests/functional/a/alternative/alternative_union_syntax_error.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ py-version=3.8
33

44
[testoptions]
55
min_pyver=3.8
6+
max_pyver=3.10
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Regression test for alternative Union syntax in runtime contexts.
2+
Syntax support was added in Python 3.10.
3+
4+
The code snipped should not raise any errors.
5+
https://github.com/PyCQA/pylint/issues/8119
6+
"""
7+
# pylint: disable=missing-docstring,too-few-public-methods
8+
from typing import Generic, TypeVar
9+
10+
T = TypeVar("T")
11+
12+
13+
class Coordinator(Generic[T]):
14+
def __init__(self, update_interval=None) -> None:
15+
self.update_interval = update_interval
16+
17+
18+
class Child(Coordinator[int | str]):
19+
def __init__(self) -> None:
20+
Coordinator.__init__(self, update_interval=2)
21+
22+
def _async_update_data(self):
23+
assert self.update_interval
24+
self.update_interval = 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.10

tests/functional/w/wrong_exception_operation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
try:
55
1/0
6-
except (ValueError | TypeError): # [wrong-exception-operation]
6+
except (ValueError | TypeError): # [catching-non-exception,wrong-exception-operation]
77
pass
88

99
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.10
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
catching-non-exception:6:8:6:30::"Catching an exception which doesn't inherit from Exception: ValueError | TypeError":UNDEFINED
12
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
23
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
34
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# pylint: disable=missing-docstring, superfluous-parens
2+
3+
4+
try:
5+
1/0
6+
except (ValueError | TypeError): # [wrong-exception-operation]
7+
pass
8+
9+
try:
10+
1/0
11+
except (ValueError + TypeError): # [wrong-exception-operation]
12+
pass
13+
14+
15+
try:
16+
1/0
17+
except (ValueError < TypeError): # [wrong-exception-operation]
18+
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[testoptions]
2+
min_pyver=3.7
3+
max_pyver=3.10
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
2+
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
3+
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED

0 commit comments

Comments
 (0)