From 2b7e9b3eab13feee1705484d6d1f2e882c93ae9f Mon Sep 17 00:00:00 2001 From: Dan Redding <125183946+dangotbanned@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:08:53 +0000 Subject: [PATCH] fix: Replace circular import in `schemapi.py` (#3751) --- altair/utils/schemapi.py | 12 +++++------- tools/schemapi/schemapi.py | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/altair/utils/schemapi.py b/altair/utils/schemapi.py index cf5f2e10e..638f2f50f 100644 --- a/altair/utils/schemapi.py +++ b/altair/utils/schemapi.py @@ -35,11 +35,6 @@ import narwhals.stable.v1 as nw from packaging.version import Version -# This leads to circular imports with the vegalite module. Currently, this works -# but be aware that when you access it in this script, the vegalite module might -# not yet be fully instantiated in case your code is being executed during import time -from altair import vegalite - if sys.version_info >= (3, 12): from typing import Protocol, TypeAliasType, runtime_checkable else: @@ -709,6 +704,8 @@ def _get_altair_class_for_error( This should lead to more informative error messages pointing the user closer to the source of the issue. """ + from altair import vegalite + for prop_name in reversed(error.absolute_path): # Check if str as e.g. first item can be a 0 if isinstance(prop_name, str): @@ -1597,14 +1594,15 @@ def __init__(self, prop: str, schema: dict[str, Any]) -> None: self.schema = schema def __get__(self, obj, cls): + from altair import vegalite + self.obj = obj self.cls = cls # The docs from the encoding class parameter (e.g. `bin` in X, Color, # etc); this provides a general description of the parameter. self.__doc__ = self.schema["description"].replace("__", "**") property_name = f"{self.prop}"[0].upper() + f"{self.prop}"[1:] - if hasattr(vegalite, property_name): - altair_prop = getattr(vegalite, property_name) + if altair_prop := getattr(vegalite, property_name, None): # Add the docstring from the helper class (e.g. `BinParams`) so # that all the parameter names of the helper class are included in # the final docstring diff --git a/tools/schemapi/schemapi.py b/tools/schemapi/schemapi.py index 5ee638266..24de6faae 100644 --- a/tools/schemapi/schemapi.py +++ b/tools/schemapi/schemapi.py @@ -33,11 +33,6 @@ import narwhals.stable.v1 as nw from packaging.version import Version -# This leads to circular imports with the vegalite module. Currently, this works -# but be aware that when you access it in this script, the vegalite module might -# not yet be fully instantiated in case your code is being executed during import time -from altair import vegalite - if sys.version_info >= (3, 12): from typing import Protocol, TypeAliasType, runtime_checkable else: @@ -707,6 +702,8 @@ def _get_altair_class_for_error( This should lead to more informative error messages pointing the user closer to the source of the issue. """ + from altair import vegalite + for prop_name in reversed(error.absolute_path): # Check if str as e.g. first item can be a 0 if isinstance(prop_name, str): @@ -1595,14 +1592,15 @@ def __init__(self, prop: str, schema: dict[str, Any]) -> None: self.schema = schema def __get__(self, obj, cls): + from altair import vegalite + self.obj = obj self.cls = cls # The docs from the encoding class parameter (e.g. `bin` in X, Color, # etc); this provides a general description of the parameter. self.__doc__ = self.schema["description"].replace("__", "**") property_name = f"{self.prop}"[0].upper() + f"{self.prop}"[1:] - if hasattr(vegalite, property_name): - altair_prop = getattr(vegalite, property_name) + if altair_prop := getattr(vegalite, property_name, None): # Add the docstring from the helper class (e.g. `BinParams`) so # that all the parameter names of the helper class are included in # the final docstring