Skip to content

Generic method signature doesn't resolve properly on a protocol classmethod #11115

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
nullie opened this issue Sep 16, 2021 · 1 comment · Fixed by #11119
Closed

Generic method signature doesn't resolve properly on a protocol classmethod #11115

nullie opened this issue Sep 16, 2021 · 1 comment · Fixed by #11119
Labels
bug mypy got something wrong

Comments

@nullie
Copy link
Contributor

nullie commented Sep 16, 2021

Bug Report

Generic type signature doesn't seem to resolve properly on a protocol classmethod

To Reproduce

from typing import Type, TypeVar, Protocol


T = TypeVar('T')


class ModelProtocol(Protocol):
    @classmethod
    def from_dict(cls: Type[T]) -> T:
        ...


class ExampleModel:
    @classmethod
    def from_dict(cls) -> 'ExampleModel':
        ...


foo: ModelProtocol = ExampleModel()

Expected Behavior

No errors

Actual Behavior

example.py:19: error: Incompatible types in assignment (expression has type "ExampleModel", variable has type "ModelProtocol")
example.py:19: note: Following member(s) of "ExampleModel" have conflicts:
example.py:19: note:     Expected:
example.py:19: note:         def from_dict(cls) -> <nothing>
example.py:19: note:     Got:
example.py:19: note:         def from_dict(cls) -> ExampleModel
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): no configuration
  • Python version used: 3.7.10
  • Operating system and version: Ubuntu 20.04.1 LTS
@nullie nullie added the bug mypy got something wrong label Sep 16, 2021
@nullie
Copy link
Contributor Author

nullie commented Sep 16, 2021

I found the origin of this bug and a way to fix it, but I need more type to write a proper test (I would appreciate if someone experienced pointed me to a place where this test should be placed)

--- a/mypy/subtypes.py
+++ b/mypy/subtypes.py
@@ -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)
         if node.is_property:
             assert isinstance(signature, CallableType)
             typ = signature.ret_type

nullie added a commit to nullie/mypy that referenced this issue Sep 16, 2021
97littleleaf11 pushed a commit that referenced this issue Nov 19, 2021
Closes #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`.
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this issue 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
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant