Skip to content

Commit

Permalink
Fix issues with new typing Union syntax (Py310) (#8122)
Browse files Browse the repository at this point in the history
* Fix issues with new typing Union syntax (Py310)
* Upgrade astroid to 2.14.1
  • Loading branch information
cdce8p authored Feb 1, 2023
1 parent c70285d commit 1b5bba4
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8119.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix issue with new typing Union syntax in runtime context for Python 3.10+.

Closes #8119
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies = [
# Also upgrade requirements_test_min.txt.
# Pinned to dev of second minor update to allow editable installs and fix primer issues,
# see https://github.com/PyCQA/astroid/issues/1341
"astroid>=2.13.3,<=2.15.0-dev0",
"astroid>=2.14.1,<=2.16.0-dev0",
"isort>=4.2.5,<6",
"mccabe>=0.6,<0.8",
"tomli>=1.1.0;python_version<'3.11'",
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_min.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-e .[testutils,spelling]
# astroid dependency is also defined in pyproject.toml
astroid==2.13.3 # Pinned to a specific version for tests
astroid==2.14.1 # Pinned to a specific version for tests
typing-extensions~=4.4
py~=1.11.0
pytest~=7.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ py-version=3.8

[testoptions]
min_pyver=3.8
max_pyver=3.10
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Regression test for alternative Union syntax in runtime contexts.
Syntax support was added in Python 3.10.
The code snipped should not raise any errors.
https://github.com/PyCQA/pylint/issues/8119
"""
# pylint: disable=missing-docstring,too-few-public-methods
from typing import Generic, TypeVar

T = TypeVar("T")


class Coordinator(Generic[T]):
def __init__(self, update_interval=None) -> None:
self.update_interval = update_interval


class Child(Coordinator[int | str]):
def __init__(self) -> None:
Coordinator.__init__(self, update_interval=2)

def _async_update_data(self):
assert self.update_interval
self.update_interval = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.10
2 changes: 1 addition & 1 deletion tests/functional/w/wrong_exception_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

try:
1/0
except (ValueError | TypeError): # [wrong-exception-operation]
except (ValueError | TypeError): # [catching-non-exception,wrong-exception-operation]
pass

try:
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/w/wrong_exception_operation.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.10
1 change: 1 addition & 0 deletions tests/functional/w/wrong_exception_operation.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
catching-non-exception:6:8:6:30::"Catching an exception which doesn't inherit from Exception: ValueError | TypeError":UNDEFINED
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
18 changes: 18 additions & 0 deletions tests/functional/w/wrong_exception_operation_py37.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pylint: disable=missing-docstring, superfluous-parens


try:
1/0
except (ValueError | TypeError): # [wrong-exception-operation]
pass

try:
1/0
except (ValueError + TypeError): # [wrong-exception-operation]
pass


try:
1/0
except (ValueError < TypeError): # [wrong-exception-operation]
pass
3 changes: 3 additions & 0 deletions tests/functional/w/wrong_exception_operation_py37.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[testoptions]
min_pyver=3.7
max_pyver=3.10
3 changes: 3 additions & 0 deletions tests/functional/w/wrong_exception_operation_py37.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wrong-exception-operation:6:8:6:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:11:8:11:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED
wrong-exception-operation:17:8:17:30::Invalid exception operation. Did you mean '(ValueError, TypeError)' instead?:UNDEFINED

0 comments on commit 1b5bba4

Please sign in to comment.