Skip to content

Commit

Permalink
fix: Replace circular import in schemapi.py (#3751)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned authored Jan 6, 2025
1 parent be5e9ec commit 2b7e9b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 5 additions & 7 deletions altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b7e9b3

Please sign in to comment.