Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Correctly handle cls in protocol classmethod - #11119

Merged
97littleleaf11 merged 2 commits into
python:masterfrom
nullie:correctly-handle-cls-in-protocol-classmethod
Nov 19, 2021
Merged

Correctly handle cls in protocol classmethod#11119
97littleleaf11 merged 2 commits into
python:masterfrom
nullie:correctly-handle-cls-in-protocol-classmethod

Conversation

@nullie

@nullie nullie commented Sep 16, 2021

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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

Comment thread mypy/subtypes.py Outdated
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
Copy Markdown
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.

Copy link
Copy Markdown

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