Source code for stubpy.analysis.symbols.models

"""
Symbol classes: the typed entries of a :class:`~stubpy.SymbolTable`.
"""
from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Any

from stubpy.analysis.harvest.models import ClassInfo, FunctionInfo, TypeVarInfo, VariableInfo


[docs] class SymbolKind(Enum): """Category of a stub symbol. Examples -------- >>> SymbolKind("class") is SymbolKind.CLASS True """ CLASS = "class" FUNCTION = "function" VARIABLE = "variable" ALIAS = "alias" OVERLOAD = "overload"
@dataclass class StubSymbol: """Base class for all symbols. Attributes ---------- name : str Symbol name. kind : SymbolKind Symbol category. lineno : int Source line of the definition; symbols are emitted in this order. branch : tuple The version branches (``(VersionBlock, index)`` pairs) the definition sits in; ``()`` when unconditional. dormant : bool The running interpreter does not take *branch*, so there is no live object. Examples -------- >>> StubSymbol(name="x", kind=SymbolKind.VARIABLE, lineno=1).dormant False """ name: str kind: SymbolKind lineno: int branch: tuple = () dormant: bool = False
[docs] @dataclass class ClassSymbol(StubSymbol): """A class definition. Parameters ---------- name : str Class name. lineno : int Source line of the definition. live_type : type, optional The runtime class; ``None`` when the module was not imported. Defaults to None. ast_info : ClassInfo, optional Metadata from the AST pre-pass. Defaults to None. Examples -------- >>> ClassSymbol(name="Widget", lineno=5).kind <SymbolKind.CLASS: 'class'> """ live_type: type | None = None ast_info: ClassInfo | None = None def __init__( self, name: str, lineno: int, live_type: type | None = None, ast_info: ClassInfo | None = None, ) -> None: super().__init__(name=name, kind=SymbolKind.CLASS, lineno=lineno) self.live_type = live_type self.ast_info = ast_info
[docs] @dataclass class FunctionSymbol(StubSymbol): """A top-level function definition. Parameters ---------- name : str Function name. lineno : int Source line of the definition. live_func : callable, optional The runtime function; ``None`` when the module was not imported. Defaults to None. ast_info : FunctionInfo, optional Metadata from the AST pre-pass. Defaults to None. is_async : bool, optional Taken from *ast_info* when given. Defaults to False. Examples -------- >>> FunctionSymbol(name="greet", lineno=3, is_async=True).is_async True """ live_func: Any | None = None ast_info: FunctionInfo | None = None is_async: bool = False def __init__( self, name: str, lineno: int, live_func: Any | None = None, ast_info: FunctionInfo | None = None, is_async: bool = False, ) -> None: super().__init__(name=name, kind=SymbolKind.FUNCTION, lineno=lineno) self.live_func = live_func self.ast_info = ast_info self.is_async = ast_info.is_async if ast_info is not None else is_async
[docs] @dataclass class VariableSymbol(StubSymbol): """A module-level variable. Parameters ---------- name : str Variable name. lineno : int Source line of the assignment. live_value : Any, optional The runtime value; ``None`` when the module was not imported. Defaults to None. annotation_str : str, optional Annotation from the source. Defaults to None. inferred_type_str : str, optional Type inferred from the value when there is no annotation. Defaults to None. ast_info : VariableInfo, optional Metadata from the AST pre-pass. Defaults to None. Examples -------- >>> VariableSymbol(name="MAX", lineno=1, annotation_str="int").effective_type_str 'int' """ live_value: Any = None annotation_str: str | None = None inferred_type_str: str | None = None ast_info: VariableInfo | None = None def __init__( self, name: str, lineno: int, live_value: Any = None, annotation_str: str | None = None, inferred_type_str: str | None = None, ast_info: VariableInfo | None = None, ) -> None: super().__init__(name=name, kind=SymbolKind.VARIABLE, lineno=lineno) self.live_value = live_value self.annotation_str = annotation_str self.inferred_type_str = inferred_type_str self.ast_info = ast_info @property def effective_type_str(self) -> str | None: """The annotation if present, else the inferred type. Returns ------- str or None The type text, or ``None`` when neither is known. Examples -------- >>> VariableSymbol(name="N", lineno=1, inferred_type_str="int").effective_type_str 'int' """ return self.annotation_str or self.inferred_type_str
[docs] @dataclass class AliasSymbol(StubSymbol): """A ``TypeAlias``, ``TypeVar``, ``ParamSpec``, ``TypeVarTuple`` or ``NewType`` declaration. Parameters ---------- name : str Declared name. lineno : int Source line of the declaration. ast_info : TypeVarInfo, optional Metadata from the AST pre-pass. Defaults to None. live_obj : Any, optional The runtime object, if available. Defaults to None. Examples -------- >>> AliasSymbol(name="T", lineno=2).kind <SymbolKind.ALIAS: 'alias'> """ ast_info: TypeVarInfo | None = None live_obj: Any = None def __init__( self, name: str, lineno: int, ast_info: TypeVarInfo | None = None, live_obj: Any = None, ) -> None: super().__init__(name=name, kind=SymbolKind.ALIAS, lineno=lineno) self.ast_info = ast_info self.live_obj = live_obj
[docs] @dataclass class OverloadGroup(StubSymbol): """The ``@overload`` variants sharing one function name. Parameters ---------- name : str The overloaded function's name. lineno : int Source line of the first variant. variants : list of FunctionSymbol, optional One entry per ``@overload`` definition, in source order. Defaults to an empty list. live_func : callable, optional The runtime implementation. Defaults to None. Examples -------- >>> OverloadGroup(name="parse", lineno=7).variants [] """ variants: list[FunctionSymbol] = field(default_factory=list) live_func: Any | None = None def __init__( self, name: str, lineno: int, variants: list[FunctionSymbol] | None = None, live_func: Any | None = None, ) -> None: super().__init__(name=name, kind=SymbolKind.OVERLOAD, lineno=lineno) self.variants = variants if variants is not None else [] self.live_func = live_func