Skip to content

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

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
ffigiel opened this issue Oct 25, 2016 · 7 comments
Closed
Labels
bug mypy got something wrong priority-0-high

Comments

@ffigiel
Copy link
Contributor

ffigiel commented Oct 25, 2016

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.

@gvanrossum gvanrossum added the bug mypy got something wrong label Oct 25, 2016
@gvanrossum gvanrossum added this to the 0.5 milestone Oct 25, 2016
@gvanrossum
Copy link
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!

@ffigiel
Copy link
Contributor Author

ffigiel commented Oct 25, 2016

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

@gvanrossum
Copy link
Member

gvanrossum commented Oct 27, 2016 via email

@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented Dec 5, 2017

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

@JelleZijlstra
Copy link
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.

@mckaysalisbury
Copy link

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.

@gvanrossum
Copy link
Member

Thanks for the advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high
Projects
None yet
Development

No branches or pull requests

5 participants