Skip to content

False-negative when instantiating a class before its constructor is defined #2324

Closed

Description

@ffigiel

I've found a small bug:

class Operation:

    @classmethod
    def randomize(cls, config: Config) -> 'Operation':
        return cls(config)

    def __init__(self, config: Config) -> None:
        self.config = config

This complains at line 5, Too many arugments for "Operation". Swapping these two methods around (init before randomize) fixed the issue.

Looks like mypy tried to type-check cls(config) before learning what Operation's constructor looks like.

Activity

  1. added this to the milestone on Oct 25, 2016
  2. gvanrossum commented on Oct 25, 2016

    @gvanrossum
    Member

    Oh, interesting. This could be dealt with by deferring the first method when the class signature isn't known yet. But we'd need some kind of flag to tell us whether we've processed __init__ yet (and maybe if there isn't one it should be set to True at the start, by the semantic analyzer).

    Do you feel up to trying to submit a PR for this? No pressure!

  3. ffigiel commented on Oct 25, 2016

    @ffigiel
    ContributorAuthor

    I could work on that, any tips what module/function to begin from?

  4. gvanrossum commented on Oct 27, 2016

    @gvanrossum
    Member
  5. removed this from the milestone on Mar 29, 2017
  6. JukkaL commented on Dec 5, 2017

    @JukkaL
    Collaborator

    Bumping priority because this seem to happen with some frequency and the workaround is not easy to figure out.

  7. JelleZijlstra commented on Dec 5, 2017

    @JelleZijlstra
    Member

    This is a dupe of #1727, which is older and has more discussion; going to close this one and make the other one high-pri.

  8. mckaysalisbury commented on Dec 6, 2017

    @mckaysalisbury

    It seems as if any method can call any method, so a two-pass system might work best. Once to process all of the function signatures, then another to process the code.

  9. gvanrossum commented on Dec 6, 2017

    @gvanrossum
    Member

    Thanks for the advice.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions