Skip to content

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

Merged
merged 7 commits into from
Sep 6, 2023
Merged

TYP: fix a few types #54976

merged 7 commits into from
Sep 6, 2023

Conversation

twoertwein
Copy link
Member

I fixed a few type issues discovered by using the pandas-stubs type tests with the pandas annotations (there are many errors).

I'm surprised that the two Excel-classes being marked as abstract wasn't detected by the CI, they are clearly meant to be instantiated, so they cannot be abstract.

@@ -110,6 +110,7 @@

from pandas.core.dtypes.dtypes import SparseDtype

from pandas import util
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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

Copy link
Member Author

@twoertwein twoertwein Sep 5, 2023

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] (in utils/__init__) to account for that. And/or we could deprecate it from utils and put it as part of a different namespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__all__ only changes behavior of from 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.

Copy link
Member Author

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__ affects from 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if __all__ is present, only those symbols listed in it are considered public.

This doesn't seem to be the case with mypy:

# test/__init__.py
x = 5
y = 6

__all__ = ['y']

# bar.py
import test

print(test.x)
print(test.y)

mypy bar.py reports no issues for me.

Copy link
Member Author

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

@@ -1926,11 +1926,17 @@ def to_dict(
self,
orient: Literal["dict", "list", "series", "split", "tight", "index"] = ...,
into: type[dict] = ...,
index: bool = ...,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Present in the implementation but missing in the overloads

@@ -11711,6 +11717,7 @@ def quantile(
axis: Axis = ...,
numeric_only: bool = ...,
interpolation: QuantileInterpolation = ...,
method: Literal["single", "table"] = ...,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Present in the implementation but missing in the overloads

@twoertwein twoertwein marked this pull request as ready for review September 3, 2023 03:15
@mroeschke mroeschke added the Typing type annotations, mypy/pyright type checking label Sep 5, 2023
Comment on lines 1424 to 1425
kwds["iterator"] = iterator
kwds["chunksize"] = chunksize
Copy link
Member

Choose a reason for hiding this comment

The 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?

@@ -110,6 +110,7 @@

from pandas.core.dtypes.dtypes import SparseDtype

from pandas import util
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__all__ only changes behavior of from 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.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm; just leaving a few comments in case the bit that confused me gets anyone else.

# error: Cannot instantiate abstract class 'ExcelWriter' with abstract
# attributes 'engine', 'save', 'supported_extensions' and 'write_cells'
writer = ExcelWriter( # type: ignore[abstract]
writer = ExcelWriter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this immediately raise in the try below?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, no, because we overwrite __new__. I think this is okay.

@rhshadrach
Copy link
Member

@twoertwein - looks like isort is making a modification

@twoertwein
Copy link
Member Author

@twoertwein - looks like isort is making a modification

All green now :)

@mroeschke mroeschke modified the milestones: 2.1.1, 2.2 Sep 6, 2023
@mroeschke mroeschke merged commit e5f81ac into pandas-dev:main Sep 6, 2023
@mroeschke
Copy link
Member

Thanks @twoertwein

mroeschke pushed a commit to mroeschke/pandas that referenced this pull request Sep 11, 2023
* TYP: fix a few types

* namespace test

* read_fwf overloads

* Revert "namespace test"

This reverts commit 0f72079.

* revert util and move kwds

* isort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants