Skip to content

Commit 42e8308

Browse files
committed
fix(selectors): resolve typing and coverage CI failures on __xor__
1 parent 2928b1b commit 42e8308

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

narwhals/_compliant/selectors.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def names(df: FrameT) -> Sequence[str]:
310310
return self.selectors._selector.from_callables(series, names, context=self)
311311
return self._to_expr() & other
312312

313-
@overload # type: ignore[override]
313+
@overload
314314
def __xor__(self, other: Self) -> Self: ...
315315
@overload
316316
def __xor__(
@@ -344,10 +344,10 @@ def names(df: FrameT) -> Sequence[str]:
344344
]
345345

346346
return self.selectors._selector.from_callables(series, names, context=self)
347-
msg = (
348-
f"unsupported operand type(s) for op: ('Selector' ^ '{type(other).__name__}')"
349-
)
350-
raise TypeError(msg)
347+
# The narwhals-level Selector.__xor__ rejects non-selectors before reaching
348+
# here, so this branch is a defensive guard only.
349+
msg = f"unsupported operand type(s) for op: ('Selector' ^ '{type(other).__name__}')" # pragma: no cover
350+
raise TypeError(msg) # pragma: no cover
351351

352352
def __invert__(self) -> CompliantSelector[FrameT, SeriesOrExprT]:
353353
return self.selectors.all() - self

narwhals/_polars/expr.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,15 @@ def __or__(self, other: PolarsExpr) -> Self:
273273
return self._with_native(self.native.__or__(extract_native(other)))
274274

275275
def __xor__(self, other: PolarsExpr) -> Self:
276-
if self._backend_version >= (1,):
277-
return self._with_native(self.native.__xor__(extract_native(other)))
278-
# Polars added Selector.__xor__ in 1.0.0; emulate via existing set ops.
279276
other_native = extract_native(other)
280-
return self._with_native(
281-
self.native.__or__(other_native).__sub__(self.native.__and__(other_native))
282-
)
277+
if self._backend_version < (1,): # pragma: no cover
278+
# Polars added Selector.__xor__ in 1.0.0; emulate via existing set ops.
279+
return self._with_native(
280+
self.native.__or__(other_native).__sub__(
281+
self.native.__and__(other_native)
282+
)
283+
)
284+
return self._with_native(self.native.__xor__(other_native))
283285

284286
def __add__(self, other: Any) -> Self:
285287
return self._with_native(self.native.__add__(extract_native(other)))

narwhals/selectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __and__(self, other: Any) -> Expr: # type: ignore[override]
5656
ExprNode(ExprKind.ELEMENTWISE, "__and__", exprs=(other,), str_as_lit=True)
5757
)
5858

59-
def __xor__(self, other: Any) -> Expr: # type: ignore[override]
59+
def __xor__(self, other: Any) -> Expr:
6060
if isinstance(other, Selector):
6161
return self._append_node(
6262
ExprNode(

0 commit comments

Comments
 (0)