Skip to content

synt.ty ¤

type_param ¤

tvar module-attribute ¤

tvar = TypeVar

Alias TypeVar.

ttup module-attribute ¤

Alias TypeVarTuple.

tspec module-attribute ¤

Alias TypeParamSpec.

TypeVar ¤

Bases: IntoCode

TypeVar in type parameters.

Examples:

ty_var = tvar(id_("T"), id_("int"))
assert ty_var.into_code() == "T: int"
References

Type parameters.

Source code in synt/ty/type_param.py
class TypeVar(IntoCode):
    r"""`TypeVar` in type parameters.

    Examples:
        ```python
        ty_var = tvar(id_("T"), id_("int"))
        assert ty_var.into_code() == "T: int"
        ```

    References:
        [Type parameters](https://docs.python.org/3/library/ast.html#ast-type-params).
    """

    name: Identifier
    """The name of the type variable."""
    bound: Expression | None
    """The bound of the type variable."""

    def __init__(self, name: Identifier, bound: IntoExpression | None = None):
        """Initialize a type variable.

        Args:
            name: The name of the type variable.
            bound: The bound of the type variable.
        """
        self.name = name
        if bound is not None:
            self.bound = bound.into_expression()
        else:
            self.bound = None

    def into_code(self) -> str:
        bound = f": {self.bound.into_code()}" if self.bound is not None else ""
        return f"{self.name.into_code()}{bound}"

bound instance-attribute ¤

bound: Expression | None

The bound of the type variable.

name instance-attribute ¤

name: Identifier = name

The name of the type variable.

__init__ ¤

1
2
3
__init__(
    name: Identifier, bound: IntoExpression | None = None
)

Initialize a type variable.

Parameters:

Name Type Description Default
name Identifier

The name of the type variable.

required
bound IntoExpression | None

The bound of the type variable.

None
Source code in synt/ty/type_param.py
def __init__(self, name: Identifier, bound: IntoExpression | None = None):
    """Initialize a type variable.

    Args:
        name: The name of the type variable.
        bound: The bound of the type variable.
    """
    self.name = name
    if bound is not None:
        self.bound = bound.into_expression()
    else:
        self.bound = None

into_code ¤

into_code() -> str
Source code in synt/ty/type_param.py
def into_code(self) -> str:
    bound = f": {self.bound.into_code()}" if self.bound is not None else ""
    return f"{self.name.into_code()}{bound}"

TypeVarTuple ¤

Bases: IntoCode

Type variable tuple.

Examples:

ty_var = ttup(id_("T"))
assert ty_var.into_code() == "*T"
References

Type variable tuple.

Source code in synt/ty/type_param.py
class TypeVarTuple(IntoCode):
    r"""Type variable tuple.

    Examples:
        ```python
        ty_var = ttup(id_("T"))
        assert ty_var.into_code() == "*T"
        ```

    References:
        [Type variable tuple](https://docs.python.org/3/library/ast.html#ast.TypeVarTuple).
    """

    name: Identifier
    """The name of the type variable tuple."""

    def __init__(self, name: Identifier):
        """Initialize a type variable tuple.

        Args:
            name: The name of the type variable tuple.
        """
        self.name = name

    def into_code(self) -> str:
        return f"*{self.name.into_code()}"

name instance-attribute ¤

name: Identifier = name

The name of the type variable tuple.

__init__ ¤

__init__(name: Identifier)

Initialize a type variable tuple.

Parameters:

Name Type Description Default
name Identifier

The name of the type variable tuple.

required
Source code in synt/ty/type_param.py
def __init__(self, name: Identifier):
    """Initialize a type variable tuple.

    Args:
        name: The name of the type variable tuple.
    """
    self.name = name

into_code ¤

into_code() -> str
Source code in synt/ty/type_param.py
def into_code(self) -> str:
    return f"*{self.name.into_code()}"

TypeParamSpec ¤

Bases: IntoCode

Type parameter spec.

Examples:

ty_var = tspec(id_("P"))
assert ty_var.into_code() == "**P"
References

Type param spec.

Source code in synt/ty/type_param.py
class TypeParamSpec(IntoCode):
    r"""Type parameter spec.

    Examples:
        ```python
        ty_var = tspec(id_("P"))
        assert ty_var.into_code() == "**P"
        ```

    References:
        [Type param spec](https://docs.python.org/3/library/ast.html#ast.ParamSpec).
    """

    name: Identifier
    """The name of the type variable tuple."""

    def __init__(self, name: Identifier):
        """Initialize a type parameter spec.

        Args:
            name: The name of the type parameter spec.
        """
        self.name = name

    def into_code(self) -> str:
        return f"**{self.name.into_code()}"

name instance-attribute ¤

name: Identifier = name

The name of the type variable tuple.

__init__ ¤

__init__(name: Identifier)

Initialize a type parameter spec.

Parameters:

Name Type Description Default
name Identifier

The name of the type parameter spec.

required
Source code in synt/ty/type_param.py
def __init__(self, name: Identifier):
    """Initialize a type parameter spec.

    Args:
        name: The name of the type parameter spec.
    """
    self.name = name

into_code ¤

into_code() -> str
Source code in synt/ty/type_param.py
def into_code(self) -> str:
    return f"**{self.name.into_code()}"