Skip to content

no error on invalid call to __init_subclass__ #3447

Closed

Description

@DetachHead

Describe the Bug

class Foo:
    def __init_subclass__(cls, asdf: int) -> None:
        pass


class Bar(Foo, asdf=1): ...
class Baz(Foo, asdf=""): ... # no error
class Qux(Foo): ... # no error

it looks like pyrefly doesn't support __init_subclass__ at all. bad-typed-dict special-cases it for TypedDict, but ideally that rule should be removed and replaced with a more general rule that checks __init_subclass__ calls on any class

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQBiuuiAOuvV-ZjGPQH0BEdBAAuAuAFds1WnCEAKanAA09WpjCJ6IsQEp6AWgB89AHK50Mdp273i8js-Ry69AEKoAToua51TTAAXgBGfR1CKI43Bi8ALz8WQLgtYLYQDIj6KMIYmncARSl8JNxs3JBVEDJvPihSQjFcAFsoCjIxAAsrIwA3GG84CCt6YPoMgGZCUIAmDI4AbUHvXCGAXQ4MdFwxVDER9CMWiDgW-cousfoAchW1m44QAF9q1EoDgcZoGAo0LDwRDILyAA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. NathanTempest commented on May 18, 2026

    @NathanTempest
    Contributor

    Thanks for the report, @DetachHead! Confirmed on HEAD that pyrefly produces 0 errors for all three cases. Pyrefly currently doesn't validate kwargs on class statements against the parent's init_subclass signature at all. No type checking, no missing-argument detection. The class Qux(Foo) case is particularly important since it produces a TypeError at import time in Python.

    The existing bad-typed-dict special case for TypedDict's total kwarg would ideally be generalized into a proper init_subclass validation rule for all classes.

    @stroxler can we extend the implementation of TypedDict's kwarg handling to Pyrefly's init subclass kwarg handling as well ?

  2. DetachHead commented on Jun 11, 2026

    @DetachHead
    ContributorAuthor

    also should be a check to ensure that __init_subclass__ is callable:

    class Super:
        __init_subclass__: None = None # should be an error
    
    class Sub(Super): ...  # crashes at runtime

    https://docs.astral.sh/ty/reference/rules/#non-callable-init-subclass

  3. github-actions commented on Jun 26, 2026

    @github-actions

    This issue has someone assigned, but has not had recent activity for more than 2 weeks.

    If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.

    Thank you for your contributions!

  4. rchen152 commented on Aug 11, 2026

    @rchen152
    Contributor

    I'll work on a fix for the remaining false negative in #3447 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions