Skip to content

typing an init parameter causes wrong argument calculation #4290

Closed

Description

@mckaysalisbury

I've got a class which has a method that creates instances of the class.

class Bar:
    @classmethod
    def create(cls, foo: int):
        return cls(foo)

    def __init__(self, foo: int) -> None:
        pass

When I run mypy against it, it says mypytest.py:4: error: Too many arguments for "Bar"

It seems like a bug to me, because this works fine

class Bar:
    @classmethod
    def create(cls, foo):
        return cls(foo)

    def __init__(self, foo: int) -> None:
        pass

I don't understand why a class method which defines the type of a parameter should break the creation of an instance.

(see https://stackoverflow.com/q/47493293/8384 )

Activity

  1. gvanrossum commented on Nov 29, 2017

    @gvanrossum
    Member

    This is because mypy processes definitions in order, and when it comes across create() in the first version it hasn't seen the __init__() signature yet. IIRC there's another issue open for this.

  2. JelleZijlstra commented on Nov 29, 2017

    @JelleZijlstra
    Member

    Yes, this is #1727.

  3. mvoitko commented on Oct 12, 2020

    @mvoitko

    The same problem remains with SQLAlchemy models.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions