chore: Reuse SeriesT alias more, make more types TYPE_CHECKING-only#2545
chore: Reuse SeriesT alias more, make more types TYPE_CHECKING-only#2545MarcoGorelli merged 1 commit intonarwhals-dev:mainfrom
Conversation
|
@MarcoGorelli some notes, not sure if you're aware of these:
IMO, I think what you're trying to do here is already covered by omitting these types from |
|
If you wanna have some super-secret typing - another option is defining them at runtime in a private module, but importing into |
|
thanks for your review - i checked the api reference and all looks good 👍 if we need to use
could you elaborate on this please? |
|
i'll take some of these out and leave them for a separate discussion |
7c4469b to
357a8bc
Compare
Thanks @MarcoGorelli 🎉
So I think something like this works across all versions of python
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import Generic
from typing import TypeVar
if TYPE_CHECKING:
from typing_extensions import Self
# Where these are runtime objects in `typing`
from narwhals.typing import _1DArray
from narwhals.typing import _NumpyScalar
NumpyT = TypeVar("NumpyT", "_1DArray", "_NumpyScalar")
class Some(Generic[NumpyT]):
def __init__(self) -> None:
super().__init__()
def meth(self) -> Self:
return self
class Another(Some["_1DArray"]): ...But changing that to this, caused me some issues in earlier versions
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import Generic
from typing import TypeVar
if TYPE_CHECKING:
import numpy as np
from typing_extensions import Self
_1DArray: TypeAlias = "np.ndarray[tuple[int], Any]" # noqa: PYI042
_NumpyScalar: TypeAlias = "np.generic[Any]"
NumpyT = TypeVar("NumpyT", "_1DArray", "_NumpyScalar")
class Some(Generic[NumpyT]):
def __init__(self) -> None:
super().__init__()
def meth(self) -> Self:
return self
class Another(Some["_1DArray"]): ...Seems to be working fine on I've just gone back over (#2064) and each of (https://github.com/narwhals-dev/narwhals/commits/main/narwhals/_translate.py) and I can't find the issue that popped up in CI. EditFound it! This is what you can't do from __future__ import annotations
from typing import TYPE_CHECKING
from typing import Generic
from typing import TypeVar
if TYPE_CHECKING:
import numpy as np
_1DArray: TypeAlias = "np.ndarray[tuple[int], Any]" # noqa: PYI042
_NumpyScalar: TypeAlias = "np.generic[Any]"
NumpyT = TypeVar("NumpyT", "_1DArray", "_NumpyScalar")
class Some(Generic["NumpyT"]):
def __init__(self) -> None:
super().__init__()At runtime it causes: TypeError Traceback (most recent call last)
Cell In[2], line 15
10 _NumpyScalar: TypeAlias = "np.generic[Any]"
12 NumpyT = TypeVar("NumpyT", "_1DArray", "_NumpyScalar")
---> 15 class Some(Generic["NumpyT"]):
16 def __init__(self) -> None:
17 super().__init__()
File /Lib\/typing.py:432, in _tp_cache.<locals>.decorator.<locals>.inner(*args, **kwds)
430 except TypeError:
431 pass # All real errors (not unhashable args) are raised below.
--> 432 return func(*args, **kwds)
File /Lib/typing.py:1231, in _generic_class_getitem(cls, args)
1227 raise TypeError(
1228 f"Parameter list to {cls.__qualname__}[...] cannot be empty"
1229 )
1230 if not all(_is_typevar_like(p) for p in args):
-> 1231 raise TypeError(
1232 f"Parameters to {cls.__name__}[...] must all be type variables "
1233 f"or parameter specification variables.")
1234 if len(set(args)) != len(args):
1235 raise TypeError(
1236 f"Parameters to {cls.__name__}[...] must all be unique")
TypeError: Parameters to Generic[...] must all be type variables or parameter specification variables. |
dangotbanned
left a comment
There was a problem hiding this comment.
Thanks @MarcoGorelli, all looking good in VSCode now 😍
|
thanks for explaining 🙏 cheers @dangotbanned ! |
What type of PR is this? (check all applicable)
Related issues
Checklist
If you have comments or can explain your changes, please do so below