Skip to content

Type objects that conform to Protocol definitions are not recognized. #9204

Closed
@mostrows2

Description

@mostrows2

A class with a classmethod that matches a Protocol definition is not recognized as such.

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

See end of report.

  • What is the actual behavior/output?
% mypy /tmp/test2.py                                                                                                                                         
/tmp/test2.py:22: error: Incompatible types in assignment (expression has type "Type[Class2]", variable has type "Proto")
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?

No errors

  • What are the versions of mypy and Python you are using?

0.770, 3..8.4

from typing import Protocol


class Proto(Protocol):
    def func(self) -> int:
        ...

class Class1:
    def func(self) -> int:
        return 0

class Class2:
    @classmethod
    def func(cls) -> int:
        return 1


a: Proto = Class1()
b: Proto = Class2()
c: Proto = Class2

aa: int = a.func()
bb: int = b.func()
cc: int = c.func()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions