-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
275 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,55 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from argparse import ArgumentParser | ||
|
||
from mypy.build import build | ||
from mypy.main import process_options | ||
from mypy.nodes import ClassDef | ||
from mypy.traverser import TraverserVisitor | ||
from rich.console import Console | ||
|
||
CONTEXT_KEY = "mypy_visitor" | ||
|
||
|
||
class MyPyVisitor(TraverserVisitor): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.classes: dict[str, bool] = {} | ||
|
||
def visit_class_def(self, o: ClassDef) -> None: | ||
super().visit_class_def(o) | ||
self.classes[o.fullname] = o.info.has_base("pydantic.main.BaseModel") | ||
|
||
|
||
def run_mypy_visitor(arg_files: list[str], console: Console | None = None) -> dict[str, bool]: | ||
console = console or Console() | ||
files, opt = process_options(arg_files, stdout=sys.stdout, stderr=sys.stderr) | ||
|
||
opt.export_types = True | ||
opt.incremental = True | ||
opt.fine_grained_incremental = True | ||
opt.cache_fine_grained = True | ||
opt.allow_redefinition = True | ||
opt.local_partial_types = True | ||
|
||
console.print("Running MyPy - this may take a while...") | ||
result = build(files, opt, stdout=sys.stdout, stderr=sys.stderr) | ||
|
||
visitor = MyPyVisitor() | ||
classes: dict[str, bool] = {} | ||
|
||
for file in files: | ||
tree = result.graph[file.module].tree | ||
if tree: | ||
tree.accept(visitor=visitor) | ||
classes.update(visitor.classes) | ||
return classes | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument("files", nargs="+") | ||
args = parser.parse_args() | ||
|
||
run_mypy_visitor(args.files) |
This file contains 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 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 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 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
Oops, something went wrong.