Skip to content

Commit

Permalink
Add alias decorator handling to the generated allowlist
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden committed Sep 26, 2023
1 parent a711b12 commit a59c268
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 83 deletions.
82 changes: 0 additions & 82 deletions ci/mypy-stubtest-allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,88 +63,6 @@ matplotlib.contour.ContourSet.tlinewidths
# positional-only argument name lacking leading underscores
matplotlib.axes._base._AxesBase.axis

# Aliases (dynamically generated, not type hinted)
matplotlib.collections.Collection.get_aa
matplotlib.collections.Collection.get_antialiaseds
matplotlib.collections.Collection.get_dashes
matplotlib.collections.Collection.get_ec
matplotlib.collections.Collection.get_edgecolors
matplotlib.collections.Collection.get_facecolors
matplotlib.collections.Collection.get_fc
matplotlib.collections.Collection.get_linestyles
matplotlib.collections.Collection.get_linewidths
matplotlib.collections.Collection.get_ls
matplotlib.collections.Collection.get_lw
matplotlib.collections.Collection.get_transOffset
matplotlib.collections.Collection.set_aa
matplotlib.collections.Collection.set_antialiaseds
matplotlib.collections.Collection.set_dashes
matplotlib.collections.Collection.set_ec
matplotlib.collections.Collection.set_edgecolors
matplotlib.collections.Collection.set_facecolors
matplotlib.collections.Collection.set_fc
matplotlib.collections.Collection.set_linestyles
matplotlib.collections.Collection.set_linewidths
matplotlib.collections.Collection.set_ls
matplotlib.collections.Collection.set_lw
matplotlib.collections.Collection.set_transOffset
matplotlib.lines.Line2D.get_aa
matplotlib.lines.Line2D.get_c
matplotlib.lines.Line2D.get_ds
matplotlib.lines.Line2D.get_ls
matplotlib.lines.Line2D.get_lw
matplotlib.lines.Line2D.get_mec
matplotlib.lines.Line2D.get_mew
matplotlib.lines.Line2D.get_mfc
matplotlib.lines.Line2D.get_mfcalt
matplotlib.lines.Line2D.get_ms
matplotlib.lines.Line2D.set_aa
matplotlib.lines.Line2D.set_c
matplotlib.lines.Line2D.set_ds
matplotlib.lines.Line2D.set_ls
matplotlib.lines.Line2D.set_lw
matplotlib.lines.Line2D.set_mec
matplotlib.lines.Line2D.set_mew
matplotlib.lines.Line2D.set_mfc
matplotlib.lines.Line2D.set_mfcalt
matplotlib.lines.Line2D.set_ms
matplotlib.patches.Patch.get_aa
matplotlib.patches.Patch.get_ec
matplotlib.patches.Patch.get_fc
matplotlib.patches.Patch.get_ls
matplotlib.patches.Patch.get_lw
matplotlib.patches.Patch.set_aa
matplotlib.patches.Patch.set_ec
matplotlib.patches.Patch.set_fc
matplotlib.patches.Patch.set_ls
matplotlib.patches.Patch.set_lw
matplotlib.text.Text.get_c
matplotlib.text.Text.get_family
matplotlib.text.Text.get_font
matplotlib.text.Text.get_font_properties
matplotlib.text.Text.get_ha
matplotlib.text.Text.get_name
matplotlib.text.Text.get_size
matplotlib.text.Text.get_style
matplotlib.text.Text.get_va
matplotlib.text.Text.get_variant
matplotlib.text.Text.get_weight
matplotlib.text.Text.set_c
matplotlib.text.Text.set_family
matplotlib.text.Text.set_font
matplotlib.text.Text.set_font_properties
matplotlib.text.Text.set_ha
matplotlib.text.Text.set_ma
matplotlib.text.Text.set_name
matplotlib.text.Text.set_size
matplotlib.text.Text.set_stretch
matplotlib.text.Text.set_style
matplotlib.text.Text.set_va
matplotlib.text.Text.set_variant
matplotlib.text.Text.set_weight
matplotlib.axes._base._AxesBase.get_fc
matplotlib.axes._base._AxesBase.set_fc

# Other dynamic python behaviors not type hinted
matplotlib.rcsetup.defaultParams

Expand Down
28 changes: 27 additions & 1 deletion tools/stubtest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import os
import pathlib
import subprocess
import sys
Expand All @@ -22,10 +23,34 @@ def visit_FunctionDef(self, node):
if hasattr(node, "parent"):
parent = node.parent
while hasattr(parent, "parent") and not isinstance(parent, ast.Module):
parents.append(parent.name)
parents.insert(0, parent.name)
parent = parent.parent
self.output.write(f"{'.'.join(self.context + parents)}.{node.name}\n")

def visit_ClassDef(self, node):
for dec in node.decorator_list:
if "define_aliases" in ast.unparse(dec):
parents = []
if hasattr(node, "parent"):
parent = node.parent
while hasattr(parent, "parent") and not isinstance(
parent, ast.Module
):
parents.insert(0, parent.name)
parent = parent.parent
aliases = ast.literal_eval(dec.args[0])
# Written as a regex rather than two lines to avoid unused entries
# for setters on items with only a getter
for substitutions in aliases.values():
parts = self.context + parents + [node.name]
self.output.write(
"\n".join(
f"{'.'.join(parts)}.[gs]et_{a}\n" for a in substitutions
)
)
for child in ast.iter_child_nodes(node):
self.visit(child)


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

sys.exit(proc.returncode)

0 comments on commit a59c268

Please sign in to comment.