Skip to content

aliases_implicit: TypeAlias as X imports resolved by substring scan of raw source, duplicating the real name cascade #412

Description

@MelbourneDeveloper

Split out of #408. Confirmed live on main @ da74283 and on the bidirectionaltype-inference branch.

The code

crates/basilisk-checker/src/rules/aliases_implicit.rs:69-83

// Scan the raw import source text for `TypeAlias as <alias>` patterns.
let Some(import_text) = slice_span(&module.source, import.span) else { continue };
for (pos, marker) in import_text.match_indices("TypeAlias as ") {
    let after = &import_text[pos + marker.len()..];
    let alias: String = after
        .chars()
        .take_while(|c| c.is_alphanumeric() || *c == '_')
        .collect();
    if !alias.is_empty() && alias != "TypeAlias" {
        names.push(alias);
    }
}

Import aliases are recovered by substring-matching raw source text, against the repo rule "avoid regex to parse anything, use ruff". Ruff already gives alias.name and alias.asname as structured fields on the import node.

Failing cases

The literal marker "TypeAlias as " requires exactly one space on each side of as. Any other legal spelling silently fails, and the resulting alias name is never registered — so aliases_implicit skips every variable annotated with it:

from typing import TypeAlias as  TA        # two spaces
X: TA = [int, str]                         # not flagged

With two spaces the marker still matches, but after begins with a space, take_while yields the empty string, and the alias is dropped.

from typing import (
    TypeAlias as
    TA,
)
Y: TA = True                               # not flagged

Here as is followed by a newline, the marker never matches at all, and TA is unknown.

Because the scanner has no notion of tokens, it also cannot distinguish code from string or comment content inside the span.

Duplication

A second, independent resolution of the same spellings already exists at assignment_compatibility/mod.rs:296-301, which resolves TypeAlias, typing.TypeAlias, t.TypeAlias, and TypeAlias as TA through the name cascade — structurally, and correctly. The rule reimplements it worse.

Fix

Delete the scanner and resolve TypeAlias through the existing name cascade, reading asname from the import node. This removes the duplication flagged by the repo's DRY rule at the same time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    spec-violationCode functionality/logic does not match its spec (see SPEC-CONFORMANCE-AUDIT-PLAN.md)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions