docs: add docstrings to types
This commit is contained in:
@@ -10,12 +10,16 @@ from midas.ast.printer import MidasPrinter
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class TopType:
|
class TopType:
|
||||||
|
"""The top type (`Any`)"""
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "Any"
|
return "Any"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class BaseType:
|
class BaseType:
|
||||||
|
"""A base / builtin type"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -24,6 +28,8 @@ class BaseType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class DerivedType:
|
class DerivedType:
|
||||||
|
"""A derived type, i.e. a named subtype of another type"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
type: Type
|
type: Type
|
||||||
|
|
||||||
@@ -33,18 +39,24 @@ class DerivedType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class UnknownType:
|
class UnknownType:
|
||||||
|
"""An unknown type"""
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "<Unknown>"
|
return "<Unknown>"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class UnitType:
|
class UnitType:
|
||||||
|
"""The unit type (`None`)"""
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "None"
|
return "None"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class Function:
|
class Function:
|
||||||
|
"""A function type"""
|
||||||
|
|
||||||
params: ParamSpec
|
params: ParamSpec
|
||||||
returns: Type
|
returns: Type
|
||||||
|
|
||||||
@@ -65,6 +77,8 @@ class Function:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ParamSpec:
|
class ParamSpec:
|
||||||
|
"""A function's parameter spec"""
|
||||||
|
|
||||||
pos: list[Function.Parameter] = field(default_factory=list)
|
pos: list[Function.Parameter] = field(default_factory=list)
|
||||||
mixed: list[Function.Parameter] = field(default_factory=list)
|
mixed: list[Function.Parameter] = field(default_factory=list)
|
||||||
kw: list[Function.Parameter] = field(default_factory=list)
|
kw: list[Function.Parameter] = field(default_factory=list)
|
||||||
@@ -87,6 +101,8 @@ class ParamSpec:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class OverloadedFunction:
|
class OverloadedFunction:
|
||||||
|
"""A list of method overloads"""
|
||||||
|
|
||||||
overloads: list[Type]
|
overloads: list[Type]
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -95,6 +111,8 @@ class OverloadedFunction:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ComplexType:
|
class ComplexType:
|
||||||
|
"""A type with inline members"""
|
||||||
|
|
||||||
members: dict[str, Type]
|
members: dict[str, Type]
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -104,6 +122,8 @@ class ComplexType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ExtensionType:
|
class ExtensionType:
|
||||||
|
"""An extension of a type, adding members through a `ComplexType`"""
|
||||||
|
|
||||||
base: Type
|
base: Type
|
||||||
extension: ComplexType
|
extension: ComplexType
|
||||||
|
|
||||||
@@ -112,6 +132,8 @@ class ExtensionType:
|
|||||||
|
|
||||||
|
|
||||||
class Variance(StrEnum):
|
class Variance(StrEnum):
|
||||||
|
"""The variance of a :class:`TypeVar`"""
|
||||||
|
|
||||||
INVARIANT = "INVARIANT"
|
INVARIANT = "INVARIANT"
|
||||||
COVARIANT = "COVARIANT"
|
COVARIANT = "COVARIANT"
|
||||||
CONTRAVARIANT = "CONTRAVARIANT"
|
CONTRAVARIANT = "CONTRAVARIANT"
|
||||||
@@ -119,6 +141,8 @@ class Variance(StrEnum):
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class TypeVar:
|
class TypeVar:
|
||||||
|
"""A type variable, often used as type parameters for a generic type"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
bound: Optional[Type]
|
bound: Optional[Type]
|
||||||
variance: Variance = Variance.INVARIANT
|
variance: Variance = Variance.INVARIANT
|
||||||
@@ -136,6 +160,8 @@ class TypeVar:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class GenericType:
|
class GenericType:
|
||||||
|
"""A generic type, with type parameters and a generic body type"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
params: list[TypeVar]
|
params: list[TypeVar]
|
||||||
body: Type
|
body: Type
|
||||||
@@ -146,6 +172,8 @@ class GenericType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class AppliedType:
|
class AppliedType:
|
||||||
|
"""An instance of a :class:`GenericType`, with concrete type arguments substituted in its body"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
args: list[Type]
|
args: list[Type]
|
||||||
body: Type
|
body: Type
|
||||||
@@ -156,6 +184,8 @@ class AppliedType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ConstraintType:
|
class ConstraintType:
|
||||||
|
"""A type with a constraint expression"""
|
||||||
|
|
||||||
type: Type
|
type: Type
|
||||||
constraint: m.Expr
|
constraint: m.Expr
|
||||||
|
|
||||||
@@ -166,6 +196,8 @@ class ConstraintType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class TupleType:
|
class TupleType:
|
||||||
|
"""A tuple type, containing any number of ordered item types"""
|
||||||
|
|
||||||
items: tuple[Type, ...]
|
items: tuple[Type, ...]
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -174,6 +206,8 @@ class TupleType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ColumnType:
|
class ColumnType:
|
||||||
|
"""A column type containing items of a given unique type"""
|
||||||
|
|
||||||
type: Type
|
type: Type
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -182,6 +216,8 @@ class ColumnType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class DataFrameType:
|
class DataFrameType:
|
||||||
|
"""A data-frame type, containing named columns of specific :class:`ColumnType`"""
|
||||||
|
|
||||||
columns: list[Column]
|
columns: list[Column]
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -197,6 +233,8 @@ class DataFrameType:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class FrameGroupBy:
|
class FrameGroupBy:
|
||||||
|
"""A frame group-by object"""
|
||||||
|
|
||||||
frame: DataFrameType
|
frame: DataFrameType
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -205,6 +243,8 @@ class FrameGroupBy:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class ColumnGroupBy:
|
class ColumnGroupBy:
|
||||||
|
"""A column group-by object"""
|
||||||
|
|
||||||
column: ColumnType
|
column: ColumnType
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -212,6 +252,19 @@ class ColumnGroupBy:
|
|||||||
|
|
||||||
|
|
||||||
def substitute_typevars(type: Type, substitutions: dict[str, Type]) -> Type:
|
def substitute_typevars(type: Type, substitutions: dict[str, Type]) -> Type:
|
||||||
|
"""Substitute type variables in the given type
|
||||||
|
|
||||||
|
This function is called recursively on inner type structures
|
||||||
|
|
||||||
|
Args:
|
||||||
|
type (Type): the type in which to substitute type variables
|
||||||
|
substitutions (dict[str, Type]): a mapping of type variable names to
|
||||||
|
concrete types
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Type: the resulting type with substitutions applied
|
||||||
|
"""
|
||||||
|
|
||||||
def sub_parameter(param: Function.Parameter):
|
def sub_parameter(param: Function.Parameter):
|
||||||
return Function.Parameter(
|
return Function.Parameter(
|
||||||
pos=param.pos,
|
pos=param.pos,
|
||||||
@@ -354,6 +407,14 @@ def substitute_typevars(type: Type, substitutions: dict[str, Type]) -> Type:
|
|||||||
|
|
||||||
|
|
||||||
def unfold_type(type: Type) -> Type:
|
def unfold_type(type: Type) -> Type:
|
||||||
|
"""Unfold a chain of :class:`DerivedType` to get the root supertype
|
||||||
|
|
||||||
|
Args:
|
||||||
|
type (Type): the type to unfold
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Type: the root supertype
|
||||||
|
"""
|
||||||
match type:
|
match type:
|
||||||
case DerivedType(type=ref_type):
|
case DerivedType(type=ref_type):
|
||||||
return unfold_type(ref_type)
|
return unfold_type(ref_type)
|
||||||
@@ -362,6 +423,15 @@ def unfold_type(type: Type) -> Type:
|
|||||||
|
|
||||||
|
|
||||||
def to_annotation(type: Type) -> str:
|
def to_annotation(type: Type) -> str:
|
||||||
|
"""Convert the given type to a Python annotation string
|
||||||
|
|
||||||
|
Args:
|
||||||
|
type (Type): the type to convert
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: the annotation string
|
||||||
|
"""
|
||||||
|
|
||||||
def _params_annotation(spec: ParamSpec) -> str:
|
def _params_annotation(spec: ParamSpec) -> str:
|
||||||
if len(spec.kw) != 0:
|
if len(spec.kw) != 0:
|
||||||
return "..."
|
return "..."
|
||||||
@@ -430,6 +500,8 @@ def to_annotation(type: Type) -> str:
|
|||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class Predicate:
|
class Predicate:
|
||||||
|
"""A predicate"""
|
||||||
|
|
||||||
type: Type
|
type: Type
|
||||||
body: m.Expr
|
body: m.Expr
|
||||||
alias: bool
|
alias: bool
|
||||||
|
|||||||
Reference in New Issue
Block a user