Skip to content

[ty] Emit invalid type form for PEP 695 aliases#24224

Closed
charliermarsh wants to merge 2 commits intomainfrom
charlie/tf
Closed

[ty] Emit invalid type form for PEP 695 aliases#24224
charliermarsh wants to merge 2 commits intomainfrom
charlie/tf

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

Closes astral-sh/ty#3150.

@astral-sh-bot astral-sh-bot bot added the ty Multi-file analysis & type inference label Mar 26, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 26, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 86.59%. The percentage of expected errors that received a diagnostic held steady at 80.96%. The number of fully passing files held steady at 68/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 26, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 26, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-await 40 0 0
invalid-return-type 1 0 0
Total 41 0 0

Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.

Full report with detailed diff (timing results)

type Bad6 = NotRequired[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad7 = ReadOnly[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad8 = InitVar[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
# error: [invalid-type-form] "`InitVar` may not be used without a type argument"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't great, we don't show this for bad10: TypeAlias = InitVar.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by sharing the codepaths.

@charliermarsh charliermarsh marked this pull request as ready for review March 27, 2026 00:54
@AlexWaygood
Copy link
Copy Markdown
Member

I think there's a much simpler solution here, give me a second.

@AlexWaygood
Copy link
Copy Markdown
Member

See #24242. Ideally I think we would do our PEP-613 validation more similarly to how we do our PEP-695 validation, but that throws up other issues...

@charliermarsh
Copy link
Copy Markdown
Member Author

Just to clarify, you prefer #24242?

Copy link
Copy Markdown
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality looks good here, just some nits about how we name and comment things.

Comment on lines +53 to +61
type Bad1 = ClassVar[str] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad2 = ClassVar # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad3 = Final[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad4 = Final # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad5 = Required[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad6 = NotRequired[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad7 = ReadOnly[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad8 = InitVar[int] # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
type Bad9 = InitVar # error: [invalid-type-form] "Type qualifiers are not allowed in type alias definitions"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already have equivalent test for TypeAlias aliases?

/// Controls the top-level semantics used when interpreting an annotation expression.
///
/// PEP 695 type-alias values mostly reuse annotation-expression inference, but a few
/// top-level special cases such as bare `InitVar` should behave like alias RHSs rather
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what "behave like alias RHSs" means here. Don't we just mean to say that most type qualifier special forms are prohibited in PEP 695 alias right hand sides?

Comment on lines +27 to +28
/// Annotation semantics for `x: TypeAlias = ...`.
Pep613TypeAlias,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading if treated as parallel to TypeAliasValue. This is not used for "right hand side of a PEP 613 type alias", it's used for annotated assignments, where an annotation of TypeAlias is allowed (vs other annotation contexts where it is not).

Not clear to me entirely if we should name this AnnotatedAssignment or PEP613AnnotationAllowed -- the former emphasizes when it is used, the latter emphasizes what difference it makes. I guess I kind of learn toward the former?

Suggested change
/// Annotation semantics for `x: TypeAlias = ...`.
Pep613TypeAlias,
/// The annotation of an annotated assignment statement.
AnnotatedAssignment,

Comment on lines +29 to +30
/// Annotation semantics for `type X = ...`.
TypeAliasValue,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits on naming here: "TypeAlias" suggests PEP 613, if we don't say PEP695, and "Value" mildly suggests "as a value expression", which is not what we're doing here.

Suggested change
/// Annotation semantics for `type X = ...`.
TypeAliasValue,
/// Annotation semantics for the right-hand-side of `type X = ...`.
PEP695AliasRHS,

}

/// Infer the type of a PEP 695 type alias value.
pub(super) fn infer_type_alias_value_expression(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(super) fn infer_type_alias_value_expression(
pub(super) fn infer_pep_695_alias_rhs(

@carljm
Copy link
Copy Markdown
Contributor

carljm commented Mar 27, 2026

Oops, had this open in a tab on "changes" view from last night and missed Alex's comments.

@charliermarsh
Copy link
Copy Markdown
Member Author

Yeah Alex and I have dueling solutions now. Unfortunately I have to assume his is better lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing error for Final in top level of type alias statement

3 participants