-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[TYP] Add tool for running stubtest #26928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a711b12
Add tool for running stubtest
ksunden a59c268
Add alias decorator handling to the generated allowlist
ksunden 61661f1
Close file so it hopefully works on windows (but not tested)
ksunden d7ad3ad
Merge branch 'main' into stubtest_script
ksunden 06e4731
Switch to tempdirectory for windows support and autocleanup
ksunden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import ast | ||
import os | ||
import pathlib | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
root = pathlib.Path(__file__).parent.parent | ||
|
||
lib = root / "lib" | ||
mpl = lib / "matplotlib" | ||
|
||
|
||
class Visitor(ast.NodeVisitor): | ||
def __init__(self, filepath, output): | ||
self.filepath = filepath | ||
self.context = list(filepath.with_suffix("").relative_to(lib).parts) | ||
self.output = output | ||
|
||
def visit_FunctionDef(self, node): | ||
if any("delete_parameter" in ast.unparse(line) for line in node.decorator_list): | ||
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 | ||
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.TemporaryDirectory() as d: | ||
p = pathlib.Path(d) / "allowlist.txt" | ||
with p.open("wt") as f: | ||
for path in mpl.glob("**/*.py"): | ||
v = Visitor(path, f) | ||
tree = ast.parse(path.read_text()) | ||
|
||
# Assign parents to tree so they can be backtraced | ||
for node in ast.walk(tree): | ||
for child in ast.iter_child_nodes(node): | ||
child.parent = node | ||
|
||
v.visit(tree) | ||
proc = subprocess.run( | ||
[ | ||
"stubtest", | ||
"--mypy-config-file=pyproject.toml", | ||
"--allowlist=ci/mypy-stubtest-allowlist.txt", | ||
f"--allowlist={p}", | ||
"matplotlib", | ||
], | ||
cwd=root, | ||
env=os.environ | {"MPLBACKEND": "agg"}, | ||
) | ||
try: | ||
os.unlink(f.name) | ||
except OSError: | ||
pass | ||
Comment on lines
+79
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I just realized this is still here; it can probably be deleted now that you're using |
||
|
||
sys.exit(proc.returncode) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.