Mypy: Too many arguments for __init_subclass__

Created on 1 Mar 2018  路  13Comments  路  Source: python/mypy

The annotated version of this simple example fails validation:

class Philosopher:
    def __init_subclass__(cls, default_name: str, **kwargs) -> None:
        super().__init_subclass__(**kwargs)
        cls.default_name = default_name

class AustralianPhilosopher(Philosopher, default_name="Bruce"):
    pass

Error message: foo.py:3: error: Too many arguments for "__init_subclass__" of "object". Note that the call to super() is necessary.

bug false-positive priority-1-normal

Most helpful comment

even without dataclass:

class Base:
    def __init_subclass__(cls, *args, **kwargs):
        super().__init_subclass__(*args, **kwargs)

class Derived(Base):
    pass
%  mypy test.py
test.py: note: In class "Derived":
test.py:5:1: error: Too few arguments for "__init_subclass__"
Found 1 error in 1 file (checked 1 source file)
%  mypy --version
mypy 0.740



md5-b5ad82e49883636c541a8b1231c69026



%  mypy test.py
Success: no issues found in 1 source file
%  mypy --version
mypy 0.730

All 13 comments

Ooh, yeah, mypy doesn't seem to understand enough about __init_subclass__.

I've just encountered the same error when calling super().__int__(arg) in a class that inherits from collections.UserDict.

Is this the same bug, or should I raise another issue for that?

Is this the same bug, or should I raise another issue for that?

IIRC UserDict was just a typeshed problem and was fixed few months ago, you should update mypy.

IIRC UserDict was just a typeshed problem and was fixed few months ago, you should update mypy.

I've just tried v0.610. That appears to have fixed the problem.

Thank you.

Is there a way to suppress this particular error? # type: ignore doesn't seem to work for this.

Is there a way to suppress this particular error? # type: ignore doesn't seem to work for this.

You need to put it on the right line :-) It's the super() call that mypy complains about. This works:

class Philosopher:
    def __init_subclass__(cls, default_name: str, **kwargs) -> None:
        super().__init_subclass__(**kwargs)  # type: ignore
        cls.default_name = default_name

Ah ha! I didn't put it on the right line. Also, hi @mjpieters! :)

I'm having this issue when I use mypy 0.740, but not 0.730, so it sounds like there was a regression.

@dhorkin Can you provide a code fragment that illustrates the difference between 0.730 and 0.740? The original example seems to work the same on 0.730 and 0.740.

@dhorkin Can you provide a code fragment that illustrates the difference between 0.730 and 0.740? The original example seems to work the same on 0.730 and 0.740.

Toy example:

from dataclasses import dataclass

@dataclass(order=True, frozen=True)
class Base:
    def __init_subclass__(cls, *args, **kwargs):
        super().__init_subclass__(*args, **kwargs)

@dataclass(order=True, frozen=True)
class Sub1(Base):
    a: str
    b: str

Passes 0.730 but fails 0.740

I guess technically it says "too few arguments" not "too many", so it might be a separate issue, but certainly similar.

even without dataclass:

class Base:
    def __init_subclass__(cls, *args, **kwargs):
        super().__init_subclass__(*args, **kwargs)

class Derived(Base):
    pass
%  mypy test.py
test.py: note: In class "Derived":
test.py:5:1: error: Too few arguments for "__init_subclass__"
Found 1 error in 1 file (checked 1 source file)
%  mypy --version
mypy 0.740



md5-b5ad82e49883636c541a8b1231c69026



%  mypy test.py
Success: no issues found in 1 source file
%  mypy --version
mypy 0.730

The issue still persists for me with the same example and it never goes away irrespective of the version

# -*- coding: utf-8 -*-

class Base:
    def __init_subclass__(cls, *args, **kwargs):
        super().__init_subclass__(*args, **kwargs)

class Derived(Base):
    pass
% mypy example.py
example.py:5: error: Too many arguments for "__init_subclass__" of "object"

The same error shows up for me even in 0.730, 0.740, 0.782. What's the difference, how can the behavior be different for me compared to @sergiorussia even when I use the same version of mypy?

@vikigenius, same here on Python 3.9 and mypy 0.790 馃槙

Was this page helpful?
0 / 5 - 0 ratings