From 2ced395288e0404fe6c24b4dcfaab410bd3352cf Mon Sep 17 00:00:00 2001 From: J van Zundert Date: Tue, 21 Mar 2023 19:39:50 +0000 Subject: [PATCH] refactor(python): Reduce number of polars.internals imports (#7628) --- py-polars/polars/dataframe/_html.py | 2 +- py-polars/polars/sql/context.py | 4 ++-- py-polars/polars/testing/_parametric.py | 12 +++++------- py-polars/polars/testing/asserts.py | 19 ++++++++----------- py-polars/polars/utils/various.py | 16 ---------------- 5 files changed, 16 insertions(+), 37 deletions(-) diff --git a/py-polars/polars/dataframe/_html.py b/py-polars/polars/dataframe/_html.py index a630d1328e59..dbf369b944e7 100644 --- a/py-polars/polars/dataframe/_html.py +++ b/py-polars/polars/dataframe/_html.py @@ -10,7 +10,7 @@ if TYPE_CHECKING: from types import TracebackType - from polars.internals import DataFrame + from polars.dataframe import DataFrame class Tag: diff --git a/py-polars/polars/sql/context.py b/py-polars/polars/sql/context.py index 1f4605ffab9f..d4e39e565909 100644 --- a/py-polars/polars/sql/context.py +++ b/py-polars/polars/sql/context.py @@ -3,7 +3,7 @@ import contextlib from typing import TYPE_CHECKING -from polars import internals as pli +from polars.lazyframe import wrap_ldf with contextlib.suppress(ImportError): # Module not available when building docs from polars.polars import PySQLContext @@ -51,7 +51,7 @@ def execute(self, query: str) -> LazyFrame: A SQL query """ - return pli.wrap_ldf(self._ctxt.execute(query)) + return wrap_ldf(self._ctxt.execute(query)) def query(self, query: str) -> DataFrame: return self.execute(query).collect() diff --git a/py-polars/polars/testing/_parametric.py b/py-polars/polars/testing/_parametric.py index 239ca8aa0aa6..12e26c1fad54 100644 --- a/py-polars/polars/testing/_parametric.py +++ b/py-polars/polars/testing/_parametric.py @@ -26,7 +26,7 @@ ) from hypothesis.strategies._internal.utils import defines_strategy -from polars import internals as pli +from polars.dataframe import DataFrame from polars.datatypes import ( Boolean, Categorical, @@ -48,18 +48,16 @@ is_polars_dtype, py_type_to_dtype, ) +from polars.series.series import Series from polars.string_cache import StringCache from polars.testing.asserts import is_categorical_dtype if TYPE_CHECKING: from hypothesis.strategies import DrawFn, SearchStrategy - from polars.dataframe.frame import DataFrame - from polars.lazyframe.frame import LazyFrame - from polars.series.series import Series + from polars.lazyframe import LazyFrame from polars.type_aliases import OneOrMoreDataTypes, PolarsDataType - # Default profile (eg: running locally) common_settings = {"print_blob": True, "deadline": None} settings.register_profile( @@ -440,7 +438,7 @@ def draw_series(draw: DrawFn) -> Series: series_values[idx] = None # init series with strategy-generated data - s = pli.Series( + s = Series( name=series_name, dtype=series_dtype, values=series_values, @@ -614,7 +612,7 @@ def draw_frames(draw: DrawFn) -> DataFrame | LazyFrame: frame_columns = [ c.name if (c.dtype is None) else (c.name, c.dtype) for c in coldefs ] - df = pli.DataFrame( + df = DataFrame( data={ c.name: draw( series( diff --git a/py-polars/polars/testing/asserts.py b/py-polars/polars/testing/asserts.py index 8ef08f8f7866..ebabaf983292 100644 --- a/py-polars/polars/testing/asserts.py +++ b/py-polars/polars/testing/asserts.py @@ -1,10 +1,10 @@ from __future__ import annotations import textwrap -from typing import TYPE_CHECKING, Any +from typing import Any from polars import functions as F -from polars import internals as pli +from polars.dataframe import DataFrame from polars.datatypes import ( Boolean, Categorical, @@ -14,13 +14,10 @@ dtype_to_py_type, ) from polars.exceptions import ComputeError, InvalidAssert +from polars.lazyframe import LazyFrame +from polars.series import Series from polars.utils.decorators import deprecate_nonkeyword_arguments, deprecated_alias -if TYPE_CHECKING: - from polars.dataframe.frame import DataFrame - from polars.lazyframe.frame import LazyFrame - from polars.series.series import Series - @deprecate_nonkeyword_arguments() @deprecated_alias(check_column_names="check_column_order") @@ -71,10 +68,10 @@ def assert_frame_equal( >>> assert_frame_equal(df1, df2) # doctest: +SKIP AssertionError: Values for column 'a' are different. """ - if isinstance(left, pli.LazyFrame) and isinstance(right, pli.LazyFrame): + if isinstance(left, LazyFrame) and isinstance(right, LazyFrame): left, right = left.collect(), right.collect() obj = "LazyFrames" - elif isinstance(left, pli.DataFrame) and isinstance(right, pli.DataFrame): + elif isinstance(left, DataFrame) and isinstance(right, DataFrame): obj = "DataFrames" else: raise_assert_detail("Inputs", "Unexpected input types", type(left), type(right)) @@ -233,8 +230,8 @@ def assert_series_equal( """ if not ( - isinstance(left, pli.Series) # type: ignore[redundant-expr] - and isinstance(right, pli.Series) + isinstance(left, Series) # type: ignore[redundant-expr] + and isinstance(right, Series) ): raise_assert_detail("Inputs", "Unexpected input types", type(left), type(right)) diff --git a/py-polars/polars/utils/various.py b/py-polars/polars/utils/various.py index b1e8d7f810f3..cb9dfa669f81 100644 --- a/py-polars/polars/utils/various.py +++ b/py-polars/polars/utils/various.py @@ -1,6 +1,5 @@ from __future__ import annotations -import contextlib import os import re import sys @@ -8,14 +7,9 @@ from typing import TYPE_CHECKING, Any, Generator, Iterable, Sequence, TypeVar from polars import functions as F -from polars import internals as pli from polars.datatypes import Int64, is_polars_dtype -with contextlib.suppress(ImportError): # Module not available when building docs - from polars.polars import PyExpr - if TYPE_CHECKING: - from polars.expr.expr import Expr from polars.series.series import Series @@ -76,16 +70,6 @@ def is_int_sequence(val: object) -> TypeGuard[Sequence[int]]: return isinstance(val, Sequence) and _is_iterable_of(val, int) -def is_expr_sequence(val: object) -> TypeGuard[Sequence[Expr]]: - """Check whether the given object is a sequence of Exprs.""" - return isinstance(val, Sequence) and _is_iterable_of(val, pli.Expr) - - -def is_pyexpr_sequence(val: object) -> TypeGuard[Sequence[PyExpr]]: - """Check whether the given object is a sequence of PyExprs.""" - return isinstance(val, Sequence) and _is_iterable_of(val, PyExpr) - - def is_str_sequence( val: object, *, allow_str: bool = False ) -> TypeGuard[Sequence[str]]: