Conversation
tests/series/test_series.py
Outdated
| """Test that StringMethods are not iterable.""" | ||
| s_str = pd.Series(["a", "b"]) | ||
| if TYPE_CHECKING_INVALID_USAGE: | ||
| assert_type(s_str.str.__iter__(), Never) |
There was a problem hiding this comment.
| assert_type(s_str.str.__iter__(), Never) | |
| assert_type(iter(s_str.str), Never) |
There was a problem hiding this comment.
For some reason that resolves to Any and not Never, curious if you have insights
There was a problem hiding this comment.
Here is a minimal script:
from typing import Any, Generic, Never, TypeVar, reveal_type
class Series:
@property
def str(self) -> StringMethods[Any, Series]: ...
_T_EXPANDING = TypeVar("_T_EXPANDING")
_T_STR = TypeVar("_T_STR", bound=Series)
class StringMethods(Generic[_T_EXPANDING, _T_STR]):
def __getitem__(self, key: int) -> _T_STR: ...
def __iter__(self) -> Never: ...
def _0() -> None:
reveal_type(StringMethods().__iter__()) # all 4 type checkers give Never
def _1() -> None:
reveal_type(iter(Series().str)) # mypy: Any, pyright, pyrefly: Never, ty: UnknownI think it's worth reporting, but I don't have time right now. @Dr-Irv do you have further insights? I think the real use case is the proposed new version.
There was a problem hiding this comment.
Opened astral-sh/ty#3041 to get some insights on the ty case, based on their response that may give us a hint for why mypy has issues.
There was a problem hiding this comment.
@loicdiridollou I think you can open up one with mypy as well and see what they have to say.
There was a problem hiding this comment.
python/mypy#21027
The example that they reduced it to on the ty thread works with mypy though:
from typing import Never, Protocol, reveal_type
class ReturnsT[T](Protocol):
def get(self) -> T: ...
class ReturnsNever:
def get(self) -> Never:
raise RuntimeError
def through_protocol[T](x: ReturnsT[T]) -> T:
return x.get()
reveal_type(through_protocol(ReturnsNever()))with https://mypy-play.net/ but ty still struggles.
Takes care of 2.0 migration issues (left in #1579):