diff --git a/IPython/core/guarded_eval.py b/IPython/core/guarded_eval.py index d8ca8ce..4ae5a41 100644 --- a/IPython/core/guarded_eval.py +++ b/IPython/core/guarded_eval.py @@ -14,10 +14,6 @@ from typing import ( get_args, get_origin, ) -from typing_extensions import ( - Self, # Python >=3.10 - TypeAliasType, # Python >=3.12 -) import ast import builtins import collections @@ -27,10 +23,20 @@ from functools import cached_property from dataclasses import dataclass, field from types import MethodDescriptorType, ModuleType - from IPython.utils.decorators import undoc +if sys.version_info < (3, 11): + from typing_extensions import Self +else: + from typing import Self + +if sys.version_info < (3, 12): + from typing_extensions import TypeAliasType +else: + from typing import TypeAliasType + + @undoc class HasGetItem(Protocol): def __getitem__(self, key) -> None: diff --git a/IPython/core/tests/test_guarded_eval.py b/IPython/core/tests/test_guarded_eval.py index 9d8052f..cb45db5 100644 --- a/IPython/core/tests/test_guarded_eval.py +++ b/IPython/core/tests/test_guarded_eval.py @@ -1,3 +1,4 @@ +import sys from contextlib import contextmanager from typing import NamedTuple, Literal, NewType from functools import partial @@ -7,14 +8,21 @@ from IPython.core.guarded_eval import ( guarded_eval, _unbind_method, ) -from typing_extensions import ( - Self, # Python >=3.10 - TypeAliasType, # Python >=3.12 -) from IPython.testing import decorators as dec import pytest +if sys.version_info < (3, 11): + from typing_extensions import Self +else: + from typing import Self + +if sys.version_info < (3, 12): + from typing_extensions import TypeAliasType +else: + from typing import TypeAliasType + + def create_context(evaluation: str, **kwargs): return EvaluationContext(locals=kwargs, globals={}, evaluation=evaluation)