Skip to content

Commit

Permalink
fix(python): Selectors should raise on + between themselves (#20825)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie authored Jan 21, 2025
1 parent 9e129cd commit e567c79
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
24 changes: 13 additions & 11 deletions py-polars/polars/io/cloud/credential_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,19 @@ def _check_module_availability(cls) -> None:

def _maybe_init_credential_provider(
credential_provider: CredentialProviderFunction | Literal["auto"] | None,
source: str
| Path
| IO[str]
| IO[bytes]
| bytes
| list[str]
| list[Path]
| list[IO[str]]
| list[IO[bytes]]
| list[bytes]
| None,
source: (
str
| Path
| IO[str]
| IO[bytes]
| bytes
| list[str]
| list[Path]
| list[IO[str]]
| list[IO[bytes]]
| list[bytes]
| None
),
storage_options: dict[str, Any] | None,
caller_name: str,
) -> CredentialProviderFunction | CredentialProvider | None:
Expand Down
39 changes: 28 additions & 11 deletions py-polars/polars/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,23 +358,20 @@ def __repr__(self) -> str:
return f"cs.{selector_name}({str_params})"

@overload
def __sub__(self, other: SelectorType) -> SelectorType: ...
def __add__(self, other: SelectorType) -> SelectorType: ...

@overload
def __sub__(self, other: Any) -> SelectorType | Expr: ...
def __add__(self, other: Any) -> Expr: ...

def __sub__(self, other: Any) -> Expr:
def __add__(self, other: Any) -> SelectorType | Expr:
if is_selector(other):
return _selector_proxy_(
self.meta._as_selector().meta._selector_sub(other),
parameters={"self": self, "other": other},
name="sub",
)
msg = "unsupported operand type(s) for op: ('Selector' + 'Selector')"
raise TypeError(msg)
else:
return self.as_expr().__sub__(other)
return self.as_expr().__add__(other)

def __rsub__(self, other: Any) -> NoReturn:
msg = "unsupported operand type(s) for op: ('Expr' - 'Selector')"
def __radd__(self, other: Any) -> Expr:
msg = "unsupported operand type(s) for op: ('Expr' + 'Selector')"
raise TypeError(msg)

@overload
Expand Down Expand Up @@ -425,6 +422,26 @@ def __ror__(self, other: Any) -> Expr:
other = by_name(other.meta.output_name())
return self.as_expr().__ror__(other)

@overload
def __sub__(self, other: SelectorType) -> SelectorType: ...

@overload
def __sub__(self, other: Any) -> SelectorType | Expr: ...

def __sub__(self, other: Any) -> Expr:
if is_selector(other):
return _selector_proxy_(
self.meta._as_selector().meta._selector_sub(other),
parameters={"self": self, "other": other},
name="sub",
)
else:
return self.as_expr().__sub__(other)

def __rsub__(self, other: Any) -> NoReturn:
msg = "unsupported operand type(s) for op: ('Expr' - 'Selector')"
raise TypeError(msg)

@overload
def __xor__(self, other: SelectorType) -> SelectorType: ...

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,12 @@ def test_selector_sets(df: pl.DataFrame) -> None:
with pytest.raises(TypeError, match=r"unsupported .* \('Expr' - 'Selector'\)"):
df.select(pl.col("colx") - cs.matches("[yz]$"))

with pytest.raises(TypeError, match=r"unsupported .* \('Expr' \+ 'Selector'\)"):
df.select(pl.col("colx") + cs.numeric())

with pytest.raises(TypeError, match=r"unsupported .* \('Selector' \+ 'Selector'\)"):
df.select(cs.string() + cs.numeric())

# complement
assert df.select(~cs.by_dtype([pl.Duration, pl.Time])).schema == {
"abc": pl.UInt16,
Expand Down

0 comments on commit e567c79

Please sign in to comment.