Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e46ff2

Browse files
authored
Merge pull request#22712 from oscargus/reprinerrors
Use repr in error messages
2 parentsc90b134 +22a44db commit0e46ff2

File tree

9 files changed

+66
-22
lines changed

9 files changed

+66
-22
lines changed

‎lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,25 @@ def _process_plot_format(fmt):
169169
iffmt[i:i+2]inmlines.lineStyles:# First, the two-char styles.
170170
iflinestyleisnotNone:
171171
raiseValueError(
172-
'Illegal format string"%s"; two linestyle symbols'%fmt)
172+
f'Illegal format string{fmt!r}; two linestyle symbols')
173173
linestyle=fmt[i:i+2]
174174
i+=2
175175
elifcinmlines.lineStyles:
176176
iflinestyleisnotNone:
177177
raiseValueError(
178-
'Illegal format string"%s"; two linestyle symbols'%fmt)
178+
f'Illegal format string{fmt!r}; two linestyle symbols')
179179
linestyle=c
180180
i+=1
181181
elifcinmlines.lineMarkers:
182182
ifmarkerisnotNone:
183183
raiseValueError(
184-
'Illegal format string"%s"; two marker symbols'%fmt)
184+
f'Illegal format string{fmt!r}; two marker symbols')
185185
marker=c
186186
i+=1
187187
elifcinmcolors.get_named_colors_mapping():
188188
ifcolorisnotNone:
189189
raiseValueError(
190-
'Illegal format string"%s"; two color symbols'%fmt)
190+
f'Illegal format string{fmt!r}; two color symbols')
191191
color=c
192192
i+=1
193193
elifc=='C'andi<len(fmt)-1:
@@ -2084,8 +2084,8 @@ def axis(self, *args, emit=True, **kwargs):
20842084
self.set_ylim([ylim[0],ylim[0]+edge_size],
20852085
emit=emit,auto=False)
20862086
else:
2087-
raiseValueError('Unrecognized string%s to axis;'
2088-
'tryon or off'%s)
2087+
raiseValueError(f"Unrecognized string{s!r} to axis;"
2088+
"try'on' or'off'")
20892089
else:
20902090
iflen(args)==1:
20912091
limits=args[0]

‎lib/matplotlib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ def _set_transforms(self):
17641764
elifself._units=='dots':
17651765
sc=1.0
17661766
else:
1767-
raiseValueError('unrecognized units:%s'%self._units)
1767+
raiseValueError(f'Unrecognized units:{self._units!r}')
17681768

17691769
self._transforms=np.zeros((len(self._widths),3,3))
17701770
widths=self._widths*sc

‎lib/matplotlib/patches.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ def _make_verts(self):
14691469
coords=np.concatenate([left_half_arrow[:-1],
14701470
right_half_arrow[-2::-1]])
14711471
else:
1472-
raiseValueError("Got unknown shape:%s"%self.shape)
1472+
raiseValueError(f"Got unknown shape:{self._shape!r}")
14731473
ifdistance!=0:
14741474
cx=self._dx/distance
14751475
sx=self._dy/distance
@@ -2191,12 +2191,13 @@ def __new__(cls, stylename, **kwargs):
21912191
try:
21922192
_cls=cls._style_list[_name]
21932193
exceptKeyErroraserr:
2194-
raiseValueError(f"Unknown style:{stylename}")fromerr
2194+
raiseValueError(f"Unknown style:{stylename!r}")fromerr
21952195
try:
21962196
_args_pair= [cs.split("=")forcsin_list[1:]]
21972197
_args= {k:float(v)fork,vin_args_pair}
21982198
exceptValueErroraserr:
2199-
raiseValueError(f"Incorrect style argument:{stylename}")fromerr
2199+
raiseValueError(
2200+
f"Incorrect style argument:{stylename!r}")fromerr
22002201
return_cls(**{**_args,**kwargs})
22012202

22022203
@classmethod

‎lib/matplotlib/rcsetup.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def validate_bool(b):
146146
elifbin ('f','n','no','off','false','0',0,False):
147147
returnFalse
148148
else:
149-
raiseValueError('Could notconvert"%s" to bool'%b)
149+
raiseValueError(f'Cannotconvert{b!r} to bool')
150150

151151

152152
defvalidate_axisbelow(s):
@@ -156,8 +156,8 @@ def validate_axisbelow(s):
156156
ifisinstance(s,str):
157157
ifs=='line':
158158
return'line'
159-
raiseValueError('%s cannot be interpreted as'
160-
' True, False, or "line"'%s)
159+
raiseValueError(f'{s!r} cannot be interpreted as'
160+
' True, False, or "line"')
161161

162162

163163
defvalidate_dpi(s):
@@ -739,14 +739,14 @@ def validate_cycler(s):
739739
_DunderChecker().visit(ast.parse(s))
740740
s=eval(s, {'cycler':cycler,'__builtins__': {}})
741741
exceptBaseExceptionase:
742-
raiseValueError("'%s' is not a valid cycler construction:%s"%
743-
(s,e))frome
742+
raiseValueError(f"{s!r} is not a valid cycler construction:{e}"
743+
)frome
744744
# Should make sure what comes from the above eval()
745745
# is a Cycler object.
746746
ifisinstance(s,Cycler):
747747
cycler_inst=s
748748
else:
749-
raiseValueError("object was not a string or Cycler instance:%s"%s)
749+
raiseValueError(f"Object is not a string or Cycler instance:{s!r}")
750750

751751
unknowns=cycler_inst.keys- (set(_prop_validators)|set(_prop_aliases))
752752
ifunknowns:

‎lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def _option_boolean(arg):
183183
elifarg.strip().lower()in ('yes','1','true'):
184184
returnTrue
185185
else:
186-
raiseValueError('"%s" unknown boolean'%arg)
186+
raiseValueError(f'{arg!r} unknown boolean')
187187

188188

189189
def_option_context(arg):

‎lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5096,7 +5096,8 @@ def test_shared_aspect_error():
50965096
r"axis\(\) takes 0 or 1 positional arguments but 2"
50975097
" were given"),
50985098
(ValueError, ('foo', ), {},
5099-
"Unrecognized string foo to axis; try on or off"),
5099+
"Unrecognized string 'foo' to axis; try 'on' or "
5100+
"'off'"),
51005101
(TypeError, ([1,2], ), {},
51015102
"the first argument to axis*"),
51025103
(TypeError,tuple(), {'foo':None},
@@ -7676,6 +7677,19 @@ def test_empty_line_plots():
76767677
assertlen(line)==1
76777678

76787679

7680+
@pytest.mark.parametrize('fmt, match', (
7681+
("foo","Unrecognized character f in format string 'foo'"),
7682+
("o+","Illegal format string 'o\\+'; two marker symbols"),
7683+
(":-","Illegal format string ':-'; two linestyle symbols"),
7684+
("rk","Illegal format string 'rk'; two color symbols"),
7685+
(":o-r","Illegal format string ':o-r'; two linestyle symbols"),
7686+
))
7687+
deftest_plot_format_errors(fmt,match):
7688+
fig,ax=plt.subplots()
7689+
withpytest.raises(ValueError,match=match):
7690+
ax.plot((0,0),fmt)
7691+
7692+
76797693
deftest_clim():
76807694
ax=plt.figure().add_subplot()
76817695
forplot_methodin [

‎lib/matplotlib/tests/test_patches.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
importmatplotlibasmpl
99
frommatplotlib.patchesimport (Annulus,Ellipse,Patch,Polygon,Rectangle,
10-
FancyArrowPatch)
10+
FancyArrowPatch,FancyArrow,BoxStyle)
1111
frommatplotlib.testing.decoratorsimportimage_comparison,check_figures_equal
1212
frommatplotlib.transformsimportBbox
1313
importmatplotlib.pyplotasplt
@@ -692,6 +692,20 @@ def test_rotated_arcs():
692692
ax.set_aspect("equal")
693693

694694

695+
deftest_fancyarrow_shape_error():
696+
withpytest.raises(ValueError,match="Got unknown shape: 'foo'"):
697+
FancyArrow(0,0,0.2,0.2,shape='foo')
698+
699+
700+
@pytest.mark.parametrize('fmt, match', (
701+
("foo","Unknown style: 'foo'"),
702+
("Round,foo","Incorrect style argument: 'Round,foo'"),
703+
))
704+
deftest_boxstyle_errors(fmt,match):
705+
withpytest.raises(ValueError,match=match):
706+
BoxStyle(fmt)
707+
708+
695709
@image_comparison(baseline_images=['annulus'],extensions=['png'])
696710
deftest_annulus():
697711

‎lib/matplotlib/tests/test_text.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ def test_annotation_contains():
251251
assertann.contains(event)== (False, {})
252252

253253

254+
@pytest.mark.parametrize('err, xycoords, match', (
255+
(RuntimeError,print,"Unknown return type"),
256+
(RuntimeError, [0,0],r"Unknown coordinate type: \[0, 0\]"),
257+
(ValueError,"foo","'foo' is not a recognized coordinate"),
258+
(ValueError,"foo bar","'foo bar' is not a recognized coordinate"),
259+
(ValueError,"offset foo","xycoords cannot be an offset coordinate"),
260+
(ValueError,"axes foo","'foo' is not a recognized unit"),
261+
))
262+
deftest_annotate_errors(err,xycoords,match):
263+
fig,ax=plt.subplots()
264+
withpytest.raises(err,match=match):
265+
ax.annotate('xy', (0,0),xytext=(0.5,0.5),xycoords=xycoords)
266+
fig.canvas.draw()
267+
268+
254269
@image_comparison(['titles'])
255270
deftest_titles():
256271
# left and right side titles

‎lib/matplotlib/text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ def _get_xy_transform(self, renderer, s):
14661466

14671467
s_=s.split()
14681468
iflen(s_)!=2:
1469-
raiseValueError("%s is not a recognized coordinate"%s)
1469+
raiseValueError(f"{s!r} is not a recognized coordinate")
14701470

14711471
bbox0,xy0=None,None
14721472

@@ -1506,12 +1506,12 @@ def _get_xy_transform(self, renderer, s):
15061506
w,h=bbox0.size
15071507
tr=Affine2D().scale(w,h)
15081508
else:
1509-
raiseValueError("%s is not a recognizedcoordinate"%s)
1509+
raiseValueError(f"{unit!r} is not a recognizedunit")
15101510

15111511
returntr.translate(ref_x,ref_y)
15121512

15131513
else:
1514-
raiseValueError("%s is not a recognized coordinate"%s)
1514+
raiseValueError(f"{s!r} is not a recognized coordinate")
15151515

15161516
def_get_ref_xy(self,renderer):
15171517
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp