Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): Selectors should raise on + between themselves #20825

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading