You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to emulate some Pandas type behavior, where Index.__new__ can return different types. The simplified case I am trying to model is that sequences of np.datetime64 and datetime.datetime get turned into one class, and other objects get turned into another class.
The following is the only way I've gotten it to work, after trying many many variations:
As expected, mypy only complains about the last two lines:
$ mypy test.py
test.py:59: error: List item 0 has incompatible type "str"; expected "datetime64"
test.py:60: error: Incompatible types in assignment (expression has type "Datetime64Index", variable has type "IndexType[bool]")
test.py:60: note: Following member(s) of "Datetime64Index" have conflicts:
test.py:60: note: Expected:
test.py:60: note: def first(self) -> bool
test.py:60: note: Got:
test.py:60: note: def first(self) -> datetime
test.py:60: error: List item 0 has incompatible type "str"; expected "datetime64"
However, the need for DefaultIndex feels like a hack. What I would actually like to do is the following:
However, mypy gets confused and complains about a: IndexType[datetime] = Index([datetime64(100)]):
test.py:50: error: List item 0 has incompatible type "datetime64"; expected "datetime"
test.py:55: error: List item 0 has incompatible type "str"; expected "datetime"
test.py:56: error: List item 0 has incompatible type "str"; expected "bool"
Found 3 errors in 1 file (checked 1 source file)
Your Environment
Mypy version used: 0.782
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.7
Operating system and version: Linux
The text was updated successfully, but these errors were encountered:
It seems that there is interference between __init__ and __new__ and the type of Index([datetime64(100)]) is determined by looking at __init__, where it takes S to be datetime64.
Usually that's fine (normally __new__ and __init__ must match) but here it's not. :-(
Your working code works around the problem by separating __new__ and __init__ into different classes. I think that's the best you can do to model this API. It would seem that your toy example here could also be solved by making Index a function, but I presume there's something in the real API that precludes this.
Sorry, that's all I have -- actually fixing this would probably require some deep changes in mypy which I couldn't do myself.
Bug Report
I am trying to emulate some Pandas type behavior, where
Index.__new__
can return different types. The simplified case I am trying to model is that sequences ofnp.datetime64
anddatetime.datetime
get turned into one class, and other objects get turned into another class.The following is the only way I've gotten it to work, after trying many many variations:
As expected, mypy only complains about the last two lines:
However, the need for
DefaultIndex
feels like a hack. What I would actually like to do is the following:However, mypy gets confused and complains about
a: IndexType[datetime] = Index([datetime64(100)])
:Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: