Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9d54dd9

Browse filesBrowse 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 41820bc commit 9d54dd9
Copy full SHA for 9d54dd9

File tree

1 file changed

+34
-3
lines changed
Filter options

1 file changed

+34
-3
lines changed

‎astroid/brain/brain_typing.py

Copy file name to clipboardExpand all lines: astroid/brain/brain_typing.py
+34-3Lines changed: 34 additions & 3 deletions
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.builder import _extract_single_node, AstroidBuilder
17+
from astroid.brain.helpers import register_module_extender
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.