Skip to content

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

Open
asukaminato0721 wants to merge 5 commits into
facebook:mainfrom
asukaminato0721:3447
Open

fix no error on invalid call to __init_subclass__ #3447#3468
asukaminato0721 wants to merge 5 commits into
facebook:mainfrom
asukaminato0721:3447

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented May 19, 2026

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
Copy link
Copy Markdown

Copilot AI left a comment

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 Outdated
Comment thread pyrefly/lib/test/constructors.rs Outdated
@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
Copy link
Copy Markdown

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

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__

2 participants