-
-
Notifications
You must be signed in to change notification settings - Fork 141
remove types from Index.__iter__()` #532
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
Conversation
@@ -223,7 +222,7 @@ class Index(IndexOpsMixin, PandasObject): | |||
def shape(self) -> tuple[int, ...]: ... | |||
# Extra methods from old stubs | |||
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override] | |||
def __iter__(self) -> Iterator[IndexIterScalar | tuple[Hashable, ...]]: ... | |||
def __iter__(self) -> Iterator: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to type the return type, shouldn't this simply have been Iterator[Hashable]
(tuple and everything in IndexIterScalar) is hashable?
(Having mypy_primer could have been helpful to see the false-positives earlier.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to type the return type, shouldn't this simply have been
Iterator[Hashable]
(tuple and everything in IndexIterScalar) is hashable?(Having mypy_primer could have been helpful to see the false-positives earlier.)
If I change it to Iterator[Hashable]
, then I get this with the test (and example above) from pyright:
Operator "in" not supported for types "Literal['at']" and "Hashable"
Operator "in" not supported for types "Literal['at']" and "Hashable"
In the original issue: #365 I had pointed out this was having issues:
That is a contrived example, but I believe the result of that is being unable to assign If we make this change, are these common patterns still supported?
I would have expected that I or someone else added tests for all of these but given that you didn't have to remove any tests, I'm not so sure that the test coverage is there. I may have added tests checking the type when I should have added tests checking the usage pattern. |
Since we don't have a generic index yet, we don't know the types that are inside of
Yes, and I added that to the tests in commit 50f4f9f
I guess we didn't have the tests when one tries to use I did have an idea (but it would take some work to get right) to change pandas where we make cols = [c for c in df.columns.tolist(str) if "at" in c] The return type of |
Thanks @Dr-Irv |
Hm...I guess I'm surprised everything works, because when I originally submitted that bug, I did so in response to something that was broken in my firm's codebase. Maybe it was fixed elsewhere since then. When this is released I'll keep an eye out for any regressions. |
* remove types from Index.__iter__()` * add some tests
assert_type()
to assert the type of any return valuetest_frame.py/test_in_columns()
Without this fix, the following code reports errors:
pyright then reports:
mypy reports:
This is because of a change made in #367 by @gandhis1 . Comments welcome from @gandhis1 if I missed something here.
Our application code uses this pattern a lot, and it would be really painful to keep having to do
cast
when selecting a subset of the columns.