Skip to content

Commit 43111bb

Browse files
authored
Initial fixes for Python 3.14 (#2747)
1 parent 5937f3d commit 43111bb

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

astroid/brain/brain_pathlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from astroid import bases, context, nodes
1010
from astroid.builder import _extract_single_node
11-
from astroid.const import PY313_PLUS
11+
from astroid.const import PY313
1212
from astroid.exceptions import InferenceError, UseInferenceDefault
1313
from astroid.inference_tip import inference_tip
1414
from astroid.manager import AstroidManager
@@ -29,7 +29,7 @@ def _looks_like_parents_subscript(node: nodes.Subscript) -> bool:
2929
value = next(node.value.infer())
3030
except (InferenceError, StopIteration):
3131
return False
32-
parents = "builtins.tuple" if PY313_PLUS else "pathlib._PathParents"
32+
parents = "builtins.tuple" if PY313 else "pathlib._PathParents"
3333
return (
3434
isinstance(value, bases.Instance)
3535
and isinstance(value._proxied, nodes.ClassDef)

astroid/brain/brain_typing.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from astroid import context
1616
from astroid.brain.helpers import register_module_extender
1717
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
18-
from astroid.const import PY312_PLUS, PY313_PLUS
18+
from astroid.const import PY312_PLUS, PY313_PLUS, PY314_PLUS
1919
from astroid.exceptions import (
2020
AstroidSyntaxError,
2121
AttributeInferenceError,
@@ -78,7 +78,7 @@ class {0}(metaclass=Meta):
7878
"typing.MutableMapping",
7979
"typing.Sequence",
8080
"typing.MutableSequence",
81-
"typing.ByteString",
81+
"typing.ByteString", # removed in 3.14
8282
"typing.Tuple",
8383
"typing.List",
8484
"typing.Deque",
@@ -431,9 +431,8 @@ def infer_typing_cast(
431431

432432

433433
def _typing_transform():
434-
return AstroidBuilder(AstroidManager()).string_build(
435-
textwrap.dedent(
436-
"""
434+
code = textwrap.dedent(
435+
"""
437436
class Generic:
438437
@classmethod
439438
def __class_getitem__(cls, item): return cls
@@ -467,8 +466,16 @@ class Match:
467466
@classmethod
468467
def __class_getitem__(cls, item): return cls
469468
"""
470-
)
471469
)
470+
if PY314_PLUS:
471+
code += textwrap.dedent(
472+
"""
473+
class Union:
474+
@classmethod
475+
def __class_getitem__(cls, item): return cls
476+
"""
477+
)
478+
return AstroidBuilder(AstroidManager()).string_build(code)
472479

473480

474481
def register(manager: AstroidManager) -> None:

astroid/const.py

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
PY310_PLUS = sys.version_info >= (3, 10)
99
PY311_PLUS = sys.version_info >= (3, 11)
1010
PY312_PLUS = sys.version_info >= (3, 12)
11+
PY313 = sys.version_info[:2] == (3, 13)
1112
PY313_PLUS = sys.version_info >= (3, 13)
13+
PY314_PLUS = sys.version_info >= (3, 14)
1214

1315
WIN32 = sys.platform == "win32"
1416

tests/brain/test_brain.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from astroid import MANAGER, builder, nodes, objects, test_utils, util
1616
from astroid.bases import Instance
1717
from astroid.brain.brain_namedtuple_enum import _get_namedtuple_fields
18-
from astroid.const import PY312_PLUS, PY313_PLUS
18+
from astroid.const import PY312_PLUS, PY313_PLUS, PY314_PLUS
1919
from astroid.exceptions import (
2020
AttributeInferenceError,
2121
InferenceError,
@@ -335,6 +335,9 @@ def test_collections_object_not_yet_subscriptable_2(self):
335335
with self.assertRaises(InferenceError):
336336
next(node.infer())
337337

338+
@pytest.mark.skipif(
339+
PY314_PLUS, reason="collections.abc.ByteString was removed in 3.14"
340+
)
338341
def test_collections_object_subscriptable_3(self):
339342
"""With Python 3.9 the ByteString class of the collections module is subscriptable
340343
(but not the same class from typing module)"""
@@ -918,6 +921,7 @@ class Derived(typing.Hashable, typing.Iterator[int]):
918921
],
919922
)
920923

924+
@pytest.mark.skipif(PY314_PLUS, reason="typing.ByteString was removed in 3.14")
921925
def test_typing_object_notsubscriptable_3(self):
922926
"""Until python39 ByteString class of the typing module is not
923927
subscriptable (whereas it is in the collections' module)"""

tests/brain/test_pathlib.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import astroid
77
from astroid import bases
8-
from astroid.const import PY310_PLUS, PY313_PLUS
8+
from astroid.const import PY310_PLUS, PY313
99
from astroid.util import Uninferable
1010

1111

@@ -23,7 +23,7 @@ def test_inference_parents() -> None:
2323
inferred = name_node.inferred()
2424
assert len(inferred) == 1
2525
assert isinstance(inferred[0], bases.Instance)
26-
if PY313_PLUS:
26+
if PY313:
2727
assert inferred[0].qname() == "builtins.tuple"
2828
else:
2929
assert inferred[0].qname() == "pathlib._PathParents"
@@ -43,7 +43,7 @@ def test_inference_parents_subscript_index() -> None:
4343
inferred = path.inferred()
4444
assert len(inferred) == 1
4545
assert isinstance(inferred[0], bases.Instance)
46-
if PY313_PLUS:
46+
if PY313:
4747
assert inferred[0].qname() == "pathlib._local.Path"
4848
else:
4949
assert inferred[0].qname() == "pathlib.Path"

tests/test_inference.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from astroid.arguments import CallSite
3535
from astroid.bases import BoundMethod, Generator, Instance, UnboundMethod, UnionType
3636
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node, parse
37-
from astroid.const import IS_PYPY, PY310_PLUS, PY312_PLUS
37+
from astroid.const import IS_PYPY, PY310_PLUS, PY312_PLUS, PY314_PLUS
3838
from astroid.context import CallContext, InferenceContext
3939
from astroid.exceptions import (
4040
AstroidTypeError,
@@ -1308,8 +1308,12 @@ class B: ...
13081308
assert i0.bool_value() is True
13091309
assert i0.pytype() == "types.UnionType"
13101310
assert i0.display_type() == "UnionType"
1311-
assert str(i0) == "UnionType(UnionType)"
1312-
assert repr(i0) == f"<UnionType(UnionType) l.0 at 0x{id(i0)}>"
1311+
if PY314_PLUS:
1312+
assert str(i0) == "UnionType(Union)"
1313+
assert repr(i0) == f"<UnionType(Union) l.0 at 0x{id(i0)}>"
1314+
else:
1315+
assert str(i0) == "UnionType(UnionType)"
1316+
assert repr(i0) == f"<UnionType(UnionType) l.0 at 0x{id(i0)}>"
13131317

13141318
i1 = ast_nodes[1].inferred()[0]
13151319
assert isinstance(i1, UnionType)

0 commit comments

Comments
 (0)