Skip to content

Commit

Permalink
refactor: rename SupportSchema -> SchemaLike, fix type definition (#8427
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jcrist authored Feb 22, 2024
1 parent 21384c3 commit ad1f53a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions ibis/backends/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import ibis.expr.datatypes as dt
from ibis.backends.sql.compiler import SQLGlotCompiler
from ibis.common.typing import SupportsSchema
from ibis.expr.schema import SchemaLike


class SQLBackend(BaseBackend):
Expand Down Expand Up @@ -137,7 +137,7 @@ def _log(self, sql: str) -> None:
def sql(
self,
query: str,
schema: SupportsSchema | None = None,
schema: SchemaLike | None = None,
dialect: str | None = None,
) -> ir.Table:
query = self._transpile_sql(query, dialect=dialect)
Expand Down
11 changes: 0 additions & 11 deletions ibis/common/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,8 @@
from ibis.common.caching import memoize

if TYPE_CHECKING:
from collections.abc import Iterable, Mapping

from typing_extensions import Self

import ibis.expr.datatypes as dt
import ibis.expr.schema as sch

SupportsSchema = TypeVar(
"SupportsSchema",
sch.Schema,
Mapping[str, str | dt.DataType],
Iterable[tuple[str, str | dt.DataType]],
)

if sys.version_info >= (3, 10):
from types import UnionType
Expand Down
12 changes: 6 additions & 6 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import pandas as pd
import pyarrow as pa

from ibis.common.typing import SupportsSchema
from ibis.expr.schema import SchemaLike

__all__ = (
"aggregate",
Expand Down Expand Up @@ -266,7 +266,7 @@ def param(type: dt.DataType) -> ir.Scalar:


def schema(
pairs: SupportsSchema | None = None,
pairs: SchemaLike | None = None,
names: Iterable[str] | None = None,
types: Iterable[str | dt.DataType] | None = None,
) -> sch.Schema:
Expand Down Expand Up @@ -310,7 +310,7 @@ def schema(


def table(
schema: SupportsSchema | None = None,
schema: SchemaLike | None = None,
name: str | None = None,
) -> ir.Table:
"""Create a table literal or an abstract table without data.
Expand Down Expand Up @@ -352,7 +352,7 @@ def memtable(
data,
*,
columns: Iterable[str] | None = None,
schema: SupportsSchema | None = None,
schema: SchemaLike | None = None,
name: str | None = None,
) -> Table:
"""Construct an ibis table expression from in-memory data.
Expand Down Expand Up @@ -441,7 +441,7 @@ def _memtable(
data: pd.DataFrame | Any,
*,
columns: Iterable[str] | None = None,
schema: SupportsSchema | None = None,
schema: SchemaLike | None = None,
name: str | None = None,
) -> Table:
import pandas as pd
Expand Down Expand Up @@ -493,7 +493,7 @@ def _memtable_from_pyarrow_table(
data: pa.Table,
*,
name: str | None = None,
schema: SupportsSchema | None = None,
schema: SchemaLike | None = None,
columns: Iterable[str] | None = None,
):
from ibis.formats.pyarrow import PyArrowTableProxy
Expand Down
10 changes: 9 additions & 1 deletion ibis/expr/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable, Iterator, Mapping
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Union

import ibis.expr.datatypes as dt
from ibis.common.annotations import attribute
Expand All @@ -14,6 +14,7 @@

if TYPE_CHECKING:
import pandas as pd
from typing_extensions import TypeAlias


class Schema(Concrete, Coercible, MapSet):
Expand Down Expand Up @@ -220,6 +221,13 @@ def apply_to(self, df: pd.DataFrame) -> pd.DataFrame:
return PandasData.convert_table(df, self)


SchemaLike: TypeAlias = Union[
Schema,
Mapping[str, Union[str, dt.DataType]],
Iterable[tuple[str, Union[str, dt.DataType]]],
]


@lazy_singledispatch
def schema(value: Any) -> Schema:
"""Construct ibis schema from schema-like python objects."""
Expand Down
8 changes: 4 additions & 4 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

import ibis.expr.types as ir
import ibis.selectors as s
from ibis.common.typing import SupportsSchema
from ibis.expr.operations.relations import JoinKind
from ibis.expr.schema import SchemaLike
from ibis.expr.types import Table
from ibis.expr.types.groupby import GroupedTable
from ibis.expr.types.tvf import WindowedTable
Expand Down Expand Up @@ -361,7 +361,7 @@ def __contains__(self, name: str) -> bool:
"""
return name in self.schema()

def cast(self, schema: SupportsSchema) -> Table:
def cast(self, schema: SchemaLike) -> Table:
"""Cast the columns of a table.
Similar to `pandas.DataFrame.astype`.
Expand Down Expand Up @@ -438,7 +438,7 @@ def cast(self, schema: SupportsSchema) -> Table:
"""
return self._cast(schema, cast_method="cast")

def try_cast(self, schema: SupportsSchema) -> Table:
def try_cast(self, schema: SchemaLike) -> Table:
"""Cast the columns of a table.
If the cast fails for a row, the value is returned
Expand Down Expand Up @@ -472,7 +472,7 @@ def try_cast(self, schema: SupportsSchema) -> Table:
"""
return self._cast(schema, cast_method="try_cast")

def _cast(self, schema: SupportsSchema, cast_method: str = "cast") -> Table:
def _cast(self, schema: SchemaLike, cast_method: str = "cast") -> Table:
schema = sch.schema(schema)

cols = []
Expand Down

0 comments on commit ad1f53a

Please sign in to comment.