Skip to content

[ty] Reject Self in type aliases#25529

Merged
charliermarsh merged 14 commits into
mainfrom
charlie/reject-self
Jun 3, 2026
Merged

[ty] Reject Self in type aliases#25529
charliermarsh merged 14 commits into
mainfrom
charlie/reject-self

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

The spec says: "We reject type aliases containing Self." Some type checkers, such as Pyright, support class-scoped aliases containing Self, but Mypy rejects them.

This change rejects Self in the value of explicit PEP 613 TypeAlias declarations and PEP 695 type statements:

from typing import Self, TypeAlias

class C:
    Alias: TypeAlias = tuple[Self]  # error
    type OtherAlias = tuple[Self]  # error

For rejected PEP 613 aliases, we recover Self as Unknown even when invalid-type-form is disabled, so suppressing the diagnostic does not preserve a receiver-bound Self type.

Implicit aliases, PEP 695 alias type-parameter bounds and defaults, direct TypeAliasType(...) definitions, and Self introduced indirectly through runtime-expression forms remain unchanged for now. Self in runtime-expression positions such as Annotated metadata also remains allowed because those expressions are not part of the alias value type expression.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 1, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 1, 2026

Copy link
Copy Markdown

Typing conformance results

The percentage of diagnostics emitted that were expected errors held steady at 91.94%. The percentage of expected errors that received a diagnostic held steady at 87.09%. The number of fully passing files held steady at 92/134.

Summary

How are test cases classified?

Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (E) are true positives when ty flags the expected location and false negatives when it does not. Optional annotations (E?) are true positives when flagged but true negatives (not false negatives) when not. Tagged annotations (E[tag]) require ty to flag exactly one of the tagged lines; tagged multi-annotations (E[tag+]) allow any number up to the tag count. Flagging unexpected locations counts as a false positive.

Metric Old New Diff Outcome
True Positives 924 924 +0
False Positives 81 81 +0
False Negatives 137 137 +0
Total Diagnostics 1052 1052 +0
Precision 91.94% 91.94% +0.00%
Recall 87.09% 87.09% +0.00%
Passing Files 92/134 92/134 +0

True positives changed (1)

1 diagnostic
Test case Diff

generics_self_usage.py:108

-error[invalid-type-form] Variable of type `<special-form 'typing.Self'>` is not allowed in a type alias value
+error[invalid-type-form] `Self` cannot be used in a type alias

@astral-sh-bot

astral-sh-bot Bot commented Jun 1, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 1, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh charliermarsh force-pushed the charlie/reject-self branch 3 times, most recently from d26485b to 05eda8b Compare June 1, 2026 22:58
@charliermarsh charliermarsh marked this pull request as ready for review June 1, 2026 23:10
@charliermarsh charliermarsh marked this pull request as draft June 1, 2026 23:10
@charliermarsh charliermarsh marked this pull request as ready for review June 1, 2026 23:21
@charliermarsh charliermarsh marked this pull request as draft June 1, 2026 23:21
@charliermarsh charliermarsh force-pushed the charlie/reject-self branch from 05eda8b to 1c8cdc6 Compare June 1, 2026 23:46
@codspeed-hq

codspeed-hq Bot commented Jun 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 65 untouched benchmarks
⏩ 60 skipped benchmarks1


Comparing charlie/reject-self (b1b9584) with main (26ac6f4)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@charliermarsh charliermarsh force-pushed the charlie/reject-self branch 2 times, most recently from 4c79821 to 285be0c Compare June 2, 2026 13:28
@charliermarsh charliermarsh marked this pull request as ready for review June 2, 2026 18:57
@charliermarsh charliermarsh force-pushed the charlie/reject-self branch from efa1f46 to cb013cf Compare June 2, 2026 21:33
@charliermarsh charliermarsh force-pushed the charlie/reject-self branch from cb013cf to dfc67c6 Compare June 3, 2026 02:31

@carljm carljm left a comment

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.

Looks good, thank you!

@charliermarsh charliermarsh force-pushed the charlie/reject-self branch from dfc67c6 to b1b9584 Compare June 3, 2026 02:38
@charliermarsh charliermarsh merged commit 9f7591d into main Jun 3, 2026
58 checks passed
@charliermarsh charliermarsh deleted the charlie/reject-self branch June 3, 2026 02:45
carljm added a commit that referenced this pull request Jun 3, 2026
* origin/main: (114 commits)
  [ty] Detect disjointness due to incompatible generic specializations (#24822)
  Update Rust crate memchr to v2.8.1 (#25586)
  [ty] Reject Self in type aliases (#25529)
  Update Rust crate log to v0.4.30 (#25572)
  Update Rust crate mimalloc to v0.1.52 (#25573)
  Update Rust crate similar to v3.1.1 (#25575)
  Update docker/setup-buildx-action action to v4.1.0 (#25580)
  Update docker/metadata-action action to v6.1.0 (#25579)
  Update docker/login-action action to v4.2.0 (#25578)
  Update docker/build-push-action action to v7.2.0 (#25577)
  Update taiki-e/install-action action to v2.79.9 (#25576)
  Update Rust crate serde_json to v1.0.150 (#25574)
  Update Rust crate assert_fs to v1.1.4 (#25570)
  Update prek dependencies (#25569)
  Update dependency ruff to v0.15.15 (#25568)
  Update dependency pyright to v1.1.410 (#25566)
  Update dependency astral-sh/uv to v0.11.18 (#25565)
  [ty] don't needlessly disambiguate same type alias (#25563)
  [ty] fix variance inference for nested type aliases (#25567)
  [ty] treat union-bound typevars like unions for possibly-missing-attribute (#25561)
  ...

# Conflicts:
#	crates/ty_python_semantic/src/types/class.rs
#	crates/ty_python_semantic/src/types/generics.rs
#	crates/ty_python_semantic/src/types/type_alias.rs
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.

2 participants