Skip to content

Commit d1a9326

Browse files
committed
chore: revise imports and import order to account for circular imports
1 parent ed7b491 commit d1a9326

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/bigframes/bigframes/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@
2424
message=".*Google will stop supporting.*Python.*",
2525
)
2626

27+
# import configuration and types.
28+
# This ensures that when the deeper 'core' modules ask for 'dtypes','options', et. al.,
29+
# they are already defined and available.
30+
import bigframes.dtypes # noqa: E402 # isort: skip
31+
import bigframes._config # noqa: E402 # isort: skip
32+
from bigframes._config import option_context, options # noqa: E402 # isort: skip
33+
2734
import bigframes.enums as enums # noqa: E402
2835
import bigframes.exceptions as exceptions # noqa: E402
2936

37+
# We import operations early to resolve a circular dependency between
38+
# bigframes.core.expression and bigframes.operations.
39+
# This ensures the 'Expression' base class is defined before 'Aggregation'
40+
# subclasses attempt to inherit from it.
41+
import bigframes.operations # noqa: E402 # isort: skip
42+
3043
# Register pandas extensions
3144
import bigframes.extensions.pandas.dataframe_accessor # noqa: F401, E402
32-
from bigframes._config import option_context, options # noqa: E402
3345
from bigframes._config.bigquery_options import BigQueryOptions # noqa: E402
3446
from bigframes.core.global_session import ( # noqa: E402
3547
close_session,

packages/bigframes/bigframes/core/expression.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
from bigframes import dtypes
2929
from bigframes.core import field
3030

31+
if typing.TYPE_CHECKING:
32+
import bigframes.operations
33+
3134

3235
def const(
3336
value: typing.Hashable, dtype: dtypes.ExpressionType = None

packages/bigframes/bigframes/dtypes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import pyarrow as pa
3232
import shapely.geometry # type: ignore
3333

34-
import bigframes.core.backports
3534
import bigframes.exceptions
3635

3736
# Type hints for Pandas dtypes supported by BigQuery DataFrame
@@ -397,6 +396,9 @@ def get_struct_fields(type_: ExpressionType) -> dict[str, Dtype]:
397396
assert isinstance(type_.pyarrow_dtype, pa.StructType)
398397
struct_type = type_.pyarrow_dtype
399398
result: dict[str, Dtype] = {}
399+
400+
# Local import to break circular dependency with core.backports
401+
import bigframes.core.backports
400402
for field in bigframes.core.backports.pyarrow_struct_type_fields(struct_type):
401403
result[field.name] = arrow_dtype_to_bigframes_dtype(field.type)
402404
return result
@@ -582,6 +584,9 @@ def to_storage_type(
582584
return pa.list_(to_storage_type(arrow_type.value_type))
583585
if pa.types.is_struct(arrow_type):
584586
assert isinstance(arrow_type, pa.StructType)
587+
588+
# Local import to break circular dependency with core.backports
589+
import bigframes.core.backports
585590
return pa.struct(
586591
field.with_type(to_storage_type(field.type))
587592
for field in bigframes.core.backports.pyarrow_struct_type_fields(arrow_type)
@@ -595,6 +600,9 @@ def arrow_type_to_literal(
595600
"""Create a representative literal value for an arrow type."""
596601
if pa.types.is_list(arrow_type):
597602
return [arrow_type_to_literal(arrow_type.value_type)]
603+
604+
# Local import to break circular dependency with core.backports
605+
import bigframes.core.backports
598606
if pa.types.is_struct(arrow_type):
599607
return {
600608
field.name: arrow_type_to_literal(field.type)
@@ -1001,6 +1009,8 @@ def contains_db_dtypes_json_arrow_type(type_):
10011009
return contains_db_dtypes_json_arrow_type(type_.value_type)
10021010

10031011
if isinstance(type_, pa.StructType):
1012+
# Local import to break circular dependency with core.backports
1013+
import bigframes.core.backports
10041014
return any(
10051015
contains_db_dtypes_json_arrow_type(field.type)
10061016
for field in bigframes.core.backports.pyarrow_struct_type_fields(type_)

0 commit comments

Comments
 (0)