Skip to content

Commit d999aac

Browse files
TYP: towards matplotlib 3.8 (#55253)
* TYP: towards matplotlib 3.8 * test 3.8 * ignore pyright errors * merging error * Conditional on import * Disable parallel build to see docbuild error * Some unnecessary ignores * Add typing in test_sql * type ignores * Multiple ignores * Uncomment --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 800ae25 commit d999aac

16 files changed

+149
-62
lines changed

ci/deps/actions-310.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies:
3333
- gcsfs>=2022.11.0
3434
- jinja2>=3.1.2
3535
- lxml>=4.9.2
36-
- matplotlib>=3.6.3, <3.8
36+
- matplotlib>=3.6.3
3737
- numba>=0.56.4
3838
- numexpr>=2.8.4
3939
- odfpy>=1.4.1

ci/deps/actions-311-downstream_compat.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies:
3434
- gcsfs>=2022.11.0
3535
- jinja2>=3.1.2
3636
- lxml>=4.9.2
37-
- matplotlib>=3.6.3, <3.8
37+
- matplotlib>=3.6.3
3838
- numba>=0.56.4
3939
- numexpr>=2.8.4
4040
- odfpy>=1.4.1

ci/deps/actions-311.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies:
3333
- gcsfs>=2022.11.0
3434
- jinja2>=3.1.2
3535
- lxml>=4.9.2
36-
- matplotlib>=3.6.3, <3.8
36+
- matplotlib>=3.6.3
3737
- numba>=0.56.4
3838
- numexpr>=2.8.4
3939
- odfpy>=1.4.1

ci/deps/actions-39.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies:
3333
- gcsfs>=2022.11.0
3434
- jinja2>=3.1.2
3535
- lxml>=4.9.2
36-
- matplotlib>=3.6.3, <3.8
36+
- matplotlib>=3.6.3
3737
- numba>=0.56.4
3838
- numexpr>=2.8.4
3939
- odfpy>=1.4.1

ci/deps/circle-310-arm64.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies:
3333
- gcsfs>=2022.11.0
3434
- jinja2>=3.1.2
3535
- lxml>=4.9.2
36-
- matplotlib>=3.6.3, <3.8
36+
- matplotlib>=3.6.3
3737
- numba>=0.56.4
3838
- numexpr>=2.8.4
3939
- odfpy>=1.4.1

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies:
3535
- ipython
3636
- jinja2>=3.1.2
3737
- lxml>=4.9.2
38-
- matplotlib>=3.6.3, <3.8
38+
- matplotlib>=3.6.3
3939
- numba>=0.56.4
4040
- numexpr>=2.8.4
4141
- openpyxl>=3.1.0

pandas/plotting/_matplotlib/converter.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
if TYPE_CHECKING:
6565
from collections.abc import Generator
6666

67+
from matplotlib.axis import Axis
68+
6769
from pandas._libs.tslibs.offsets import BaseOffset
6870

6971

@@ -187,7 +189,7 @@ class TimeFormatter(Formatter):
187189
def __init__(self, locs) -> None:
188190
self.locs = locs
189191

190-
def __call__(self, x, pos: int = 0) -> str:
192+
def __call__(self, x, pos: int | None = 0) -> str:
191193
"""
192194
Return the time of day as a formatted string.
193195
@@ -364,8 +366,14 @@ def get_locator(self, dmin, dmax):
364366
locator = MilliSecondLocator(self.tz)
365367
locator.set_axis(self.axis)
366368

367-
locator.axis.set_view_interval(*self.axis.get_view_interval())
368-
locator.axis.set_data_interval(*self.axis.get_data_interval())
369+
# error: Item "None" of "Axis | _DummyAxis | _AxisWrapper | None"
370+
# has no attribute "get_data_interval"
371+
locator.axis.set_view_interval( # type: ignore[union-attr]
372+
*self.axis.get_view_interval() # type: ignore[union-attr]
373+
)
374+
locator.axis.set_data_interval( # type: ignore[union-attr]
375+
*self.axis.get_data_interval() # type: ignore[union-attr]
376+
)
369377
return locator
370378

371379
return mdates.AutoDateLocator.get_locator(self, dmin, dmax)
@@ -950,6 +958,8 @@ class TimeSeries_DateLocator(Locator):
950958
day : {int}, optional
951959
"""
952960

961+
axis: Axis
962+
953963
def __init__(
954964
self,
955965
freq: BaseOffset,
@@ -999,7 +1009,9 @@ def __call__(self):
9991009
base = self.base
10001010
(d, m) = divmod(vmin, base)
10011011
vmin = (d + 1) * base
1002-
locs = list(range(vmin, vmax + 1, base))
1012+
# error: No overload variant of "range" matches argument types "float",
1013+
# "float", "int"
1014+
locs = list(range(vmin, vmax + 1, base)) # type: ignore[call-overload]
10031015
return locs
10041016

10051017
def autoscale(self):
@@ -1038,6 +1050,8 @@ class TimeSeries_DateFormatter(Formatter):
10381050
Whether the formatter works in dynamic mode or not.
10391051
"""
10401052

1053+
axis: Axis
1054+
10411055
def __init__(
10421056
self,
10431057
freq: BaseOffset,
@@ -1084,7 +1098,7 @@ def set_locs(self, locs) -> None:
10841098
(vmin, vmax) = (vmax, vmin)
10851099
self._set_default_format(vmin, vmax)
10861100

1087-
def __call__(self, x, pos: int = 0) -> str:
1101+
def __call__(self, x, pos: int | None = 0) -> str:
10881102
if self.formatdict is None:
10891103
return ""
10901104
else:
@@ -1107,6 +1121,8 @@ class TimeSeries_TimedeltaFormatter(Formatter):
11071121
Formats the ticks along an axis controlled by a :class:`TimedeltaIndex`.
11081122
"""
11091123

1124+
axis: Axis
1125+
11101126
@staticmethod
11111127
def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
11121128
"""
@@ -1124,7 +1140,7 @@ def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
11241140
s = f"{int(d):d} days {s}"
11251141
return s
11261142

1127-
def __call__(self, x, pos: int = 0) -> str:
1143+
def __call__(self, x, pos: int | None = 0) -> str:
11281144
(vmin, vmax) = tuple(self.axis.get_view_interval())
11291145
n_decimals = min(int(np.ceil(np.log10(100 * 10**9 / abs(vmax - vmin)))), 9)
11301146
return self.format_timedelta_ticks(x, pos, n_decimals)

pandas/plotting/_matplotlib/core.py

+57-25
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,16 @@ def _maybe_right_yaxis(self, ax: Axes, axes_num: int) -> Axes:
529529
# otherwise, create twin axes
530530
orig_ax, new_ax = ax, ax.twinx()
531531
# TODO: use Matplotlib public API when available
532-
new_ax._get_lines = orig_ax._get_lines
533-
new_ax._get_patches_for_fill = orig_ax._get_patches_for_fill
534-
orig_ax.right_ax, new_ax.left_ax = new_ax, orig_ax
532+
new_ax._get_lines = orig_ax._get_lines # type: ignore[attr-defined]
533+
# TODO #54485
534+
new_ax._get_patches_for_fill = ( # type: ignore[attr-defined]
535+
orig_ax._get_patches_for_fill # type: ignore[attr-defined]
536+
)
537+
# TODO #54485
538+
orig_ax.right_ax, new_ax.left_ax = ( # type: ignore[attr-defined]
539+
new_ax,
540+
orig_ax,
541+
)
535542

536543
if not self._has_plotted_object(orig_ax): # no data on left y
537544
orig_ax.get_yaxis().set_visible(False)
@@ -540,7 +547,7 @@ def _maybe_right_yaxis(self, ax: Axes, axes_num: int) -> Axes:
540547
new_ax.set_yscale("log")
541548
elif self.logy == "sym" or self.loglog == "sym":
542549
new_ax.set_yscale("symlog")
543-
return new_ax
550+
return new_ax # type: ignore[return-value]
544551

545552
@final
546553
@cache_readonly
@@ -1206,12 +1213,15 @@ def _get_errorbars(
12061213

12071214
@final
12081215
def _get_subplots(self, fig: Figure):
1209-
from matplotlib.axes import Subplot
1216+
if Version(mpl.__version__) < Version("3.8"):
1217+
from matplotlib.axes import Subplot as Klass
1218+
else:
1219+
from matplotlib.axes import Axes as Klass
12101220

12111221
return [
12121222
ax
12131223
for ax in fig.get_axes()
1214-
if (isinstance(ax, Subplot) and ax.get_subplotspec() is not None)
1224+
if (isinstance(ax, Klass) and ax.get_subplotspec() is not None)
12151225
]
12161226

12171227
@final
@@ -1255,8 +1265,10 @@ def _post_plot_logic(self, ax: Axes, data) -> None:
12551265
x, y = self.x, self.y
12561266
xlabel = self.xlabel if self.xlabel is not None else pprint_thing(x)
12571267
ylabel = self.ylabel if self.ylabel is not None else pprint_thing(y)
1258-
ax.set_xlabel(xlabel)
1259-
ax.set_ylabel(ylabel)
1268+
# error: Argument 1 to "set_xlabel" of "_AxesBase" has incompatible
1269+
# type "Hashable"; expected "str"
1270+
ax.set_xlabel(xlabel) # type: ignore[arg-type]
1271+
ax.set_ylabel(ylabel) # type: ignore[arg-type]
12601272

12611273
@final
12621274
def _plot_colorbar(self, ax: Axes, *, fig: Figure, **kwds):
@@ -1393,7 +1405,7 @@ def _get_norm_and_cmap(self, c_values, color_by_categorical: bool):
13931405
else:
13941406
cmap = None
13951407

1396-
if color_by_categorical:
1408+
if color_by_categorical and cmap is not None:
13971409
from matplotlib import colors
13981410

13991411
n_cats = len(self.data[c].cat.categories)
@@ -1584,13 +1596,13 @@ def _ts_plot(self, ax: Axes, x, data: Series, style=None, **kwds):
15841596
decorate_axes(ax.left_ax, freq, kwds)
15851597
if hasattr(ax, "right_ax"):
15861598
decorate_axes(ax.right_ax, freq, kwds)
1587-
ax._plot_data.append((data, self._kind, kwds))
1599+
# TODO #54485
1600+
ax._plot_data.append((data, self._kind, kwds)) # type: ignore[attr-defined]
15881601

15891602
lines = self._plot(ax, data.index, np.asarray(data.values), style=style, **kwds)
15901603
# set date formatter, locators and rescale limits
1591-
# error: Argument 3 to "format_dateaxis" has incompatible type "Index";
1592-
# expected "DatetimeIndex | PeriodIndex"
1593-
format_dateaxis(ax, ax.freq, data.index) # type: ignore[arg-type]
1604+
# TODO #54485
1605+
format_dateaxis(ax, ax.freq, data.index) # type: ignore[arg-type, attr-defined]
15941606
return lines
15951607

15961608
@final
@@ -1606,11 +1618,15 @@ def _initialize_stacker(cls, ax: Axes, stacking_id, n: int) -> None:
16061618
if stacking_id is None:
16071619
return
16081620
if not hasattr(ax, "_stacker_pos_prior"):
1609-
ax._stacker_pos_prior = {}
1621+
# TODO #54485
1622+
ax._stacker_pos_prior = {} # type: ignore[attr-defined]
16101623
if not hasattr(ax, "_stacker_neg_prior"):
1611-
ax._stacker_neg_prior = {}
1612-
ax._stacker_pos_prior[stacking_id] = np.zeros(n)
1613-
ax._stacker_neg_prior[stacking_id] = np.zeros(n)
1624+
# TODO #54485
1625+
ax._stacker_neg_prior = {} # type: ignore[attr-defined]
1626+
# TODO #54485
1627+
ax._stacker_pos_prior[stacking_id] = np.zeros(n) # type: ignore[attr-defined]
1628+
# TODO #54485
1629+
ax._stacker_neg_prior[stacking_id] = np.zeros(n) # type: ignore[attr-defined]
16141630

16151631
@final
16161632
@classmethod
@@ -1624,9 +1640,17 @@ def _get_stacked_values(
16241640
cls._initialize_stacker(ax, stacking_id, len(values))
16251641

16261642
if (values >= 0).all():
1627-
return ax._stacker_pos_prior[stacking_id] + values
1643+
# TODO #54485
1644+
return (
1645+
ax._stacker_pos_prior[stacking_id] # type: ignore[attr-defined]
1646+
+ values
1647+
)
16281648
elif (values <= 0).all():
1629-
return ax._stacker_neg_prior[stacking_id] + values
1649+
# TODO #54485
1650+
return (
1651+
ax._stacker_neg_prior[stacking_id] # type: ignore[attr-defined]
1652+
+ values
1653+
)
16301654

16311655
raise ValueError(
16321656
"When stacked is True, each column must be either "
@@ -1640,9 +1664,11 @@ def _update_stacker(cls, ax: Axes, stacking_id: int | None, values) -> None:
16401664
if stacking_id is None:
16411665
return
16421666
if (values >= 0).all():
1643-
ax._stacker_pos_prior[stacking_id] += values
1667+
# TODO #54485
1668+
ax._stacker_pos_prior[stacking_id] += values # type: ignore[attr-defined]
16441669
elif (values <= 0).all():
1645-
ax._stacker_neg_prior[stacking_id] += values
1670+
# TODO #54485
1671+
ax._stacker_neg_prior[stacking_id] += values # type: ignore[attr-defined]
16461672

16471673
def _post_plot_logic(self, ax: Axes, data) -> None:
16481674
from matplotlib.ticker import FixedLocator
@@ -1658,7 +1684,9 @@ def get_label(i):
16581684
if self._need_to_set_index:
16591685
xticks = ax.get_xticks()
16601686
xticklabels = [get_label(x) for x in xticks]
1661-
ax.xaxis.set_major_locator(FixedLocator(xticks))
1687+
# error: Argument 1 to "FixedLocator" has incompatible type "ndarray[Any,
1688+
# Any]"; expected "Sequence[float]"
1689+
ax.xaxis.set_major_locator(FixedLocator(xticks)) # type: ignore[arg-type]
16621690
ax.set_xticklabels(xticklabels)
16631691

16641692
# If the index is an irregular time series, then by default
@@ -1737,9 +1765,11 @@ def _plot( # type: ignore[override]
17371765
if stacking_id is None:
17381766
start = np.zeros(len(y))
17391767
elif (y >= 0).all():
1740-
start = ax._stacker_pos_prior[stacking_id]
1768+
# TODO #54485
1769+
start = ax._stacker_pos_prior[stacking_id] # type: ignore[attr-defined]
17411770
elif (y <= 0).all():
1742-
start = ax._stacker_neg_prior[stacking_id]
1771+
# TODO #54485
1772+
start = ax._stacker_neg_prior[stacking_id] # type: ignore[attr-defined]
17431773
else:
17441774
start = np.zeros(len(y))
17451775

@@ -2005,7 +2035,9 @@ def _decorate_ticks(
20052035
ax.set_yticklabels(ticklabels)
20062036
if name is not None and self.use_index:
20072037
ax.set_ylabel(name)
2008-
ax.set_xlabel(self.xlabel)
2038+
# error: Argument 1 to "set_xlabel" of "_AxesBase" has incompatible type
2039+
# "Hashable | None"; expected "str"
2040+
ax.set_xlabel(self.xlabel) # type: ignore[arg-type]
20092041

20102042

20112043
class PiePlot(MPLPlot):

pandas/plotting/_matplotlib/hist.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,21 @@ def _get_column_weights(weights, i: int, y):
199199

200200
def _post_plot_logic(self, ax: Axes, data) -> None:
201201
if self.orientation == "horizontal":
202-
ax.set_xlabel("Frequency" if self.xlabel is None else self.xlabel)
203-
ax.set_ylabel(self.ylabel)
202+
# error: Argument 1 to "set_xlabel" of "_AxesBase" has incompatible
203+
# type "Hashable"; expected "str"
204+
ax.set_xlabel(
205+
"Frequency"
206+
if self.xlabel is None
207+
else self.xlabel # type: ignore[arg-type]
208+
)
209+
ax.set_ylabel(self.ylabel) # type: ignore[arg-type]
204210
else:
205-
ax.set_xlabel(self.xlabel)
206-
ax.set_ylabel("Frequency" if self.ylabel is None else self.ylabel)
211+
ax.set_xlabel(self.xlabel) # type: ignore[arg-type]
212+
ax.set_ylabel(
213+
"Frequency"
214+
if self.ylabel is None
215+
else self.ylabel # type: ignore[arg-type]
216+
)
207217

208218
@property
209219
def orientation(self) -> PlottingOrientation:
@@ -447,8 +457,14 @@ def hist_series(
447457
ax.grid(grid)
448458
axes = np.array([ax])
449459

460+
# error: Argument 1 to "set_ticks_props" has incompatible type "ndarray[Any,
461+
# dtype[Any]]"; expected "Axes | Sequence[Axes]"
450462
set_ticks_props(
451-
axes, xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot
463+
axes, # type: ignore[arg-type]
464+
xlabelsize=xlabelsize,
465+
xrot=xrot,
466+
ylabelsize=ylabelsize,
467+
yrot=yrot,
452468
)
453469

454470
else:

pandas/plotting/_matplotlib/style.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ def _is_single_string_color(color: Color) -> bool:
269269
"""
270270
conv = matplotlib.colors.ColorConverter()
271271
try:
272-
conv.to_rgba(color)
272+
# error: Argument 1 to "to_rgba" of "ColorConverter" has incompatible type
273+
# "str | Sequence[float]"; expected "tuple[float, float, float] | ..."
274+
conv.to_rgba(color) # type: ignore[arg-type]
273275
except ValueError:
274276
return False
275277
else:

0 commit comments

Comments
 (0)