Skip to content

Commit f2120db

Browse files
Python 3.12 adjustments for typing brain
A C-accelerator was introduced for the typing module in Python 3.12 (see python/cpython#103764). Because a pure python source is no longer available, now we stub out the missing classes and provide some __class_getitem__ methods to allow subscripting like Type[int]. This may mean when 3.12 is the minimum, we can remove older brain features like infer_typing_alias().
1 parent 6d4f364 commit f2120db

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

Diff for: astroid/brain/brain_typing.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
from __future__ import annotations
88

9+
import textwrap
910
import typing
1011
from collections.abc import Iterator
1112
from functools import partial
1213
from typing import Final
1314

1415
from astroid import context, extract_node, inference_tip
15-
from astroid.builder import _extract_single_node
16-
from astroid.const import PY39_PLUS
16+
from astroid.brain.helpers import register_module_extender
17+
from astroid.builder import AstroidBuilder, _extract_single_node
18+
from astroid.const import PY39_PLUS, PY312_PLUS
1719
from astroid.exceptions import (
1820
AttributeInferenceError,
1921
InferenceError,
@@ -231,7 +233,8 @@ def _looks_like_typing_alias(node: Call) -> bool:
231233
"""
232234
return (
233235
isinstance(node.func, Name)
234-
and node.func.name == "_alias"
236+
# TODO: remove _DeprecatedGenericAlias when Py3.14 min
237+
and node.func.name in {"_alias", "_DeprecatedGenericAlias"}
235238
and (
236239
# _alias function works also for builtins object such as list and dict
237240
isinstance(node.args[0], (Attribute, Name))
@@ -273,6 +276,8 @@ def infer_typing_alias(
273276
274277
:param node: call node
275278
:param context: inference context
279+
280+
# TODO: evaluate if still necessary when Py3.12 is minimum
276281
"""
277282
if (
278283
not isinstance(node.parent, Assign)
@@ -415,6 +420,29 @@ def infer_typing_cast(
415420
return node.args[1].infer(context=ctx)
416421

417422

423+
def _typing_transform():
424+
return AstroidBuilder(AstroidManager()).string_build(
425+
textwrap.dedent(
426+
"""
427+
class Generic:
428+
@classmethod
429+
def __class_getitem__(cls, item): return cls
430+
class ParamSpec: ...
431+
class ParamSpecArgs: ...
432+
class ParamSpecKwargs: ...
433+
class TypeAlias: ...
434+
class Type:
435+
@classmethod
436+
def __class_getitem__(cls, item): return cls
437+
class TypeVar:
438+
@classmethod
439+
def __class_getitem__(cls, item): return cls
440+
class TypeVarTuple: ...
441+
"""
442+
)
443+
)
444+
445+
418446
AstroidManager().register_transform(
419447
Call,
420448
inference_tip(infer_typing_typevar_or_newtype),
@@ -442,3 +470,6 @@ def infer_typing_cast(
442470
AstroidManager().register_transform(
443471
Call, inference_tip(infer_special_alias), _looks_like_special_alias
444472
)
473+
474+
if PY312_PLUS:
475+
register_module_extender(AstroidManager(), "typing", _typing_transform)

0 commit comments

Comments
 (0)