Skip to content

Commit 603c4e9

Browse files
committed
chore(typing): Add _typing_compat.py
- Mentioned in (#2391 (comment)) - Needed again for #2572
1 parent 808eb1c commit 603c4e9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

narwhals/_typing_compat.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import annotations
2+
3+
# ruff: noqa: ARG001, ANN202, N802
4+
import sys
5+
from typing import TYPE_CHECKING
6+
from typing import Any
7+
8+
if TYPE_CHECKING:
9+
from typing import Callable
10+
from typing import Protocol as Protocol38
11+
12+
if sys.version_info >= (3, 13):
13+
from typing import TypeVar
14+
from warnings import deprecated
15+
else:
16+
from typing_extensions import TypeVar
17+
from typing_extensions import deprecated
18+
19+
_Fn = TypeVar("_Fn", bound=Callable[..., Any])
20+
21+
22+
else: # pragma: no cover
23+
if sys.version_info >= (3, 13):
24+
from typing import TypeVar
25+
from warnings import deprecated
26+
else:
27+
from typing import TypeVar as _TypeVar
28+
29+
def TypeVar(
30+
name: str,
31+
*constraints: Any,
32+
bound: Any | None = None,
33+
covariant: bool = False,
34+
contravariant: bool = False,
35+
**kwds: Any,
36+
):
37+
return _TypeVar(
38+
name,
39+
*constraints,
40+
bound=bound,
41+
covariant=covariant,
42+
contravariant=contravariant,
43+
)
44+
45+
def deprecated(message: str, /) -> Callable[[_Fn], _Fn]:
46+
def wrapper(func: _Fn, /) -> _Fn:
47+
return func
48+
49+
return wrapper
50+
51+
# TODO @dangotbanned: Remove after dropping `3.8` (#2084)
52+
# - https://github.com/narwhals-dev/narwhals/pull/2064#discussion_r1965921386
53+
if sys.version_info >= (3, 9):
54+
from typing import Protocol as Protocol38
55+
else:
56+
from typing import Generic as Protocol38
57+
58+
59+
__all__ = ["Protocol38", "TypeVar", "deprecated"]

0 commit comments

Comments
 (0)