MCPcopy Index your code
hub / github.com/python/mypy / ASTStubGenerator

Class ASTStubGenerator

mypy/stubgen.py:520–1500  ·  view source on GitHub ↗

Generate stub text from a mypy AST.

Source from the content-addressed store, hash-verified

518
519
520class ASTStubGenerator(BaseStubGenerator, mypy.traverser.TraverserVisitor):
521 """Generate stub text from a mypy AST."""
522
523 def __init__(
524 self,
525 _all_: list[str] | None = None,
526 include_private: bool = False,
527 analyzed: bool = False,
528 export_less: bool = False,
529 include_docstrings: bool = False,
530 ) -> None:
531 super().__init__(_all_, include_private, export_less, include_docstrings)
532 self._decorators: list[str] = []
533 # Stack of defined variables (per scope).
534 self._vars: list[list[str]] = [[]]
535 # What was generated previously in the stub file.
536 self._state = EMPTY
537 self._class_stack: list[ClassDef] = []
538 # Was the tree semantically analysed before?
539 self.analyzed = analyzed
540 # Short names of methods defined in the body of the current class
541 self.method_names: set[str] = set()
542 self.processing_enum = False
543 self.processing_dataclass = False
544 self.dataclass_field_specifier: tuple[str, ...] = ()
545
546 @property
547 def _current_class(self) -> ClassDef | None:
548 return self._class_stack[-1] if self._class_stack else None
549
550 def visit_mypy_file(self, o: MypyFile) -> None:
551 self.module_name = o.fullname # Current module being processed
552 self.path = o.path
553 self.set_defined_names(find_defined_names(o))
554 self.referenced_names = find_referenced_names(o)
555 super().visit_mypy_file(o)
556 self.check_undefined_names()
557
558 def visit_overloaded_func_def(self, o: OverloadedFuncDef) -> None:
559 """@property with setters and getters, @overload chain and some others."""
560 overload_chain = False
561 for item in o.items:
562 if not isinstance(item, Decorator):
563 continue
564 if self.is_private_name(item.func.name, item.func.fullname):
565 continue
566
567 self.process_decorator(item)
568 if not overload_chain:
569 self.visit_func_def(item.func)
570 if item.func.is_overload:
571 overload_chain = True
572 elif item.func.is_overload:
573 self.visit_func_def(item.func)
574 else:
575 # skip the overload implementation and clear the decorator we just processed
576 self.clear_decorators()
577

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…