fix(typing): resolve invalid Self to Any during annotation solving to prevent internal-error#3014
Closed
Arths17 wants to merge 1 commit intofacebook:mainfrom
Closed
fix(typing): resolve invalid Self to Any during annotation solving to prevent internal-error#3014Arths17 wants to merge 1 commit intofacebook:mainfrom
Arths17 wants to merge 1 commit intofacebook:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a type-checker internal error caused by typing.Self leaking into later inference phases when used invalidly (outside a class context), by normalizing unresolved Self special forms to an error-Any after class-context substitution.
Changes:
- Update the annotation solving path to replace any remaining
SpecialForm::SelfTypewithAny(error) to prevent downstream undefined states. - Add a regression test covering the classmethod “trampoline” pattern from Issue #3008 to ensure the diagnostic is emitted without an internal error.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyrefly/lib/alt/solve.rs |
Normalizes dangling Self special forms to error-Any during annotation solving to avoid internal errors in later inference. |
pyrefly/lib/test/typing_self.rs |
Adds a regression test reproducing the Issue #3008 pattern (invalid Self outside class + attribute access) to ensure no internal error occurs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
stroxler
approved these changes
Apr 6, 2026
Contributor
stroxler
left a comment
There was a problem hiding this comment.
Looks good to me, I'll try to get a second review / get it merged. Thanks!
Contributor
yangdanny97
approved these changes
Apr 6, 2026
Contributor
yangdanny97
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves Issue #3008, which reported a TODO internal error in Expr::attr_infer_for_type. The root cause was that typing.Self, when used invalidly outside of a class context, was being reported by the binding phase but was still "leaking" as an unresolved special form into the inference engine.
When the engine attempted an attribute lookup on this raw Self form, it hit an undefined state. This patch ensures that any Self forms remaining after class-context substitution are collapsed to Any (Error type), allowing the type-checker to proceed gracefully using the already-emitted diagnostic.
Changes
Validation Results
This pull request addresses a regression related to the handling of the
Selfspecial form in type annotations, particularly when it appears outside of a class context. The main fix ensures that invalid uses ofSelfare replaced withAnyto prevent internal errors in later phases. Additionally, a regression test is added to verify the fix.Type system robustness:
AnswersSolverinsolve.rsto replace unresolvedSelfspecial forms withAnyduring annotation processing, preventing internal errors whenSelfis used outside a class.Testing:
typing_self.rsto ensure that usingSelfoutside a class (e.g., via a classmethod trampoline) does not cause internal errors, referencing issue TODO: Expr::attr_infer_for_type attribute base undefined for type: Self #3008.