11from __future__ import annotations
22
33import re
4+ import sys
45from typing import TYPE_CHECKING
56from typing import Any
67from typing import ClassVar
78from typing import Iterator
89from typing import Mapping
9- from typing import Protocol
1010from typing import Sequence
1111
1212from narwhals ._compliant .typing import CompliantDataFrameT
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
7888class 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