-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
TYP: Overload concat #41184
TYP: Overload concat #41184
Conversation
The only issue left seems to be $ mypy t.py
...
pandas/core/reshape/concat.py:58: error: Overloaded function signatures 1 and 3 overlap with incompatible return types [misc]
... but signature 1 has I played around with it but could not get the desired |
Thanks for looking into this - I also wouldn't have expected them to overlap, so I'm not sure why this is happening, but I'll look into it later |
Updated overloads as I just realized that a passed |
I can't make sense of the mypy error you're getting: from __future__ import annotations
from typing import overload, Iterable, Mapping, Hashable
@overload
def foo(myarg: Iterable[str] | Mapping[Hashable, str]) -> str: ...
@overload
def foo(myarg: Iterable[bool] | Mapping[Hashable, bool]) -> bool: ...
def foo(myarg) -> str | bool: ... $ mypy t.py
t.py:5: error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
Found 1 error in 1 file (checked 1 source file) I'll ask on StackOverflow, then open an issue in |
OK, I see the issue: it's because from typing import Iterable
a: Iterable[str]
a = {'foo': 0} So...gonna have to think about what to do about this e.g. from typing import Hashable
from pandas import DataFrame
a: Hashable
a = DataFrame() type checks fine. So, if you pass a mapping in which the keys are |
This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this. |
This is pending on #41283 being merged first |
Hey @pckSF - if you fetch and merge upstream/master, then I'd expect the mypy issue to be fixed |
Done, and the error is gone |
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.
can you fixup the remaining lint issues?
mypy --version
mypy 0.910
Performing static analysis using mypy
pandas/core/reshape/reshape.py:1053: error: Redundant cast to "DataFrame" [redundant-cast]
pandas/core/reshape/melt.py:139: error: Redundant cast to "Series" [redundant-cast]
pandas/core/groupby/generic.py:312: error: unused "type: ignore" comment
pandas/core/groupby/generic.py:551: error: unused "type: ignore" comment
Found 4 errors in 3 files (checked 1299 source files)
Performing static analysis using mypy DONE
Error: Process completed with exit code 1.
nice - is this ready for review? |
Thanks for the reminder, I always forget that I have them in preview... |
Nice, looks good - the typing failure is unrelated, might need to merge again when that's fixed on master |
just merged #42614 |
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.
I think a couple of these might be unnecessary, because the return type is always the same if axis is 1/"columns"
Could you check that you check the same result by removing them? (if not I can do it later in the week)
🤔 maybe those overloads weren't redundant...I'm not sure why though, I'll look into it |
Question regarding def astype(
self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise"
) -> FrameOrSeries:
...
...
result = concat(results, axis=1, copy=False)
result.columns = self.columns
return result given that it uses |
If I do diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 3b8458fac2..db6f213b3b 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -5747,7 +5747,9 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
return self.copy()
# GH 19920: retain column metadata after concat
+ reveal_type(results)
result = concat(results, axis=1, copy=False)
+ reveal_type(result)
result.columns = self.columns
return result then I get $ mypy pandas
pandas/core/arrays/categorical.py:2318: error: Incompatible types in assignment (expression has type "List[str]", variable has type "Index") [assignment]
pandas/core/generic.py:5750: note: Revealed type is "builtins.list[Any]"
pandas/core/generic.py:5752: note: Revealed type is "pandas.core.frame.DataFrame"
pandas/core/generic.py:5754: error: Incompatible return value type (got "DataFrame", expected "FrameOrSeries") [return-value]
pandas/core/generic.py:6117: error: Incompatible return value type (got "DataFrame", expected "FrameOrSeries") [return-value]
Found 3 errors in 2 files (checked 1299 source files) , so the revealed type at $ mypy pandas
pandas/core/generic.py:5750: note: Revealed type is "builtins.list[Any]"
pandas/core/generic.py:5752: note: Revealed type is "Any" So, it worked in the previous commit because the type checker was being disabled on the Likewise, on $ mypy pandas
pandas/core/generic.py:5750: note: Revealed type is "builtins.list[Any]"
pandas/core/generic.py:5752: note: Revealed type is "Any" So, what you have in the current commit seems correct, although I can't figure out why in your previous commit the type of |
OK, looks like we're running into python/mypy#8354 So, I'd suggest you do something like diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 3b8458fac2..6205463aad 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -5749,7 +5749,8 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
# GH 19920: retain column metadata after concat
result = concat(results, axis=1, copy=False)
result.columns = self.columns
- return result
+ # https://github.com/python/mypy/issues/8354
+ return cast(FrameOrSeries, result)
@final
def copy(self: FrameOrSeries, deep: bool_t = True) -> FrameOrSeries:
@@ -6112,7 +6113,8 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
for col_name, col in self.items()
]
if len(results) > 0:
- return concat(results, axis=1, copy=False)
+ # https://github.com/python/mypy/issues/8354
+ return cast(FrameOrSeries, concat(results, axis=1, copy=False))
else:
return self.copy() The other issue seems correct though, for that I'd suggest diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
index 3fdb52a73d..57e7c0af52 100644
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -2315,7 +2315,7 @@ class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMi
from pandas.core.reshape.concat import concat
result = concat([counts, freqs], axis=1)
- result.columns = ["counts", "freqs"]
+ result.columns = Index(["counts", "freqs"])
result.index.name = "categories"
return result |
thanks - now the CI is failing because of #42716 , once that's fixed I'd like to think that this PR will be fine |
should be good to fetch/merge upstream/master now |
@@ -2311,7 +2311,7 @@ def describe(self): | |||
from pandas.core.reshape.concat import concat | |||
|
|||
result = concat([counts, freqs], axis=1) | |||
result.columns = ["counts", "freqs"] | |||
result.columns = Index(["counts", "freqs"]) |
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.
you'll need to import Index
in/after line 2311 for this to work
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.
cc @simonjayhawkins in case you have comments |
Thanks @pckSF ! |
* TYP: Overload concat * Fix NDFrame columns overload * Fix FrameOrSeries * Fix redundant casts * Import Axis outside of TYPE_CHECKING * Remove redundant overloads * Mark mypy/issues/8354 return type issues * Fix missing imports * noop Co-authored-by: Marco Gorelli <[email protected]>
concat
from https://github.com/pandas-dev/pandas/blob/master/pandas/core/reshape/concat.pymypy
Setup and Sutputwhich results in the following mypy output: