Skip to content

Commit 16a9230

Browse files
committed
fix: 3.8 compat
Needed for `__init__` support
1 parent 5b3f3f5 commit 16a9230

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

narwhals/_compliant/group_by.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

33
import re
4+
import sys
45
from typing import TYPE_CHECKING
56
from typing import Any
67
from typing import ClassVar
78
from typing import Iterator
89
from typing import Mapping
9-
from typing import Protocol
1010
from typing import Sequence
1111

1212
from narwhals._compliant.typing import CompliantDataFrameT
@@ -23,13 +23,23 @@
2323

2424
Frame: TypeAlias = "CompliantDataFrame[Any, Any, NativeFrameT_co] | CompliantLazyFrame[Any, NativeFrameT_co]"
2525

26+
if not TYPE_CHECKING: # pragma: no cover
27+
if sys.version_info >= (3, 9):
28+
from typing import Protocol as Protocol38
29+
else:
30+
from typing import Generic as Protocol38
31+
else: # pragma: no cover
32+
# TODO @dangotbanned: Remove after dropping `3.8` (#2084)
33+
# - https://github.com/narwhals-dev/narwhals/pull/2064#discussion_r1965921386
34+
from typing import Protocol as Protocol38
35+
2636
__all__ = ["CompliantGroupBy", "EagerGroupBy"]
2737

2838

2939
# NOTE: Type checkers disagree
3040
# - `pyright` wants invariant `*Expr`
3141
# - `mypy` want contravariant `*Expr`
32-
class CompliantGroupBy(Protocol[CompliantFrameT, CompliantExprT]): # type: ignore[misc]
42+
class CompliantGroupBy(Protocol38[CompliantFrameT, CompliantExprT]): # type: ignore[misc]
3343
_NARWHALS_TO_NATIVE_AGGREGATIONS: ClassVar[Mapping[str, Any]]
3444
_compliant_frame: CompliantFrameT
3545
_keys: Sequence[str]
@@ -60,7 +70,7 @@ def _ensure_all_simple(self, exprs: Sequence[CompliantExprT]) -> None:
6070
and re.sub(r"(\w+->)", "", expr._function_name)
6171
in self._NARWHALS_TO_NATIVE_AGGREGATIONS
6272
):
63-
# NOTE: Need to define `_implementation` in both protocols
73+
# NOTE: Need to define `_implementation` in both protocols (#2251)
6474
name = self.compliant._implementation.name.lower() # type: ignore # noqa: PGH003
6575
msg = (
6676
f"Non-trivial complex aggregation found.\n\n"
@@ -77,6 +87,6 @@ def _ensure_all_simple(self, exprs: Sequence[CompliantExprT]) -> None:
7787

7888
class EagerGroupBy( # type: ignore[misc]
7989
CompliantGroupBy[CompliantDataFrameT, CompliantExprT],
80-
Protocol[CompliantDataFrameT, CompliantExprT],
90+
Protocol38[CompliantDataFrameT, CompliantExprT],
8191
):
8292
def __iter__(self) -> Iterator[tuple[Any, CompliantDataFrameT]]: ...

0 commit comments

Comments
 (0)