|
8 | 8 | from operator import attrgetter
|
9 | 9 | from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar, cast, overload
|
10 | 10 |
|
| 11 | +from narwhals._plan._guards import is_function_expr, is_iterable_reject, is_literal |
11 | 12 | from narwhals._plan._immutable import Immutable
|
12 | 13 | from narwhals._plan.options import ExprIROptions, FEOptions, FunctionOptions
|
13 | 14 | from narwhals._plan.typing import (
|
|
19 | 20 | IRNamespaceT,
|
20 | 21 | MapIR,
|
21 | 22 | NamedOrExprIRT,
|
22 |
| - NativeSeriesT, |
23 | 23 | NonNestedDTypeT,
|
24 | 24 | Seq,
|
25 | 25 | )
|
26 |
| -from narwhals._utils import _hasattr_static |
27 | 26 | from narwhals.dtypes import DType
|
28 | 27 | from narwhals.utils import Version
|
29 | 28 |
|
30 | 29 | if TYPE_CHECKING:
|
31 | 30 | from collections.abc import Iterator
|
32 | 31 | from typing import Any, Callable
|
33 | 32 |
|
34 |
| - from typing_extensions import Self, TypeAlias, TypeIs |
35 |
| - |
36 |
| - from narwhals._plan import expr |
37 |
| - from narwhals._plan.dummy import Expr, Selector, Series |
38 |
| - from narwhals._plan.expr import ( |
39 |
| - AggExpr, |
40 |
| - Alias, |
41 |
| - BinaryExpr, |
42 |
| - Cast, |
43 |
| - Column, |
44 |
| - FunctionExpr, |
45 |
| - WindowExpr, |
46 |
| - ) |
| 33 | + from typing_extensions import Self, TypeAlias |
| 34 | + |
| 35 | + from narwhals._plan.dummy import Expr, Selector |
| 36 | + from narwhals._plan.expr import Alias, Cast, Column, FunctionExpr |
47 | 37 | from narwhals._plan.meta import IRMetaNamespace
|
48 |
| - from narwhals._plan.protocols import CompliantSeries, Ctx, FrameT_contra, R_co |
| 38 | + from narwhals._plan.protocols import Ctx, FrameT_contra, R_co |
49 | 39 | from narwhals.typing import NonNestedDType, NonNestedLiteral
|
50 | 40 |
|
51 | 41 |
|
@@ -453,97 +443,6 @@ class HorizontalFunction(
|
453 | 443 | ): ...
|
454 | 444 |
|
455 | 445 |
|
456 |
| -_NON_NESTED_LITERAL_TPS = ( |
457 |
| - int, |
458 |
| - float, |
459 |
| - str, |
460 |
| - dt.date, |
461 |
| - dt.time, |
462 |
| - dt.timedelta, |
463 |
| - bytes, |
464 |
| - Decimal, |
465 |
| -) |
466 |
| - |
467 |
| - |
468 |
| -def is_non_nested_literal(obj: Any) -> TypeIs[NonNestedLiteral]: |
469 |
| - return obj is None or isinstance(obj, _NON_NESTED_LITERAL_TPS) |
470 |
| - |
471 |
| - |
472 |
| -def is_expr(obj: Any) -> TypeIs[Expr]: |
473 |
| - from narwhals._plan.dummy import Expr |
474 |
| - |
475 |
| - return isinstance(obj, Expr) |
476 |
| - |
477 |
| - |
478 |
| -def is_column(obj: Any) -> TypeIs[Expr]: |
479 |
| - """Indicate if the given object is a basic/unaliased column. |
480 |
| -
|
481 |
| - https://github.com/pola-rs/polars/blob/a3d6a3a7863b4d42e720a05df69ff6b6f5fc551f/py-polars/polars/_utils/various.py#L164-L168. |
482 |
| - """ |
483 |
| - return is_expr(obj) and obj.meta.is_column() |
484 |
| - |
485 |
| - |
486 |
| -def is_series(obj: Series[NativeSeriesT] | Any) -> TypeIs[Series[NativeSeriesT]]: |
487 |
| - from narwhals._plan.dummy import Series |
488 |
| - |
489 |
| - return isinstance(obj, Series) |
490 |
| - |
491 |
| - |
492 |
| -def is_compliant_series( |
493 |
| - obj: CompliantSeries[NativeSeriesT] | Any, |
494 |
| -) -> TypeIs[CompliantSeries[NativeSeriesT]]: |
495 |
| - return _hasattr_static(obj, "__narwhals_series__") |
496 |
| - |
497 |
| - |
498 |
| -def is_iterable_reject(obj: Any) -> TypeIs[str | bytes | Series | CompliantSeries]: |
499 |
| - from narwhals._plan.dummy import Series |
500 |
| - |
501 |
| - return isinstance(obj, (str, bytes, Series)) or is_compliant_series(obj) |
502 |
| - |
503 |
| - |
504 |
| -def is_window_expr(obj: Any) -> TypeIs[WindowExpr]: |
505 |
| - from narwhals._plan.expr import WindowExpr |
506 |
| - |
507 |
| - return isinstance(obj, WindowExpr) |
508 |
| - |
509 |
| - |
510 |
| -def is_function_expr(obj: Any) -> TypeIs[FunctionExpr[Any]]: |
511 |
| - from narwhals._plan.expr import FunctionExpr |
512 |
| - |
513 |
| - return isinstance(obj, FunctionExpr) |
514 |
| - |
515 |
| - |
516 |
| -def is_binary_expr(obj: Any) -> TypeIs[BinaryExpr]: |
517 |
| - from narwhals._plan.expr import BinaryExpr |
518 |
| - |
519 |
| - return isinstance(obj, BinaryExpr) |
520 |
| - |
521 |
| - |
522 |
| -def is_agg_expr(obj: Any) -> TypeIs[AggExpr]: |
523 |
| - from narwhals._plan.expr import AggExpr |
524 |
| - |
525 |
| - return isinstance(obj, AggExpr) |
526 |
| - |
527 |
| - |
528 |
| -def is_aggregation(obj: Any) -> TypeIs[AggExpr | FunctionExpr[Any]]: |
529 |
| - """Superset of `ExprIR.is_scalar`, excludes literals & len.""" |
530 |
| - return is_agg_expr(obj) or (is_function_expr(obj) and obj.is_scalar) |
531 |
| - |
532 |
| - |
533 |
| -def is_literal(obj: Any) -> TypeIs[expr.Literal[Any]]: |
534 |
| - from narwhals._plan import expr |
535 |
| - |
536 |
| - return isinstance(obj, expr.Literal) |
537 |
| - |
538 |
| - |
539 |
| -def is_horizontal_reduction(obj: FunctionExpr[Any] | Any) -> TypeIs[FunctionExpr[Any]]: |
540 |
| - return is_function_expr(obj) and obj.options.is_input_wildcard_expansion() |
541 |
| - |
542 |
| - |
543 |
| -def is_tuple_of(obj: Any, tp: type[T]) -> TypeIs[Seq[T]]: |
544 |
| - return bool(isinstance(obj, tuple) and obj and isinstance(obj[0], tp)) |
545 |
| - |
546 |
| - |
547 | 446 | def py_to_narwhals_dtype(obj: NonNestedLiteral, version: Version = Version.MAIN) -> DType:
|
548 | 447 | dtypes = version.dtypes
|
549 | 448 | mapping: dict[type[NonNestedLiteral], type[NonNestedDType]] = {
|
|
0 commit comments