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

Conversation

pckSF
Copy link
Contributor

@pckSF pckSF commented Apr 25, 2021

Setup and Output for ffill

# t_ffill.py
import pandas as pd

inplace: bool

reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=False))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=True))
reveal_type(pd.DataFrame([1, None, None]).ffill())
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=inplace))

reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=False))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=True))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=inplace))

reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=False, limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=True, limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=inplace, limit=1))

reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=False, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=True, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=inplace, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=False, limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=True, limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, limit=1))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=inplace, limit=1))

reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=False, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=True, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=inplace, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=False, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=True, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(inplace=inplace, limit=1, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=False, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=True, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).ffill(axis=0, inplace=inplace, limit=1, downcast="infer"))

which results in the following mypy output:

$ mypy t_ffill.py
  t_ffill.py:6: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:7: note: Revealed type is 'None'
  t_ffill.py:8: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:9: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:11: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:12: note: Revealed type is 'None'
  t_ffill.py:13: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:14: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:16: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:17: note: Revealed type is 'None'
  t_ffill.py:18: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:19: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:21: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:22: note: Revealed type is 'None'
  t_ffill.py:23: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:24: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:26: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:27: note: Revealed type is 'None'
  t_ffill.py:28: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:29: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:31: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:32: note: Revealed type is 'None'
  t_ffill.py:33: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:34: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:36: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:37: note: Revealed type is 'None'
  t_ffill.py:38: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:39: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:41: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:42: note: Revealed type is 'None'
  t_ffill.py:43: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:44: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'

Setup and Ouput for bfill

# t_bfill.py
import pandas as pd

inplace: bool

reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=False))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=True))
reveal_type(pd.DataFrame([1, None, None]).bfill())
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=inplace))

reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=False))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=True))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=inplace))

reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=False, limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=True, limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=inplace, limit=1))

reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=False, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=True, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=inplace, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=False, limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=True, limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, limit=1))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=inplace, limit=1))

reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=False, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=True, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=inplace, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=False, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=True, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(inplace=inplace, limit=1, downcast="infer"))

reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=False, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=True, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, limit=1, downcast="infer"))
reveal_type(pd.DataFrame([1, None, None]).bfill(axis=0, inplace=inplace, limit=1, downcast="infer"))

which results in the following mypy output:

$ mypy t_bfill.py
  t_ffill.py:6: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:7: note: Revealed type is 'None'
  t_ffill.py:8: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:9: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:11: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:12: note: Revealed type is 'None'
  t_ffill.py:13: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:14: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:16: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:17: note: Revealed type is 'None'
  t_ffill.py:18: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:19: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:21: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:22: note: Revealed type is 'None'
  t_ffill.py:23: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:24: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:26: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:27: note: Revealed type is 'None'
  t_ffill.py:28: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:29: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:31: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:32: note: Revealed type is 'None'
  t_ffill.py:33: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:34: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:36: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:37: note: Revealed type is 'None'
  t_ffill.py:38: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:39: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'
  t_ffill.py:41: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:42: note: Revealed type is 'None'
  t_ffill.py:43: note: Revealed type is 'pandas.core.frame.DataFrame*'
  t_ffill.py:44: note: Revealed type is 'Union[pandas.core.frame.DataFrame*, None]'

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks Peter, have left some comments

Not too sure about downcast but that one can always be left out for now and typed later on

@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

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

@MarcoGorelli MarcoGorelli self-requested a review April 25, 2021 19:43
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Small comment about the return type in the function itself, other than that looks good to me

downcast=None,
) -> FrameOrSeries | 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

@pckSF pckSF marked this pull request as ready for review April 25, 2021 20:47
@MarcoGorelli MarcoGorelli added the Typing type annotations, mypy/pyright type checking label Apr 26, 2021
@MarcoGorelli MarcoGorelli added this to the 1.3 milestone Apr 26, 2021
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks @pckSF !

@MarcoGorelli MarcoGorelli merged commit 44b041e into pandas-dev:master Apr 26, 2021
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
* TYP: Overload series/ffill and bfill

* Resolved comments

* Keep the return types
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
* TYP: Overload series/ffill and bfill

* Resolved comments

* Keep the return types
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.

2 participants