Created
March 22, 2021 01:21
-
-
Save mwtoews/2da8d6e1d757a3fca26069d3ddb82577 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Check built-in super function to see if could be made better via PEP 3135. | |
| Created on Sat Mar 20 21:23:36 2021 | |
| """ | |
| import ast | |
| import os | |
| import git | |
| done = set() # lineno, col | |
| def find_super_in_class(parent): | |
| global done | |
| for node in ast.walk(parent): | |
| if node is not parent and isinstance(node, ast.ClassDef): | |
| # print('recurse 1 with', node.name, sep=" ") | |
| find_super_in_class(node) | |
| elif isinstance(node, ast.FunctionDef): | |
| for part in ast.walk(node): | |
| if (isinstance(part, ast.Call) and | |
| isinstance(part.func, ast.Name) and | |
| part.func.id == "super" and | |
| len(part.args) == 2): | |
| here = (part.lineno, part.col_offset) | |
| if (here not in done and | |
| isinstance(part.args[0], ast.Name) and | |
| isinstance(part.args[1], ast.Name)): | |
| info = "{}: {}: {}".format( | |
| part.lineno, node.name, parent.name) | |
| if part.args[0].id == parent.name: | |
| if part.args[1].id == node.args.args[0].arg: | |
| print("ok: {}".format(info)) | |
| done.add(here) | |
| else: | |
| print("!!! NO: different 2nd arg: {}".format( | |
| info)) | |
| else: | |
| print("!!! NO: different 1st arg: {}".format(info)) | |
| elif part is not node and isinstance(part, ast.ClassDef): | |
| # print('recurse 2 with', part.name, sep=" ") | |
| find_super_in_class(part) | |
| repo = git.Repo('.', search_parent_directories=True) | |
| file_list = repo.git.grep("--files-with-matches", "super([^)]").split("\n") | |
| print("found {} files in {}".format(len(file_list), repo.working_tree_dir)) | |
| for fname in file_list: | |
| print(fname) | |
| with open(os.path.join(repo.working_tree_dir, fname), 'r') as f: | |
| try: | |
| module = ast.parse(f.read()) | |
| except SyntaxError as e: | |
| print(e) | |
| print() | |
| continue | |
| for node in ast.walk(module): | |
| if isinstance(node, ast.ClassDef): | |
| part = find_super_in_class(node) | |
| print() | |
| done = set() # lineno, col |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment