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

Appearance settings

Commit aef5bd6

Browse filesBrowse files
committed
Rewrite Triangulation.get_from_args_and_kwargs to make parameter parsing testable
1 parent dc1e9c1 commit aef5bd6
Copy full SHA for aef5bd6

File tree

Expand file treeCollapse file tree

2 files changed

+71
-25
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-25
lines changed

‎lib/matplotlib/tests/test_triangulation.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_triangulation.py
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,52 @@
1212
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1313

1414

15+
x = [-1, 0, 1, 0]
16+
y = [0, -1, 0, 1]
17+
triangles = [[0, 1, 2], [0, 2, 3]]
18+
mask = [False, True]
19+
20+
21+
@pytest.mark.parametrize('args, kwargs, expected', [
22+
([x, y], {}, [x, y, None, None]),
23+
([x, y, triangles], {}, [x, y, triangles, None]),
24+
([x, y], dict(triangles=triangles), [x, y, triangles, None]),
25+
([x, y], dict(mask=mask), [x, y, None, mask]),
26+
([x, y, triangles], dict(mask=mask), [x, y, triangles, mask]),
27+
([x, y], dict(triangles=triangles, mask=mask), [x, y, triangles, mask]),
28+
])
29+
def test_extract_triangulation_params(args, kwargs, expected):
30+
other_args = [1, 2]
31+
other_kwargs = {'a': 3, 'b': '4'}
32+
x_, y_, triangles_, mask_, args_, kwargs_ = \
33+
mtri.Triangulation._extract_triangulation_params(
34+
args + other_args, {**kwargs, **other_kwargs})
35+
x, y, triangles, mask = expected
36+
assert x_ is x
37+
assert y_ is y
38+
assert_array_equal(triangles_, triangles)
39+
assert mask_ is mask
40+
assert args_ == other_args
41+
assert kwargs_ == other_kwargs
42+
43+
44+
def test_extract_triangulation_positional_mask():
45+
global x, y, triangles, mask
46+
# mask cannot be passed positionally
47+
x_, y_, triangles_, mask_, args_, kwargs_ = \
48+
mtri.Triangulation._extract_triangulation_params(x, y, triangles, mask)
49+
assert mask_ is None
50+
assert args_ == [mask]
51+
# the positional mask has to be catched downstream because this must pass
52+
# unknown args through
53+
54+
55+
del x
56+
del y
57+
del triangles
58+
del mask
59+
60+
1561
def test_delaunay():
1662
# No duplicate points, regular grid.
1763
nx = 5

‎lib/matplotlib/tri/triangulation.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tri/triangulation.py
+25-25Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,34 @@ def get_from_args_and_kwargs(*args, **kwargs):
136136
if isinstance(args[0], Triangulation):
137137
triangulation, *args = args
138138
else:
139-
x, y, *args = args
140-
141-
# Check triangles in kwargs then args.
142-
triangles = kwargs.pop('triangles', None)
143-
from_args = False
144-
if triangles is None and args:
145-
triangles = args[0]
146-
from_args = True
147-
148-
if triangles is not None:
149-
try:
150-
triangles = np.asarray(triangles, dtype=np.int32)
151-
except ValueError:
152-
triangles = None
153-
154-
if triangles is not None and (triangles.ndim != 2 or
155-
triangles.shape[1] != 3):
156-
triangles = None
157-
158-
if triangles is not None and from_args:
159-
args = args[1:] # Consumed first item in args.
160-
161-
# Check for mask in kwargs.
162-
mask = kwargs.pop('mask', None)
163-
139+
x, y, triangles, mask, args, kwargs = \
140+
Triangulation._extract_triangulation_params(args, kwargs)
164141
triangulation = Triangulation(x, y, triangles, mask)
165142
return triangulation, args, kwargs
166143

144+
@staticmethod
145+
def _extract_triangulation_params(args, kwargs):
146+
x, y, *args = args
147+
# Check triangles in kwargs then args.
148+
triangles = kwargs.pop('triangles', None)
149+
from_args = False
150+
if triangles is None and args:
151+
triangles = args[0]
152+
from_args = True
153+
if triangles is not None:
154+
try:
155+
triangles = np.asarray(triangles, dtype=np.int32)
156+
except ValueError:
157+
triangles = None
158+
if triangles is not None and (triangles.ndim != 2 or
159+
triangles.shape[1] != 3):
160+
triangles = None
161+
if triangles is not None and from_args:
162+
args = args[1:] # Consumed first item in args.
163+
# Check for mask in kwargs.
164+
mask = kwargs.pop('mask', None)
165+
return x, y, triangles, mask, args, kwargs
166+
167167
def get_trifinder(self):
168168
"""
169169
Return the default `matplotlib.tri.TriFinder` of this

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.