python / python/mypy

(🐞) No error when assigning an non-concrete type object to a `Callable` variable matching the constructor (`Protocol`, `abstract`)

Open
#13,171 0 comments 1 reaction 1 assignee View on GitHub

@sobolevn is already working on this.

Since Jul 21, 2022.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from typing import Protocol, Callable
from abc import ABC, abstractmethod

class A(ABC):
    @abstractmethod
    def f(self) -> None: ...
    
class P(Protocol):
    a: int

ta1 = A
reveal_type(ta1)
reveal_type(ta1())  # error

ta2: type[A] = A  # error
reveal_type(ta2)
reveal_type(ta2())

ta3: Callable[[], A] = A  # no error
reveal_type(ta3)
reveal_type(ta3())


tp1 = P
reveal_type(tp1)
reveal_type(tp1())  # error

tp2: type[P] = P  # error
reveal_type(tp2)
reveal_type(tp2())

tp3: Callable[[], P] = P  # no error
reveal_type(tp3)
reveal_type(tp3())

playground

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.