From 7b2cc0e411682c9de98818a0ac5644bda28f52b6 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:12:20 +0000 Subject: [PATCH] refactor: Replace unconditional `typing_extensions` imports https://github.com/vega/altair/pull/3591, https://github.com/vega/altair/issues/3620, https://github.com/vega/altair/pull/3593 --- altair/utils/_transformed_data.py | 7 ++++++- altair/utils/data.py | 20 +++++++++++++++----- altair/utils/display.py | 11 +++++++++-- altair/utils/mimebundle.py | 11 +++++++++-- altair/utils/plugin_registry.py | 13 +++++++++++-- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/altair/utils/_transformed_data.py b/altair/utils/_transformed_data.py index a1460f2c4..06c3dfe94 100644 --- a/altair/utils/_transformed_data.py +++ b/altair/utils/_transformed_data.py @@ -1,7 +1,6 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any, overload -from typing_extensions import TypeAlias from altair import ( Chart, @@ -31,8 +30,14 @@ from altair.utils.schemapi import Undefined if TYPE_CHECKING: + import sys from collections.abc import Iterable + if sys.version_info >= (3, 10): + from typing import TypeAlias + else: + from typing_extensions import TypeAlias + from altair.typing import ChartType from altair.utils.core import DataFrameLike diff --git a/altair/utils/data.py b/altair/utils/data.py index 89ec31f1f..bb6868b07 100644 --- a/altair/utils/data.py +++ b/altair/utils/data.py @@ -12,14 +12,11 @@ Any, Callable, Literal, - Protocol, TypedDict, TypeVar, Union, overload, - runtime_checkable, ) -from typing_extensions import Concatenate, ParamSpec, TypeAlias import narwhals.stable.v1 as nw from narwhals.dependencies import is_pandas_dataframe as _is_pandas_dataframe @@ -36,11 +33,24 @@ from .plugin_registry import PluginRegistry if sys.version_info >= (3, 13): - from typing import TypeIs + from typing import Protocol, runtime_checkable else: - from typing_extensions import TypeIs + from typing_extensions import Protocol, runtime_checkable +if sys.version_info >= (3, 10): + from typing import Concatenate, ParamSpec +else: + from typing_extensions import Concatenate, ParamSpec if TYPE_CHECKING: + if sys.version_info >= (3, 13): + from typing import TypeIs + else: + from typing_extensions import TypeIs + + if sys.version_info >= (3, 10): + from typing import TypeAlias + else: + from typing_extensions import TypeAlias import pandas as pd import pyarrow as pa diff --git a/altair/utils/display.py b/altair/utils/display.py index b6d6d39f5..223417a11 100644 --- a/altair/utils/display.py +++ b/altair/utils/display.py @@ -4,14 +4,21 @@ import pkgutil import textwrap import uuid -from typing import Any, Callable, Union -from typing_extensions import TypeAlias +from typing import TYPE_CHECKING, Any, Callable, Union from ._vegafusion_data import compile_with_vegafusion, using_vegafusion from .mimebundle import spec_to_mimebundle from .plugin_registry import PluginEnabler, PluginRegistry from .schemapi import validate_jsonschema +if TYPE_CHECKING: + import sys + + if sys.version_info >= (3, 10): + from typing import TypeAlias + else: + from typing_extensions import TypeAlias + # ============================================================================== # Renderer registry # ============================================================================== diff --git a/altair/utils/mimebundle.py b/altair/utils/mimebundle.py index 029388025..575b3f71c 100644 --- a/altair/utils/mimebundle.py +++ b/altair/utils/mimebundle.py @@ -1,12 +1,19 @@ from __future__ import annotations import struct -from typing import Any, Literal, cast, overload -from typing_extensions import TypeAlias +from typing import TYPE_CHECKING, Any, Literal, cast, overload from ._importers import import_vl_convert, vl_version_for_vl_convert from .html import spec_to_html +if TYPE_CHECKING: + import sys + + if sys.version_info >= (3, 10): + from typing import TypeAlias + else: + from typing_extensions import TypeAlias + MimeBundleFormat: TypeAlias = Literal[ "html", "json", "png", "svg", "pdf", "vega", "vega-lite" ] diff --git a/altair/utils/plugin_registry.py b/altair/utils/plugin_registry.py index b4505fd9f..81e34cb8d 100644 --- a/altair/utils/plugin_registry.py +++ b/altair/utils/plugin_registry.py @@ -1,12 +1,21 @@ from __future__ import annotations +import sys from functools import partial from importlib.metadata import entry_points -from typing import TYPE_CHECKING, Any, Callable, Generic, cast -from typing_extensions import TypeAliasType, TypeIs, TypeVar +from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, cast from altair.utils.deprecation import deprecated_warn +if sys.version_info >= (3, 13): + from typing import TypeIs +else: + from typing_extensions import TypeIs +if sys.version_info >= (3, 12): + from typing import TypeAliasType +else: + from typing_extensions import TypeAliasType + if TYPE_CHECKING: from types import TracebackType