fix no error on invalid call to __init_subclass__ #3447#3468
Open
asukaminato0721 wants to merge 5 commits into
Open
fix no error on invalid call to __init_subclass__ #3447#3468asukaminato0721 wants to merge 5 commits into
__init_subclass__ #3447#3468asukaminato0721 wants to merge 5 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR adds type-checking for class-header keyword arguments by routing them through __init_subclass__ (excluding the default object.__init_subclass__), and preserves keyword identifier ranges through binding so diagnostics can point at the keyword name in the class header.
Changes:
- Store class-header keywords as
Identifier(not justName) so keyword ranges survive into the solving phase. - Add
__init_subclass__lookup (excludingobject.__init_subclass__) and invoke the existing call checker to validate class-header keywords. - Add a regression test covering an invalid keyword type in a
__init_subclass__call.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pyrefly/lib/test/constructors.rs | Adds a regression test for validating class-header keywords against __init_subclass__. |
| pyrefly/lib/binding/class.rs | Preserves keyword identifiers/ranges in bindings; updates synthesized class keyword storage accordingly. |
| pyrefly/lib/binding/binding.rs | Updates BindingClassMetadata.keywords to store Identifier instead of Name. |
| pyrefly/lib/alt/class/class_metadata.rs | Implements __init_subclass__ keyword checking during class metadata computation. |
| pyrefly/lib/alt/class/class_field.rs | Adds get_dunder_init_subclass helper, excluding object.__init_subclass__. |
Comments suppressed due to low confidence (1)
pyrefly/lib/alt/class/class_metadata.rs:550
- This only checks
__init_subclass__onbases_with_metadata.first(). If the leftmost base doesn't override__init_subclass__but a later base does, class-header keywords won't be validated against the method that will actually be resolved via MRO. Consider scanningbases_with_metadatain order and picking the first base whoseget_dunder_init_subclass(...)returnsSome, instead of unconditionally using the first base.
let Some((base, _)) = bases_with_metadata.first() else {
return;
};
let base = self.promote_nontypeddict_silently_to_classtype(base);
let Some(init_subclass) = self.get_dunder_init_subclass(&base) else {
return;
};
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Summary
Fixes #3447
Added
__init_subclass__lookup excludingobject.__init_subclass__Class metadata now checks class-header keywords against the first real base class’s
__init_subclass__via the existing callable checkerPreserved keyword identifiers through binding so diagnostics can point at class-header keyword names.
Test Plan
add test