Skip to content

Repetition

Rules for repeated name words in declarations.

pynudger.rule.repetition.RepetitionVariable

Bases: _Repetition, GlobalDefinition

Rule checking module-scope variable binding names.

pynudger.rule.repetition.RepetitionVariable.values

values()

Yield module-scope variable binding names.

Yields:

Type Description
Iterable[Value[str]]

Variable identifiers with their declaration locations.

Source code in src/pynudger/rule/repetition.py
def values(self) -> collections.abc.Iterable[lintkit.Value[str]]:
    """Yield module-scope variable binding names.

    Yields:
        Variable identifiers with their declaration locations.

    """
    for node in super().nodes():
        if isinstance(node, ast.Name):
            yield lintkit.Value.from_python(node.id, node)

pynudger.rule.repetition.RepetitionVariable.nodes

nodes()

Return declarations from module-level control flow.

Returns:

Type Description
Iterable[GlobalDefinitionNode]

Iterable of declarations outside class and function bodies.

Source code in src/pynudger/_loader.py
def nodes(
    self,
) -> collections.abc.Iterable[_types.GlobalDefinitionNode]:
    """Return declarations from module-level control flow.

    Returns:
        Iterable of declarations outside class and function bodies.

    """
    tree: ast.Module = self.getitem("ast")
    return _node.global_nodes(tree)

pynudger.rule.repetition.RepetitionVariable.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]

pynudger.rule.repetition.RepetitionVariable.check

check(value)

Report identifiers containing the module name as complete words.

Parameters:

Name Type Description Default
value Value[str]

Identifier name to check.

required

Returns:

Type Description
bool

True when the module name is a non-exact, contiguous word sequence

bool

in the identifier.

Source code in src/pynudger/rule/repetition.py
def check(self, value: lintkit.Value[str]) -> bool:
    """Report identifiers containing the module name as complete words.

    Args:
        value:
            Identifier name to check.

    Returns:
        True when the module name is a non-exact, contiguous word sequence
        in the identifier.
    """
    module_name = self.file.resolve().stem  # pyright: ignore[reportAttributeAccessIssue]
    identifier = "_".join(
        word.casefold() for word in _split(self.kind, value)
    )
    return (
        module_name != identifier
        and f"_{module_name}_" in f"_{identifier}_"
    )

pynudger.rule.repetition.RepetitionVariable.message

message(value)

Describe the repeated module name.

Parameters:

Name Type Description Default
value Value[str]

Identifier name that repeats the module name.

required

Returns:

Type Description
str

Diagnostic message containing both names.

Source code in src/pynudger/rule/repetition.py
def message(self, value: lintkit.Value[str]) -> str:
    """Describe the repeated module name.

    Args:
        value:
            Identifier name that repeats the module name.

    Returns:
        Diagnostic message containing both names.
    """
    return (
        f"{self.kind.title()} '{value}' repeats module name "
        f"'{self.file.resolve().stem}'."  # pyright: ignore[reportAttributeAccessIssue]
    )

pynudger.rule.repetition.RepetitionVariable.description

description()

Return the public description of the rule.

Returns:

Type Description
str

Description of the identifier category checked by this rule.

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

    Returns:
        Description of the identifier category checked by this rule.
    """
    return f"Avoid repeating module name in {self.kind}."

pynudger.rule.repetition.RepetitionClass

Bases: _Repetition, Class

Rule checking class names in all scopes.

pynudger.rule.repetition.RepetitionClass.nodes

nodes()

Return class definition nodes in their existing loader order.

Returns:

Type Description
Iterable[ClassDef]

Iterable of ast.ClassDef nodes.

Source code in src/pynudger/_loader.py
def nodes(self) -> collections.abc.Iterable[ast.ClassDef]:
    """Return class definition nodes in their existing loader order.

    Returns:
        Iterable of ``ast.ClassDef`` nodes.

    """
    return self.getitem("nodes_map")[ast.ClassDef]

pynudger.rule.repetition.RepetitionClass.values

values()

Wrap values from the configured AST nodes.

Yields:

Type Description
Iterable[Value[Any]]

Values extracted from the configured AST nodes.

Source code in src/pynudger/_loader.py
def values(
    self,
) -> collections.abc.Iterable[lintkit.Value[typing.Any]]:
    """Wrap values from the configured AST nodes.

    Yields:
        Values extracted from the configured AST nodes.

    """
    for node in self.nodes():
        yield lintkit.Value.from_python(self.unpack(node), node)

pynudger.rule.repetition.RepetitionClass.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]

pynudger.rule.repetition.RepetitionClass.check

check(value)

Report identifiers containing the module name as complete words.

Parameters:

Name Type Description Default
value Value[str]

Identifier name to check.

required

Returns:

Type Description
bool

True when the module name is a non-exact, contiguous word sequence

bool

in the identifier.

Source code in src/pynudger/rule/repetition.py
def check(self, value: lintkit.Value[str]) -> bool:
    """Report identifiers containing the module name as complete words.

    Args:
        value:
            Identifier name to check.

    Returns:
        True when the module name is a non-exact, contiguous word sequence
        in the identifier.
    """
    module_name = self.file.resolve().stem  # pyright: ignore[reportAttributeAccessIssue]
    identifier = "_".join(
        word.casefold() for word in _split(self.kind, value)
    )
    return (
        module_name != identifier
        and f"_{module_name}_" in f"_{identifier}_"
    )

pynudger.rule.repetition.RepetitionClass.message

message(value)

Describe the repeated module name.

Parameters:

Name Type Description Default
value Value[str]

Identifier name that repeats the module name.

required

Returns:

Type Description
str

Diagnostic message containing both names.

Source code in src/pynudger/rule/repetition.py
def message(self, value: lintkit.Value[str]) -> str:
    """Describe the repeated module name.

    Args:
        value:
            Identifier name that repeats the module name.

    Returns:
        Diagnostic message containing both names.
    """
    return (
        f"{self.kind.title()} '{value}' repeats module name "
        f"'{self.file.resolve().stem}'."  # pyright: ignore[reportAttributeAccessIssue]
    )

pynudger.rule.repetition.RepetitionClass.description

description()

Return the public description of the rule.

Returns:

Type Description
str

Description of the identifier category checked by this rule.

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

    Returns:
        Description of the identifier category checked by this rule.
    """
    return f"Avoid repeating module name in {self.kind}."

pynudger.rule.repetition.RepetitionFunction

Bases: _Repetition, Function

Rule checking function names in all scopes.

pynudger.rule.repetition.RepetitionFunction.nodes

nodes()

Yield function definition nodes in their existing loader order.

Returns:

Type Description
Iterable[FunctionNode]

Synchronous definitions followed by asynchronous definitions.

Source code in src/pynudger/_loader.py
def nodes(
    self,
) -> collections.abc.Iterable[_types.FunctionNode]:
    """Yield function definition nodes in their existing loader order.

    Returns:
        Synchronous definitions followed by asynchronous definitions.

    """
    return itertools.chain.from_iterable(
        self.getitem("nodes_map")[t]
        for t in [ast.FunctionDef, ast.AsyncFunctionDef]
    )

pynudger.rule.repetition.RepetitionFunction.values

values()

Wrap values from the configured AST nodes.

Yields:

Type Description
Iterable[Value[Any]]

Values extracted from the configured AST nodes.

Source code in src/pynudger/_loader.py
def values(
    self,
) -> collections.abc.Iterable[lintkit.Value[typing.Any]]:
    """Wrap values from the configured AST nodes.

    Yields:
        Values extracted from the configured AST nodes.

    """
    for node in self.nodes():
        yield lintkit.Value.from_python(self.unpack(node), node)

pynudger.rule.repetition.RepetitionFunction.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]

pynudger.rule.repetition.RepetitionFunction.check

check(value)

Report identifiers containing the module name as complete words.

Parameters:

Name Type Description Default
value Value[str]

Identifier name to check.

required

Returns:

Type Description
bool

True when the module name is a non-exact, contiguous word sequence

bool

in the identifier.

Source code in src/pynudger/rule/repetition.py
def check(self, value: lintkit.Value[str]) -> bool:
    """Report identifiers containing the module name as complete words.

    Args:
        value:
            Identifier name to check.

    Returns:
        True when the module name is a non-exact, contiguous word sequence
        in the identifier.
    """
    module_name = self.file.resolve().stem  # pyright: ignore[reportAttributeAccessIssue]
    identifier = "_".join(
        word.casefold() for word in _split(self.kind, value)
    )
    return (
        module_name != identifier
        and f"_{module_name}_" in f"_{identifier}_"
    )

pynudger.rule.repetition.RepetitionFunction.message

message(value)

Describe the repeated module name.

Parameters:

Name Type Description Default
value Value[str]

Identifier name that repeats the module name.

required

Returns:

Type Description
str

Diagnostic message containing both names.

Source code in src/pynudger/rule/repetition.py
def message(self, value: lintkit.Value[str]) -> str:
    """Describe the repeated module name.

    Args:
        value:
            Identifier name that repeats the module name.

    Returns:
        Diagnostic message containing both names.
    """
    return (
        f"{self.kind.title()} '{value}' repeats module name "
        f"'{self.file.resolve().stem}'."  # pyright: ignore[reportAttributeAccessIssue]
    )

pynudger.rule.repetition.RepetitionFunction.description

description()

Return the public description of the rule.

Returns:

Type Description
str

Description of the identifier category checked by this rule.

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

    Returns:
        Description of the identifier category checked by this rule.
    """
    return f"Avoid repeating module name in {self.kind}."

pynudger.rule.repetition.Name

Bases: Check, GlobalDefinition

Rule checking shared words in module-scope declaration names.

pynudger.rule.repetition.Name.values

values()

Yield every declaration-word candidate with its total count.

Yields:

Type Description
Iterable[Value[_Candidate]]

Every unique declaration-word pair with the word's module-wide

Iterable[Value[_Candidate]]

occurrence count.

Source code in src/pynudger/rule/repetition.py
def values(
    self,
) -> collections.abc.Iterable[lintkit.Value[_Candidate]]:
    """Yield every declaration-word candidate with its total count.

    Yields:
        Every unique declaration-word pair with the word's module-wide
        occurrence count.

    """
    candidates = [
        candidate
        for node in super().nodes()
        for candidate in self._split_node(node)
    ]
    counts = collections.Counter(word for _, word in candidates)
    for node, word in candidates:
        name = node.id if isinstance(node, ast.Name) else node.name
        candidate = _Candidate(
            name=name,
            label=self._label(node),
            word=word,
            occurrences=counts[word],
        )
        yield lintkit.Value.from_python(candidate, node)

pynudger.rule.repetition.Name.check

check(value)

Report candidates meeting the configured occurrence minimum.

Parameters:

Name Type Description Default
value Value[_Candidate]

Declaration-word candidate with its module-wide count.

required

Returns:

Type Description
bool

Whether the candidate reaches the configured minimum.

Source code in src/pynudger/rule/repetition.py
def check(self, value: lintkit.Value[_Candidate]) -> bool:
    """Report candidates meeting the configured occurrence minimum.

    Args:
        value:
            Declaration-word candidate with its module-wide count.

    Returns:
        Whether the candidate reaches the configured minimum.

    """
    return value.occurrences >= self.config(
        "minimum_same_name_occurrences", 2
    )

pynudger.rule.repetition.Name.message

message(value)

Describe the module that should own the declaration.

Parameters:

Name Type Description Default
value Value[_Candidate]

Repeated declaration-word candidate.

required

Returns:

Type Description
str

Diagnostic with the declaration kind, name, and module name.

Source code in src/pynudger/rule/repetition.py
def message(self, value: lintkit.Value[_Candidate]) -> str:
    """Describe the module that should own the declaration.

    Args:
        value:
            Repeated declaration-word candidate.

    Returns:
        Diagnostic with the declaration kind, name, and module name.

    """
    return (
        f"{value.label} '{value.name}' should be placed under module "
        f"'{value.word}'."
    )

pynudger.rule.repetition.Name.description

description()

Return the public rule description.

Returns:

Type Description
str

Description of the module-grouping rule.

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

    Returns:
        Description of the module-grouping rule.

    """
    return "Group globals with shared name words under modules."

pynudger.rule.repetition.Name.nodes

nodes()

Return declarations from module-level control flow.

Returns:

Type Description
Iterable[GlobalDefinitionNode]

Iterable of declarations outside class and function bodies.

Source code in src/pynudger/_loader.py
def nodes(
    self,
) -> collections.abc.Iterable[_types.GlobalDefinitionNode]:
    """Return declarations from module-level control flow.

    Returns:
        Iterable of declarations outside class and function bodies.

    """
    tree: ast.Module = self.getitem("ast")
    return _node.global_nodes(tree)

pynudger.rule.repetition.Name.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]