Skip to content

Commit

Permalink
refactor(python): Reduce number of polars.internals imports (#7628)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj authored Mar 21, 2023
1 parent efe48bc commit 2ced395
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 37 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/dataframe/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if TYPE_CHECKING:
from types import TracebackType

from polars.internals import DataFrame
from polars.dataframe import DataFrame


class Tag:
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/sql/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
12 changes: 5 additions & 7 deletions py-polars/polars/testing/_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 8 additions & 11 deletions py-polars/polars/testing/asserts.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))

Expand Down
16 changes: 0 additions & 16 deletions py-polars/polars/utils/various.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from __future__ import annotations

import contextlib
import os
import re
import sys
from collections.abc import MappingView, Sized
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


Expand Down Expand Up @@ -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]]:
Expand Down

0 comments on commit 2ced395

Please sign in to comment.