Skip to content

Commit 2b7e9b3

Browse files
authored
fix: Replace circular import in schemapi.py (#3751)
1 parent be5e9ec commit 2b7e9b3

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

altair/utils/schemapi.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
import narwhals.stable.v1 as nw
3636
from packaging.version import Version
3737

38-
# This leads to circular imports with the vegalite module. Currently, this works
39-
# but be aware that when you access it in this script, the vegalite module might
40-
# not yet be fully instantiated in case your code is being executed during import time
41-
from altair import vegalite
42-
4338
if sys.version_info >= (3, 12):
4439
from typing import Protocol, TypeAliasType, runtime_checkable
4540
else:
@@ -709,6 +704,8 @@ def _get_altair_class_for_error(
709704
710705
This should lead to more informative error messages pointing the user closer to the source of the issue.
711706
"""
707+
from altair import vegalite
708+
712709
for prop_name in reversed(error.absolute_path):
713710
# Check if str as e.g. first item can be a 0
714711
if isinstance(prop_name, str):
@@ -1597,14 +1594,15 @@ def __init__(self, prop: str, schema: dict[str, Any]) -> None:
15971594
self.schema = schema
15981595

15991596
def __get__(self, obj, cls):
1597+
from altair import vegalite
1598+
16001599
self.obj = obj
16011600
self.cls = cls
16021601
# The docs from the encoding class parameter (e.g. `bin` in X, Color,
16031602
# etc); this provides a general description of the parameter.
16041603
self.__doc__ = self.schema["description"].replace("__", "**")
16051604
property_name = f"{self.prop}"[0].upper() + f"{self.prop}"[1:]
1606-
if hasattr(vegalite, property_name):
1607-
altair_prop = getattr(vegalite, property_name)
1605+
if altair_prop := getattr(vegalite, property_name, None):
16081606
# Add the docstring from the helper class (e.g. `BinParams`) so
16091607
# that all the parameter names of the helper class are included in
16101608
# the final docstring

tools/schemapi/schemapi.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
import narwhals.stable.v1 as nw
3434
from packaging.version import Version
3535

36-
# This leads to circular imports with the vegalite module. Currently, this works
37-
# but be aware that when you access it in this script, the vegalite module might
38-
# not yet be fully instantiated in case your code is being executed during import time
39-
from altair import vegalite
40-
4136
if sys.version_info >= (3, 12):
4237
from typing import Protocol, TypeAliasType, runtime_checkable
4338
else:
@@ -707,6 +702,8 @@ def _get_altair_class_for_error(
707702
708703
This should lead to more informative error messages pointing the user closer to the source of the issue.
709704
"""
705+
from altair import vegalite
706+
710707
for prop_name in reversed(error.absolute_path):
711708
# Check if str as e.g. first item can be a 0
712709
if isinstance(prop_name, str):
@@ -1595,14 +1592,15 @@ def __init__(self, prop: str, schema: dict[str, Any]) -> None:
15951592
self.schema = schema
15961593

15971594
def __get__(self, obj, cls):
1595+
from altair import vegalite
1596+
15981597
self.obj = obj
15991598
self.cls = cls
16001599
# The docs from the encoding class parameter (e.g. `bin` in X, Color,
16011600
# etc); this provides a general description of the parameter.
16021601
self.__doc__ = self.schema["description"].replace("__", "**")
16031602
property_name = f"{self.prop}"[0].upper() + f"{self.prop}"[1:]
1604-
if hasattr(vegalite, property_name):
1605-
altair_prop = getattr(vegalite, property_name)
1603+
if altair_prop := getattr(vegalite, property_name, None):
16061604
# Add the docstring from the helper class (e.g. `BinParams`) so
16071605
# that all the parameter names of the helper class are included in
16081606
# the final docstring

0 commit comments

Comments
 (0)