Repository navigation
fix no error on invalid call to __init_subclass__ #3447 - #3468
asukaminato0721 wants to merge 2 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.
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.
There was a problem hiding this comment.
Thanks @asukaminato0721, and nice work! The feature itself is a real gap vs. mypy/pyright, so it's worth landing.
This looks good to import but can you be an extra reviewer ? @stroxler :
- MRO resolution: the check resolves init_subclass from bases_with_metadata.first() (walking that base's ancestors). For multiple inheritance where a later base defines init_subclass, that method won't be validated, it'd be more correct to follow the new class's full MRO.
- Metaclass skip breadth: if !keywords.is_empty() && metaclass.is_some() { return; } turns off all keyword validation whenever a custom metaclass exists. That's what fixed manticore, but it also silences real errors for keywords the metaclass doesn't consume. Worth considering filtering only the metaclass-consumed keywords instead.
|
@NathanTempest has imported this pull request. If you are a Meta employee, you can view this in D112153555. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
yangdanny97
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
kinto0
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@NathanTempest merged this pull request in 7cbda17. |
…yrefly-org#3468) Summary: Pyrefly silently accepted invalid class-header keywords (e.g. `class Foo(Bar, bad_kw=1)`) that didn't match the parent's `__init_subclass__` signature. This adds validation by looking up `__init_subclass__` from base classes and checking keyword arguments against it with the existing callable checker. The binding layer now preserves full `Identifier` tokens (not just `Name` strings) so error diagnostics point at the exact keyword in the class header. Pull Request resolved: pyrefly-org#3468 Test Plan: Test suite Reviewed By: stroxler Differential Revision: D112153555 Pulled By: NathanTempest fbshipit-source-id: f892c5f7764d485b008bc393c5af239ca5c0b8fe
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