Skip to content

Depth

Rule limiting nested type subscripts.

pynudger.rule.depth.Depth

Bases: Check, Type

Rule checking type expressions with deeply nested subscripts.

pynudger.rule.depth.Depth.values

values()

Yield the subscript depth of every explicit type root.

Yields:

Type Description
Iterable[Value[int]]

Maximum nested subscript depth for each explicit type root.

Source code in src/pynudger/rule/depth.py
def values(self) -> collections.abc.Iterable[lintkit.Value[int]]:
    """Yield the subscript depth of every explicit type root.

    Yields:
        Maximum nested subscript depth for each explicit type root.

    """
    for root in super().nodes():
        yield lintkit.Value.from_python(_depth(root), root)

pynudger.rule.depth.Depth.check

check(value)

Report subscript depths above the configured maximum.

Parameters:

Name Type Description Default
value Value[int]

Maximum nested subscript depth in the type root.

required

Returns:

Type Description
bool

True when the type depth exceeds the configured maximum.

Source code in src/pynudger/rule/depth.py
def check(self, value: lintkit.Value[int]) -> bool:
    """Report subscript depths above the configured maximum.

    Args:
        value:
            Maximum nested subscript depth in the type root.

    Returns:
        True when the type depth exceeds the configured maximum.

    """
    return value > self._max_depth()

pynudger.rule.depth.Depth.message

message(value)

Describe a type root that exceeds the depth limit.

Parameters:

Name Type Description Default
value Value[int]

Maximum nested subscript depth in the type root.

required

Returns:

Type Description
str

Diagnostic containing the actual and maximum type depths.

Source code in src/pynudger/rule/depth.py
def message(self, value: lintkit.Value[int]) -> str:
    """Describe a type root that exceeds the depth limit.

    Args:
        value:
            Maximum nested subscript depth in the type root.

    Returns:
        Diagnostic containing the actual and maximum type depths.

    """
    return (
        f"Type has depth {value}, which exceeds the maximum of "
        f"{self._max_depth()}."
    )

pynudger.rule.depth.Depth.description

description()

Return the public rule description.

Returns:

Type Description
str

Description of the nested type depth limit.

Source code in src/pynudger/rule/depth.py
def description(self) -> str:
    """Return the public rule description.

    Returns:
        Description of the nested type depth limit.

    """
    return "Avoid deeply nested types."

pynudger.rule.depth.Depth.nodes

nodes()

Yield explicit type-expression roots in grouped AST order.

Yields:

Type Description
Iterable[expr]

Annotation, return, alias, and type-variable bound expressions.

Source code in src/pynudger/_loader.py
def nodes(self) -> collections.abc.Iterable[ast.expr]:
    """Yield explicit type-expression roots in grouped AST order.

    Yields:
        Annotation, return, alias, and type-variable bound expressions.

    """
    maybe_annotated = itertools.chain.from_iterable(
        self.getitem("nodes_map")[t] for t in [ast.AnnAssign, ast.arg]
    )
    maybe_returns = itertools.chain.from_iterable(
        self.getitem("nodes_map")[t]
        for t in [ast.FunctionDef, ast.AsyncFunctionDef]
    )
    maybe_bound = self.getitem("nodes_map")[ast.TypeVar]
    maybe_value = self.getitem("nodes_map")[ast.TypeAlias]

    for field, nodes in zip(
        ("annotation", "returns", "bound", "value"),
        (maybe_annotated, maybe_returns, maybe_bound, maybe_value),
        strict=True,
    ):
        for node in nodes:
            if (value := getattr(node, field)) is not None:
                yield value

pynudger.rule.depth.Depth.unpack

unpack(node)

Extract the value from a node.

Parameters:

Name Type Description Default
node AST

The AST node to extract the value from.

required

Returns:

Type Description
Any

The value loaded from the node.

Source code in src/pynudger/_loader.py
def unpack(self, node: ast.AST) -> typing.Any:
    """Extract the value from a node.

    Args:
        node:
            The AST node to extract the value from.

    Returns:
        The value loaded from the node.

    """
    return node.name  # pyright: ignore[reportAttributeAccessIssue]