Skip to content

Commit a59c268

Browse files
committed
Add alias decorator handling to the generated allowlist
1 parent a711b12 commit a59c268

File tree

2 files changed

+27
-83
lines changed

2 files changed

+27
-83
lines changed

ci/mypy-stubtest-allowlist.txt

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -63,88 +63,6 @@ matplotlib.contour.ContourSet.tlinewidths
6363
# positional-only argument name lacking leading underscores
6464
matplotlib.axes._base._AxesBase.axis
6565

66-
# Aliases (dynamically generated, not type hinted)
67-
matplotlib.collections.Collection.get_aa
68-
matplotlib.collections.Collection.get_antialiaseds
69-
matplotlib.collections.Collection.get_dashes
70-
matplotlib.collections.Collection.get_ec
71-
matplotlib.collections.Collection.get_edgecolors
72-
matplotlib.collections.Collection.get_facecolors
73-
matplotlib.collections.Collection.get_fc
74-
matplotlib.collections.Collection.get_linestyles
75-
matplotlib.collections.Collection.get_linewidths
76-
matplotlib.collections.Collection.get_ls
77-
matplotlib.collections.Collection.get_lw
78-
matplotlib.collections.Collection.get_transOffset
79-
matplotlib.collections.Collection.set_aa
80-
matplotlib.collections.Collection.set_antialiaseds
81-
matplotlib.collections.Collection.set_dashes
82-
matplotlib.collections.Collection.set_ec
83-
matplotlib.collections.Collection.set_edgecolors
84-
matplotlib.collections.Collection.set_facecolors
85-
matplotlib.collections.Collection.set_fc
86-
matplotlib.collections.Collection.set_linestyles
87-
matplotlib.collections.Collection.set_linewidths
88-
matplotlib.collections.Collection.set_ls
89-
matplotlib.collections.Collection.set_lw
90-
matplotlib.collections.Collection.set_transOffset
91-
matplotlib.lines.Line2D.get_aa
92-
matplotlib.lines.Line2D.get_c
93-
matplotlib.lines.Line2D.get_ds
94-
matplotlib.lines.Line2D.get_ls
95-
matplotlib.lines.Line2D.get_lw
96-
matplotlib.lines.Line2D.get_mec
97-
matplotlib.lines.Line2D.get_mew
98-
matplotlib.lines.Line2D.get_mfc
99-
matplotlib.lines.Line2D.get_mfcalt
100-
matplotlib.lines.Line2D.get_ms
101-
matplotlib.lines.Line2D.set_aa
102-
matplotlib.lines.Line2D.set_c
103-
matplotlib.lines.Line2D.set_ds
104-
matplotlib.lines.Line2D.set_ls
105-
matplotlib.lines.Line2D.set_lw
106-
matplotlib.lines.Line2D.set_mec
107-
matplotlib.lines.Line2D.set_mew
108-
matplotlib.lines.Line2D.set_mfc
109-
matplotlib.lines.Line2D.set_mfcalt
110-
matplotlib.lines.Line2D.set_ms
111-
matplotlib.patches.Patch.get_aa
112-
matplotlib.patches.Patch.get_ec
113-
matplotlib.patches.Patch.get_fc
114-
matplotlib.patches.Patch.get_ls
115-
matplotlib.patches.Patch.get_lw
116-
matplotlib.patches.Patch.set_aa
117-
matplotlib.patches.Patch.set_ec
118-
matplotlib.patches.Patch.set_fc
119-
matplotlib.patches.Patch.set_ls
120-
matplotlib.patches.Patch.set_lw
121-
matplotlib.text.Text.get_c
122-
matplotlib.text.Text.get_family
123-
matplotlib.text.Text.get_font
124-
matplotlib.text.Text.get_font_properties
125-
matplotlib.text.Text.get_ha
126-
matplotlib.text.Text.get_name
127-
matplotlib.text.Text.get_size
128-
matplotlib.text.Text.get_style
129-
matplotlib.text.Text.get_va
130-
matplotlib.text.Text.get_variant
131-
matplotlib.text.Text.get_weight
132-
matplotlib.text.Text.set_c
133-
matplotlib.text.Text.set_family
134-
matplotlib.text.Text.set_font
135-
matplotlib.text.Text.set_font_properties
136-
matplotlib.text.Text.set_ha
137-
matplotlib.text.Text.set_ma
138-
matplotlib.text.Text.set_name
139-
matplotlib.text.Text.set_size
140-
matplotlib.text.Text.set_stretch
141-
matplotlib.text.Text.set_style
142-
matplotlib.text.Text.set_va
143-
matplotlib.text.Text.set_variant
144-
matplotlib.text.Text.set_weight
145-
matplotlib.axes._base._AxesBase.get_fc
146-
matplotlib.axes._base._AxesBase.set_fc
147-
14866
# Other dynamic python behaviors not type hinted
14967
matplotlib.rcsetup.defaultParams
15068

tools/stubtest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ast
2+
import os
23
import pathlib
34
import subprocess
45
import sys
@@ -22,10 +23,34 @@ def visit_FunctionDef(self, node):
2223
if hasattr(node, "parent"):
2324
parent = node.parent
2425
while hasattr(parent, "parent") and not isinstance(parent, ast.Module):
25-
parents.append(parent.name)
26+
parents.insert(0, parent.name)
2627
parent = parent.parent
2728
self.output.write(f"{'.'.join(self.context + parents)}.{node.name}\n")
2829

30+
def visit_ClassDef(self, node):
31+
for dec in node.decorator_list:
32+
if "define_aliases" in ast.unparse(dec):
33+
parents = []
34+
if hasattr(node, "parent"):
35+
parent = node.parent
36+
while hasattr(parent, "parent") and not isinstance(
37+
parent, ast.Module
38+
):
39+
parents.insert(0, parent.name)
40+
parent = parent.parent
41+
aliases = ast.literal_eval(dec.args[0])
42+
# Written as a regex rather than two lines to avoid unused entries
43+
# for setters on items with only a getter
44+
for substitutions in aliases.values():
45+
parts = self.context + parents + [node.name]
46+
self.output.write(
47+
"\n".join(
48+
f"{'.'.join(parts)}.[gs]et_{a}\n" for a in substitutions
49+
)
50+
)
51+
for child in ast.iter_child_nodes(node):
52+
self.visit(child)
53+
2954

3055
with tempfile.NamedTemporaryFile("wt") as f:
3156
for path in mpl.glob("**/*.py"):
@@ -48,6 +73,7 @@ def visit_FunctionDef(self, node):
4873
"matplotlib",
4974
],
5075
cwd=root,
76+
env=os.environ | {"MPLBACKEND": "agg"},
5177
)
5278

5379
sys.exit(proc.returncode)

0 commit comments

Comments
 (0)