Skip to content

fix no error on invalid call to __init_subclass__ #3447 - #3468

Closed
asukaminato0721 wants to merge 2 commits into
pyrefly-org:mainfrom
asukaminato0721:3447
Closed

asukaminato0721 wants to merge 2 commits into
pyrefly-org:mainfrom
asukaminato0721:3447

Conversation

@asukaminato0721

@asukaminato0721 asukaminato0721 commented May 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #3447

Added __init_subclass__ lookup excluding object.__init_subclass__

Class metadata now checks class-header keywords against the first real base class’s __init_subclass__ via the existing callable checker

Preserved keyword identifiers through binding so diagnostics can point at class-header keyword names.

Test Plan

add test

@github-actions

This comment has been minimized.

@github-actions github-actions Bot added size/m and removed size/m labels May 19, 2026
@github-actions

This comment has been minimized.

@asukaminato0721
asukaminato0721 marked this pull request as ready for review May 19, 2026 18:53
Copilot AI review requested due to automatic review settings May 19, 2026 18:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 just Name) so keyword ranges survive into the solving phase.
  • Add __init_subclass__ lookup (excluding object.__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__ on bases_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 scanning bases_with_metadata in order and picking the first base whose get_dunder_init_subclass(...) returns Some, 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.

Comment thread pyrefly/lib/alt/class/class_metadata.rs Resolved
Comment thread pyrefly/lib/test/constructors.rs Resolved
@github-actions github-actions Bot added size/m and removed size/m labels May 19, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions github-actions Bot added size/m and removed size/m labels May 19, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@NathanTempest NathanTempest 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.

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.

@meta-codesync

meta-codesync Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

@NathanTempest has imported this pull request. If you are a Meta employee, you can view this in D112153555.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@yangdanny97 yangdanny97 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@kinto0 kinto0 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.

Review automatically exported from Phabricator review in Meta.

@stroxler stroxler 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.

Review automatically exported from Phabricator review in Meta.

@stroxler stroxler 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.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 7cbda17 Jul 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Jul 28, 2026
@meta-codesync

meta-codesync Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@NathanTempest merged this pull request in 7cbda17.

@asukaminato0721
asukaminato0721 deleted the 3447 branch July 28, 2026 19:35
avokin2 pushed a commit to avokin2/pyrefly that referenced this pull request Aug 12, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no error on invalid call to __init_subclass__

6 participants