Skip to content

Commit 73a731b

Browse files
authored
Merge pull request matplotlib#28599 from QuLogic/pyupgrade
Upgrade code to Python 3.10
2 parents 9c69c32 + 54c8491 commit 73a731b

38 files changed

+130
-127
lines changed

galleries/examples/showcase/stock_prices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
'ADBE', 'GSPC', 'IXIC']
4343

4444
# Manually adjust the label positions vertically (units are points = 1/72 inch)
45-
y_offsets = {k: 0 for k in stocks_ticker}
45+
y_offsets = dict.fromkeys(stocks_ticker, 0)
4646
y_offsets['IBM'] = 5
4747
y_offsets['AAPL'] = -5
4848
y_offsets['AMZN'] = -6

galleries/examples/units/basic_units.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
"""
99

10+
import itertools
1011
import math
1112

1213
from packaging.version import parse as parse_version
@@ -254,7 +255,7 @@ def get_unit(self):
254255

255256
class UnitResolver:
256257
def addition_rule(self, units):
257-
for unit_1, unit_2 in zip(units[:-1], units[1:]):
258+
for unit_1, unit_2 in itertools.pairwise(units):
258259
if unit_1 != unit_2:
259260
return NotImplemented
260261
return units[0]

lib/matplotlib/_api/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Callable, Generator, Mapping, Sequence
2-
from typing import Any, Iterable, TypeVar, overload
1+
from collections.abc import Callable, Generator, Iterable, Mapping, Sequence
2+
from typing import Any, TypeVar, overload
33
from typing_extensions import Self # < Py 3.11
44

55
from numpy.typing import NDArray

lib/matplotlib/_docstring.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def __missing__(self, key):
8282
name = key[:-len(":kwdoc")]
8383
from matplotlib.artist import Artist, kwdoc
8484
try:
85-
cls, = [cls for cls in _api.recursive_subclasses(Artist)
86-
if cls.__name__ == name]
85+
cls, = (cls for cls in _api.recursive_subclasses(Artist)
86+
if cls.__name__ == name)
8787
except ValueError as e:
8888
raise KeyError(key) from e
8989
return self.setdefault(key, kwdoc(cls))

lib/matplotlib/_docstring.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, Callable, TypeVar, overload
1+
from collections.abc import Callable
2+
from typing import Any, TypeVar, overload
23

34

45
_T = TypeVar('_T')

lib/matplotlib/_mathtext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def _get_info(self, fontname: str, font_class: str, sym: str, fontsize: float,
376376
font.set_size(fontsize, dpi)
377377
glyph = font.load_char(num, flags=self.load_glyph_flags)
378378

379-
xmin, ymin, xmax, ymax = [val/64.0 for val in glyph.bbox]
379+
xmin, ymin, xmax, ymax = (val / 64 for val in glyph.bbox)
380380
offset = self._get_offset(font, glyph, fontsize, dpi)
381381
metrics = FontMetrics(
382382
advance=glyph.linearHoriAdvance / 65536,
@@ -2645,7 +2645,7 @@ def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathSty
26452645
if rdelim == '':
26462646
rdelim = '.'
26472647
return self._auto_sized_delimiter(ldelim,
2648-
T.cast(list[T.Union[Box, Char, str]],
2648+
T.cast(list[Box | Char | str],
26492649
result),
26502650
rdelim)
26512651
return result
@@ -2786,7 +2786,7 @@ def _auto_sized_delimiter(self, front: str,
27862786
del middle[idx]
27872787
# There should only be \middle and its delimiter as str, which have
27882788
# just been removed.
2789-
middle_part = T.cast(list[T.Union[Box, Char]], middle)
2789+
middle_part = T.cast(list[Box | Char], middle)
27902790
else:
27912791
height = 0
27922792
depth = 0

lib/matplotlib/_mathtext_data.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from __future__ import annotations
6-
from typing import overload, Union
6+
from typing import overload
77

88
latex_to_bakoma = {
99
'\\__sqrt__' : ('cmex10', 0x70),
@@ -1113,11 +1113,10 @@
11131113
# Each element is a 4-tuple of the form:
11141114
# src_start, src_end, dst_font, dst_start
11151115

1116-
_EntryTypeIn = tuple[str, str, str, Union[str, int]]
1116+
_EntryTypeIn = tuple[str, str, str, str | int]
11171117
_EntryTypeOut = tuple[int, int, str, int]
11181118

1119-
_stix_virtual_fonts: dict[str, Union[dict[
1120-
str, list[_EntryTypeIn]], list[_EntryTypeIn]]] = {
1119+
_stix_virtual_fonts: dict[str, dict[str, list[_EntryTypeIn]] | list[_EntryTypeIn]] = {
11211120
'bb': {
11221121
"rm": [
11231122
("\N{DIGIT ZERO}",
@@ -1729,8 +1728,7 @@ def _normalize_stix_fontcodes(d):
17291728
return {k: _normalize_stix_fontcodes(v) for k, v in d.items()}
17301729

17311730

1732-
stix_virtual_fonts: dict[str, Union[dict[str, list[_EntryTypeOut]],
1733-
list[_EntryTypeOut]]]
1731+
stix_virtual_fonts: dict[str, dict[str, list[_EntryTypeOut]] | list[_EntryTypeOut]]
17341732
stix_virtual_fonts = _normalize_stix_fontcodes(_stix_virtual_fonts)
17351733

17361734
# Free redundant list now that it has been normalized

lib/matplotlib/animation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,8 @@ def _pre_composite_to_white(color):
10861086
# canvas._is_saving = True makes the draw_event animation-starting
10871087
# callback a no-op; canvas.manager = None prevents resizing the GUI
10881088
# widget (both are likewise done in savefig()).
1089-
with writer.saving(self._fig, filename, dpi), \
1090-
cbook._setattr_cm(self._fig.canvas, _is_saving=True, manager=None):
1089+
with (writer.saving(self._fig, filename, dpi),
1090+
cbook._setattr_cm(self._fig.canvas, _is_saving=True, manager=None)):
10911091
for anim in all_anim:
10921092
anim._init_draw() # Clear the initial frame
10931093
frame_number = 0

lib/matplotlib/artist.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from collections import namedtuple
22
import contextlib
3-
from functools import cache, wraps
3+
from functools import cache, reduce, wraps
44
import inspect
55
from inspect import Signature, Parameter
66
import logging
77
from numbers import Number, Real
8+
import operator
89
import re
910
import warnings
1011

@@ -1290,7 +1291,8 @@ def matchfunc(x):
12901291
raise ValueError('match must be None, a matplotlib.artist.Artist '
12911292
'subclass, or a callable')
12921293

1293-
artists = sum([c.findobj(matchfunc) for c in self.get_children()], [])
1294+
artists = reduce(operator.iadd,
1295+
[c.findobj(matchfunc) for c in self.get_children()], [])
12941296
if include_self and matchfunc(self):
12951297
artists.append(self)
12961298
return artists

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6000,7 +6000,7 @@ def _pcolorargs(self, funcname, *args, shading='auto', **kwargs):
60006000
# unit conversion allows e.g. datetime objects as axis values
60016001
X, Y = args[:2]
60026002
X, Y = self._process_unit_info([("x", X), ("y", Y)], kwargs)
6003-
X, Y = [cbook.safe_masked_invalid(a, copy=True) for a in [X, Y]]
6003+
X, Y = (cbook.safe_masked_invalid(a, copy=True) for a in [X, Y])
60046004

60056005
if funcname == 'pcolormesh':
60066006
if np.ma.is_masked(X) or np.ma.is_masked(Y):

0 commit comments

Comments
 (0)