Skip to content

Fixes #1727: constructor after classmethod counts #5501

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
wants to merge 5 commits into from

Conversation

VadimPushtaev
Copy link

This is a fix for #1727.

Instead of prepare classmethod definitions while analyzing class, let's postpone this until the class is fully analyzed.

dataclass magic creates __init__ after the class is already created, so they fix up all the classmethods. This is no longer needed.

@VadimPushtaev
Copy link
Author

Possibly, another solution is to do what dataclass does for every class instead of postponing something.

@VadimPushtaev
Copy link
Author

Also, I don't quite like self.prepare_postponed_classmethods_signature() being in the leave_class method, which seems to be too high-level for this kind of tasks, but I'm not sure. I could use some advice.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is an important issue to fix, but there are some potential dangers. I believe the right fix should also fix #4575 The underlying problem is the same: processing of class method signatures happens too soon.

Also the current solution has the problem that it might not work well with the fine grained daemon (because class is not a "real" fine-grained dependency target). Probably, I would move this step to the patches after third pass (at least after forward refs are resolved) unless there are some other problems.

Also please note many mypy core devs are on vacation now, so we may not always respond in time. I am only leaving "large scale" comments now.

self._arg: int = arg

reveal_type(C.create) # E: Revealed type is 'def (arg: builtins.int) -> __main__.C'
[builtins fixtures/classmethod.pyi]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this test actually tests the situation enough. I think what you need to add is a reveal_type for cls and cls(<expected args>) in the method body, and as well check that cls(<unexpected args>) fails with a reasonable error.

Please be sure to include all the examples that appeared in the original issue. In addition please add at least tests that check your fix works correctly with the following:

  • Fine-grained daemon
  • Forward references
  • Generic classes
  • Overloaded methods (there are known issues, but few simple tests would help)

mypy/semanal.py Outdated
functype = func.type
assert self.type
assert functype
assert isinstance(functype, CallableType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need these asserts? Please add assert messages.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was basically to make mypy happy of this code. Fixed to make more sense.

@VadimPushtaev
Copy link
Author

Thanks for the review, I'll try to fix all the issues as soon as possible.

Constructor defined after classmethod was considered
to have no args in the classmethod.
This commit fixes that bug.
@VadimPushtaev
Copy link
Author

I believe that's all tests we reasonably need.

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 21, 2018

Hmm is this now superseded by #5646? Thanks for getting the ball rolling with this anyway!

@ilevkivskyi
Copy link
Member

Yes, I think now that we have a broader fix, this one is not needed. Closing in favor of #5646

@VadimPushtaev Thank you for working on 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

Successfully merging this pull request may close these issues.

3 participants