Skip to content

TYP: Overload series/ffill and bfill #41152

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 3 commits into from
Apr 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 68 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6469,15 +6469,45 @@ def fillna(
else:
return result.__finalize__(self, method="fillna")

@overload
def ffill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: Literal[False] = ...,
limit: None | int = ...,
downcast: None | str | dict = ...,
) -> FrameOrSeries:
...

@overload
def ffill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: Literal[True] = ...,
Copy link
Member

Choose a reason for hiding this comment

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

this one doesn't need =... - else, if you call .ffill(), mypy won't know which overload to match

Copy link
Member

Choose a reason for hiding this comment

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

there also needs to be an extra overload for when axis isn't passed:

    @overload
    def ffill(
        self: FrameOrSeries,
        *,
        inplace: Literal[True],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

limit: None | int = ...,
downcast: None | str | dict = ...,
) -> None:
...

@overload
def ffill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: bool_t = ...,
limit: None | int = ...,
downcast: None | str | dict = ...,
) -> FrameOrSeries | None:
...

@final
@doc(klass=_shared_doc_kwargs["klass"])
def ffill(
self: FrameOrSeries,
axis=None,
axis: None | str | int = None,
Copy link
Member

Choose a reason for hiding this comment

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

axis has it's own type alias, Axis (check the fillna signature)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

inplace: bool_t = False,
limit=None,
downcast=None,
) -> FrameOrSeries | None:
limit: None | int = None,
downcast: None | str | dict = None,
):
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 it should work to keep the return types here, then the function body's return type will be fixed (admittedly this needs fixing in my blog post, will do that now)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

"""
Synonym for :meth:`DataFrame.fillna` with ``method='ffill'``.

Expand All @@ -6492,15 +6522,45 @@ def ffill(

pad = ffill

@overload
def bfill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: Literal[False] = ...,
limit: None | int = ...,
downcast: None | str | dict = ...,
) -> FrameOrSeries:
...

@overload
def bfill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: Literal[True] = ...,
limit: None | int = ...,
downcast: None | str | dict = ...,
) -> None:
...

@overload
def bfill(
self: FrameOrSeries,
axis: None | str | int = ...,
inplace: bool_t = ...,
limit: None | int = ...,
downcast: None | str | dict = ...,
) -> FrameOrSeries | None:
...

@final
@doc(klass=_shared_doc_kwargs["klass"])
def bfill(
self: FrameOrSeries,
axis=None,
axis: None | str | int = None,
inplace: bool_t = False,
limit=None,
downcast=None,
) -> FrameOrSeries | None:
limit: None | int = None,
downcast: None | str | dict = None,
):
"""
Synonym for :meth:`DataFrame.fillna` with ``method='bfill'``.

Expand Down