-
-
Notifications
You must be signed in to change notification settings - Fork 141
BUG: Overloads for accessing columns of a DataFrame are too restrictive #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ignoring non-list sequences: I think it might be sufficient to replace Ideally, we could simply use |
I'm a bit surprised that a type of |
I don't think NumPy arrays are hashable. >>> from typing import Hashable
>>> import numpy as np
>>> isinstance(np.array([1,2]),Hashable)
False
>>> hash(np.array([1,2]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 hash(np.array([1,2]))
TypeError: unhashable type: 'numpy.ndarray' I'm also pretty sure that the individual column type is There are many references in _typing.py to |
From a dynamic typing perspective, they are not. But they might be from the perspective of static typing. I'm not sure. There was a point in time (just last week) where I tried to change one of our arguments in an overloaded signature from something like from typing import Hashable, List, TypeVar, Union
import numpy as np
tt: List[Union[int, str]]
tt = [1]
def func(a: List[Union[int, str, bool]]):
print(a)
func(tt)
HashableT = TypeVar("HashableT", bound=Hashable)
def func2(a: List[HashableT]):
print(a)
func2(tt)
nt = [np.array([1, 2, 3])]
func2(nt) With the above code, So my point here is that a list of |
I think this can only be a bug in pyright. If you look at NumPy there is no mention of |
I'm pretty sure this is due to the invariance of |
The type annotations of NumPy declare ndarray incorrectly as hashable, I opened a bug at numpy: numpy/numpy#21930 |
It's an issue with |
After numpy fixes their |
Yes, so the question is if we make any changes here. If you remove the line pandas-stubs/pandas-stubs/core/frame.pyi Line 366 in 451233b
df[[1, "abc"]] (which would be quite unusual anyway to have a mix of types in the column names)
Another idea to try is to change in from typing import Hashable, List, TypeVar, Union
import numpy as np
BigType = TypeVar("BigType", bound=Union[int, str, bool])
tt: List[Union[int, str]]
tt = [1]
def func(a: List[BigType]):
print(a) I may look at that later, or hoping someone else can beat me to it! |
I recall that the there were a few mypy related bugs to |
Pandas-stubs has almost the same issue as numpy (only difference is that the return type is somehow lost): import pandas as pd
pd.DataFrame().__hash__ is None # True
reveal_type(pd.DataFrame().__hash__)
|
Describe the bug
Current overloads for accessing columns of a DataFrame are too restrictive
To Reproduce
returns
mypy 0.961
mypy-extensions 0.4.3
pandas 1.4.2
pandas-stubs 1.4.3.220704
The text was updated successfully, but these errors were encountered: