Skip to content

Commit

Permalink
Fix typing and 3.7 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
thatch committed Nov 12, 2024
1 parent 460ddcb commit 8873151
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions metadata_please/source_checkout_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import ast
from typing import Any, Dict, Optional


# Copied from orig-index
Expand All @@ -21,14 +22,14 @@ class ShortCircuitingVisitor(ast.NodeVisitor):
visiting of children is not the responsibility of the visit_ method.
"""

def visit(self, node):
def visit(self, node: ast.AST) -> None:
method = "visit_" + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
rv = visitor(node)
if rv:
self.visit_children(node)

def visit_children(self, node):
def visit_children(self, node: ast.AST) -> None:
for field, value in ast.iter_fields(node):
if isinstance(value, list):
for item in value:
Expand All @@ -37,36 +38,37 @@ def visit_children(self, node):
elif isinstance(value, ast.AST):
self.visit(value)

def generic_visit(self, node) -> bool:
def generic_visit(self, node: ast.AST) -> bool:
return True


class QualifiedNameSaver(ShortCircuitingVisitor):
"""Similar to LibCST's QualifiedNameProvider except simpler and wronger"""

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.qualified_name_prefixes = {}
self.qualified_name_prefixes: Dict[str, str] = {}

def qualified_name(self, node: ast.AST) -> str:
if isinstance(node, ast.Attribute):
return self.qualified_name(node.value) + "." + node.attr
elif isinstance(node, ast.Expr):
return self.qualified_name(node.value)
elif isinstance(node, ast.Name):
if new := self.qualified_name_prefixes.get(node.id):
new = self.qualified_name_prefixes.get(node.id)
if new:
return new
return f"<locals>.{node.id}"
else:
raise ValueError(f"Complex expression: {type(node)}")

def visit_Import(self, node: ast.Import):
def visit_Import(self, node: ast.Import) -> None:
# .names
# alias = (identifier name, identifier? asname)
for a in node.names:
self.qualified_name_prefixes[a.asname or a.name] = a.name

def visit_ImportFrom(self, node: ast.ImportFrom):
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
# .identifier / .level
# .names
# alias = (identifier name, identifier? asname)
Expand All @@ -87,12 +89,12 @@ class Unknown:


class SetupFindingVisitor(QualifiedNameSaver):
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.setup_call_args = None
self.setup_call_kwargs = None
self.setup_call_args: Optional[Dict[str, Any]] = None
self.setup_call_kwargs: Optional[bool] = None

def visit_Call(self, node):
def visit_Call(self, node: ast.Call) -> None:
# .func (expr, can just be name)
# .args
# .keywords
Expand Down

0 comments on commit 8873151

Please sign in to comment.