Skip to content

TYP: annotation of __init__ return type (PEP 484) (pandas/plotting) #46283

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 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ class PlotAccessor(PandasObject):
_kind_aliases = {"density": "kde"}
_all_kinds = _common_kinds + _series_kinds + _dataframe_kinds

def __init__(self, data):
def __init__(self, data) -> 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 am pretty sure that not annotating this was a deliberate decision in the past.

cc @simonjayhawkins did this change over the last few weeks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am pretty sure that not annotating this was a deliberate decision in the past.

cc @simonjayhawkins did this change over the last few weeks?

As far as I can see, this line was added on 03/07/2019(#27009) without any additional comments. And this line also has not changed since those times.

Copy link
Member

Choose a reason for hiding this comment

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

I am talking about init returning None in general

Copy link
Member

Choose a reason for hiding this comment

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

Simon mentioned this in #44677 (comment)

self._parent = data

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BP(NamedTuple):
ax: Axes
lines: dict[str, list[Line2D]]

def __init__(self, data, return_type="axes", **kwargs):
def __init__(self, data, return_type="axes", **kwargs) -> None:
# Do not call LinePlot.__init__ which may fill nan
if return_type not in self._valid_return_types:
raise ValueError("return_type must be {None, 'axes', 'dict', 'both'}")
Expand Down
10 changes: 5 additions & 5 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def default_units(x, axis) -> str:

# time formatter
class TimeFormatter(Formatter):
def __init__(self, locs):
def __init__(self, locs) -> None:
self.locs = locs

def __call__(self, x, pos=0) -> str:
Expand Down Expand Up @@ -339,7 +339,7 @@ def axisinfo(unit: tzinfo | None, axis) -> units.AxisInfo:


class PandasAutoDateFormatter(dates.AutoDateFormatter):
def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"):
def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d") -> None:
dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)


Expand Down Expand Up @@ -371,7 +371,7 @@ class MilliSecondLocator(dates.DateLocator):

UNIT = 1.0 / (24 * 3600 * 1000)

def __init__(self, tz):
def __init__(self, tz) -> None:
dates.DateLocator.__init__(self, tz)
self._interval = 1.0

Expand Down Expand Up @@ -941,7 +941,7 @@ def __init__(
month=1,
day=1,
plot_obj=None,
):
) -> None:
freq = to_offset(freq)
self.freq = freq
self.base = base
Expand Down Expand Up @@ -1025,7 +1025,7 @@ def __init__(
minor_locator: bool = False,
dynamic_mode: bool = True,
plot_obj=None,
):
) -> None:
freq = to_offset(freq)
self.format = None
self.freq = freq
Expand Down
16 changes: 8 additions & 8 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(
include_bool=False,
column: IndexLabel | None = None,
**kwds,
):
) -> None:

import matplotlib.pyplot as plt

Expand Down Expand Up @@ -984,7 +984,7 @@ class PlanePlot(MPLPlot):

_layout_type = "single"

def __init__(self, data, x, y, **kwargs):
def __init__(self, data, x, y, **kwargs) -> None:
MPLPlot.__init__(self, data, **kwargs)
if x is None or y is None:
raise ValueError(self._kind + " requires an x and y column")
Expand Down Expand Up @@ -1037,7 +1037,7 @@ def _plot_colorbar(self, ax: Axes, **kwds):
class ScatterPlot(PlanePlot):
_kind = "scatter"

def __init__(self, data, x, y, s=None, c=None, **kwargs):
def __init__(self, data, x, y, s=None, c=None, **kwargs) -> None:
if s is None:
# hide the matplotlib default for size, in case we want to change
# the handling of this argument later
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def _make_plot(self):
class HexBinPlot(PlanePlot):
_kind = "hexbin"

def __init__(self, data, x, y, C=None, **kwargs):
def __init__(self, data, x, y, C=None, **kwargs) -> None:
super().__init__(data, x, y, **kwargs)
if is_integer(C) and not self.data.columns.holds_integer():
C = self.data.columns[C]
Expand Down Expand Up @@ -1157,7 +1157,7 @@ class LinePlot(MPLPlot):
_default_rot = 0
orientation = "vertical"

def __init__(self, data, **kwargs):
def __init__(self, data, **kwargs) -> None:
from pandas.plotting import plot_params

MPLPlot.__init__(self, data, **kwargs)
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def get_label(i):
class AreaPlot(LinePlot):
_kind = "area"

def __init__(self, data, **kwargs):
def __init__(self, data, **kwargs) -> None:
kwargs.setdefault("stacked", True)
data = data.fillna(value=0)
LinePlot.__init__(self, data, **kwargs)
Expand Down Expand Up @@ -1424,7 +1424,7 @@ class BarPlot(MPLPlot):
_default_rot = 90
orientation = "vertical"

def __init__(self, data, **kwargs):
def __init__(self, data, **kwargs) -> None:
# we have to treat a series differently than a
# 1-column DataFrame w.r.t. color handling
self._is_series = isinstance(data, ABCSeries)
Expand Down Expand Up @@ -1606,7 +1606,7 @@ class PiePlot(MPLPlot):
_kind = "pie"
_layout_type = "horizontal"

def __init__(self, data, kind=None, **kwargs):
def __init__(self, data, kind=None, **kwargs) -> None:
data = data.fillna(value=0)
if (data < 0).any().any():
raise ValueError(f"{self._kind} plot doesn't allow negative values")
Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
class HistPlot(LinePlot):
_kind = "hist"

def __init__(self, data, bins=10, bottom=0, **kwargs):
def __init__(self, data, bins=10, bottom=0, **kwargs) -> None:
self.bins = bins # use mpl default
self.bottom = bottom
# Do not call LinePlot.__init__ which may fill nan
Expand Down Expand Up @@ -170,7 +170,7 @@ class KdePlot(HistPlot):
_kind = "kde"
orientation = "vertical"

def __init__(self, data, bw_method=None, ind=None, **kwargs):
def __init__(self, data, bw_method=None, ind=None, **kwargs) -> None:
MPLPlot.__init__(self, data, **kwargs)
self.bw_method = bw_method
self.ind = ind
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ class _Options(dict):
_ALIASES = {"x_compat": "xaxis.compat"}
_DEFAULT_KEYS = ["xaxis.compat"]

def __init__(self, deprecated=False):
def __init__(self, deprecated=False) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

as long as you're doing these can you try to annotate other things in these signatures

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, I think I can do it in separate PR as long as these are for fixing __init__ return type annotation

Copy link
Member

Choose a reason for hiding this comment

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

yeah, let's maybe keep the scope here limited to the return type of __init__ since if there is consensus on #46337, there will be quite a bit to do (if not done in one big PR, #46337 (comment))

self._deprecated = deprecated
super().__setitem__("xaxis.compat", False)

Expand Down