-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
TYP: fix a few types #54976
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: fix a few types #54976
Changes from 3 commits
88f93f9
0f72079
4f03cc2
9d9aefc
bb379a5
9b99638
b3a2ada
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1926,11 +1926,17 @@ def to_dict( | |
self, | ||
orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., | ||
into: type[dict] = ..., | ||
index: bool = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Present in the implementation but missing in the overloads |
||
) -> dict: | ||
... | ||
|
||
@overload | ||
def to_dict(self, orient: Literal["records"], into: type[dict] = ...) -> list[dict]: | ||
def to_dict( | ||
self, | ||
orient: Literal["records"], | ||
into: type[dict] = ..., | ||
index: bool = ..., | ||
) -> list[dict]: | ||
... | ||
|
||
@deprecate_nonkeyword_arguments( | ||
|
@@ -11297,7 +11303,7 @@ def _reduce_axis1(self, name: str, func, skipna: bool) -> Series: | |
def any( # type: ignore[override] | ||
self, | ||
*, | ||
axis: Axis = 0, | ||
axis: Axis | None = 0, | ||
bool_only: bool = False, | ||
skipna: bool = True, | ||
**kwargs, | ||
|
@@ -11312,7 +11318,7 @@ def any( # type: ignore[override] | |
@doc(make_doc("all", ndim=2)) | ||
def all( | ||
self, | ||
axis: Axis = 0, | ||
axis: Axis | None = 0, | ||
bool_only: bool = False, | ||
skipna: bool = True, | ||
**kwargs, | ||
|
@@ -11711,6 +11717,7 @@ def quantile( | |
axis: Axis = ..., | ||
numeric_only: bool = ..., | ||
interpolation: QuantileInterpolation = ..., | ||
method: Literal["single", "table"] = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Present in the implementation but missing in the overloads |
||
) -> Series: | ||
... | ||
|
||
|
@@ -11721,6 +11728,7 @@ def quantile( | |
axis: Axis = ..., | ||
numeric_only: bool = ..., | ||
interpolation: QuantileInterpolation = ..., | ||
method: Literal["single", "table"] = ..., | ||
) -> Series | DataFrame: | ||
... | ||
|
||
|
@@ -11731,6 +11739,7 @@ def quantile( | |
axis: Axis = ..., | ||
numeric_only: bool = ..., | ||
interpolation: QuantileInterpolation = ..., | ||
method: Literal["single", "table"] = ..., | ||
) -> Series | DataFrame: | ||
... | ||
|
||
|
@@ -11830,11 +11839,10 @@ def quantile( | |
|
||
if not is_list_like(q): | ||
# BlockManager.quantile expects listlike, so we wrap and unwrap here | ||
# error: List item 0 has incompatible type "Union[float, Union[Union[ | ||
# ExtensionArray, ndarray[Any, Any]], Index, Series], Sequence[float]]"; | ||
# expected "float" | ||
res_df = self.quantile( # type: ignore[call-overload] | ||
[q], | ||
# error: List item 0 has incompatible type "float | ExtensionArray | | ||
# ndarray[Any, Any] | Index | Series | Sequence[float]"; expected "float" | ||
res_df = self.quantile( | ||
[q], # type: ignore[list-item] | ||
axis=axis, | ||
numeric_only=numeric_only, | ||
interpolation=interpolation, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -941,9 +941,7 @@ def write( | |
if isinstance(writer, ExcelWriter): | ||
need_save = False | ||
else: | ||
# error: Cannot instantiate abstract class 'ExcelWriter' with abstract | ||
# attributes 'engine', 'save', 'supported_extensions' and 'write_cells' | ||
writer = ExcelWriter( # type: ignore[abstract] | ||
writer = ExcelWriter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this immediately raise in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, no, because we overwrite |
||
writer, | ||
engine=engine, | ||
storage_options=storage_options, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1307,13 +1307,60 @@ def read_table( | |
return _read(filepath_or_buffer, kwds) | ||
|
||
|
||
@overload | ||
def read_fwf( | ||
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], | ||
*, | ||
colspecs: Sequence[tuple[int, int]] | str | None = ..., | ||
widths: Sequence[int] | None = ..., | ||
infer_nrows: int = ..., | ||
dtype_backend: DtypeBackend | lib.NoDefault = ..., | ||
iterator: Literal[True], | ||
chunksize: int | None = ..., | ||
**kwds, | ||
) -> TextFileReader: | ||
... | ||
|
||
|
||
@overload | ||
def read_fwf( | ||
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], | ||
*, | ||
colspecs: Sequence[tuple[int, int]] | str | None = ..., | ||
widths: Sequence[int] | None = ..., | ||
infer_nrows: int = ..., | ||
dtype_backend: DtypeBackend | lib.NoDefault = ..., | ||
iterator: bool = ..., | ||
chunksize: int, | ||
**kwds, | ||
) -> TextFileReader: | ||
... | ||
|
||
|
||
@overload | ||
def read_fwf( | ||
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], | ||
*, | ||
colspecs: Sequence[tuple[int, int]] | str | None = ..., | ||
widths: Sequence[int] | None = ..., | ||
infer_nrows: int = ..., | ||
dtype_backend: DtypeBackend | lib.NoDefault = ..., | ||
iterator: Literal[False] = ..., | ||
chunksize: None = ..., | ||
**kwds, | ||
) -> DataFrame: | ||
... | ||
|
||
|
||
def read_fwf( | ||
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str], | ||
*, | ||
colspecs: Sequence[tuple[int, int]] | str | None = "infer", | ||
widths: Sequence[int] | None = None, | ||
infer_nrows: int = 100, | ||
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default, | ||
iterator: bool = False, | ||
chunksize: int | None = None, | ||
**kwds, | ||
) -> DataFrame | TextFileReader: | ||
r""" | ||
|
@@ -1374,6 +1421,9 @@ def read_fwf( | |
-------- | ||
>>> pd.read_fwf('data.csv') # doctest: +SKIP | ||
""" | ||
kwds["iterator"] = iterator | ||
kwds["chunksize"] = chunksize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Would it be better to do this down on L1462 with the other assignments? |
||
|
||
# Check input arguments. | ||
if colspecs is None and widths is None: | ||
raise ValueError("Must specify either colspecs or widths") | ||
|
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.
For https://pandas.pydata.org/docs/reference/api/pandas.util.hash_pandas_object.html and other functions
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 there are other methods in this module that are not meant to be public though:
capitalize_first_letter
,cache_readonly
,Substitution
,Appender
Uh oh!
There was an error while loading. Please reload this page.
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.
Okay, I think we could do
__all__ = [all_classes_functions_in_the_docs]
(inutils/__init__
) to account for that. And/or we could deprecate it from utils and put it as part of a different namespace.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.
__all__
only changes behavior offrom foo import *
, no? What do we think of moving these functions (can we make a list of them?) to the top namespace?Does this hold up other aspects of this PR? If not, I would suggest moving to a separate PR.
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.
Happy to remove this change from this PR. I will open an issue to first discuss how this should be handled.
__all__
affectsfrom pandas.utils import *
but (more importantly) type checkers, use it (next to other rules) to determine which symbols are meant to be public: if__all__
is present, only those symbols listed in it are considered public.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.
This doesn't seem to be the case with mypy:
mypy bar.py
reports no issues for me.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 must have misremembered that (pyright also allows that).
Pyright describes how it handles it: https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface