Skip to content
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

Correctly handle cls in protocol classmethod #11119

Conversation

nullie
Copy link
Contributor

@nullie nullie commented Sep 16, 2021

Description

Correctly handle cls in generic classmethods of protocol. Closes #11115

This should correctly handle cls in classmethods. Current behavior of not passing is_classmethod seems like an omission in implementation, so this should only correct buggy behavior and shouldn't break something else.

Test Plan

I've included a test in the test suite (testSelfTypeProtocolClassmethodMatch)

T = TypeVar('T')

class HasDoX(Protocol):
@classmethod
Copy link
Member

Choose a reason for hiding this comment

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

What about @staticmethod? Does it fall into the same problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

staticmethod doesn't have this problem, because there's no self/cls to bind

mypy/subtypes.py Outdated
@@ -710,7 +710,7 @@ def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -
and node.is_initialized_in_class
and not node.is_staticmethod)):
assert isinstance(typ, FunctionLike)
signature = bind_self(typ, subtype)
signature = bind_self(typ, subtype, isinstance(node, Var) and node.is_classmethod)
Copy link
Member

Choose a reason for hiding this comment

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

I think that adding argument name here would be better for readability.

Choose a reason for hiding this comment

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

@sobolevn I've added argument name here, is there anything else I can do?

@nullie nullie changed the title Correctly handle cls in protocol classmethod (#11115) Correctly handle cls in protocol classmethod Sep 21, 2021
@97littleleaf11 97littleleaf11 merged commit 89bb94a into python:master Nov 19, 2021
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Closes python#11115.

Correctly handle cls in generic classmethods of protocol. 

This should correctly handle cls in classmethods. Current behavior of not 
passing is_classmethod seems like an omission in implementation, so this 
should only correct buggy behavior and shouldn't break something else.

Adds a test case `testSelfTypeProtocolClassmethodMatch`.
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.

Generic method signature doesn't resolve properly on a protocol classmethod
4 participants