Skip to content

Using cls Argument of Classmethod Defined Before __init__ Causes Error #5415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
janneronkko opened this issue Aug 2, 2018 · 1 comment
Closed

Comments

@janneronkko
Copy link

Mypy is giving an error Too many arguments for "<class>" if cls argument is used for creating object in a classmethod that is defined before __init__. I would expect this to work as similar constructs (see below) work.

The case for having separate class method create is to allow doing some initializations in certain cases (when the classmethod is called) but not in others (when the __init__ is called). The example code is not complete in that sense but is enough to demonstrate the issue I faced.

Used versions:

  • Mypy: 0.620 and 0.630+dev-132dfa264e983a0c566f3fa6f316cde27afff131, i.e. the latest version from master branch
  • Python: 3.6 and 3.7 (haven't tried others)

The output from mypy example.py: example.py:4: error: Too many arguments for "A" for the code below:

class A:
    @classmethod
    def create(cls) -> A:
        return cls(0)

    def __init__(self, arg: int) -> None:
        self.arg = arg

The code below contains similar constructs (in addition to the case above) that do not cause mypy to report errors, i.e. the only reported error is from create1 class method (and the error is the same as in the above code):

def foo() -> A:
    return bar(9)


def bar(value: int) -> A:
    return A(value)


class A:
    # The only error is reported from this class method
    @classmethod
    def create1(cls) -> A:
        return cls(0)

    @classmethod
    def create2(cls) -> A:
        return A(0)

    @staticmethod
    def create3() -> A:
        return A(1)

    def __init__(self, arg: int) -> None:
        self.arg = arg

    @classmethod
    def create4(cls) -> A:
        return cls(0)

    @classmethod
    def create5(cls) -> A:
        return A(0)

    @staticmethod
    def create6() -> A:
        return A(1)
@emmatyping
Copy link
Member

Hi, this is a duplicate of #1727, so I am going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants