Skip to content

assert_type does not handle 'Type[]' of Protocol types correctly #15701

Closed as not planned
@ippeiukai

Description

@ippeiukai

Bug Report

According to PEP 544, "Variables and parameters annotated with Type[Proto] accept only concrete (non-protocol) subtypes of Proto." (https://peps.python.org/pep-0544/#type-and-class-objects-vs-protocols)

Using assert_type to check if Proto is accepted as Type[Proto] fails to report an error.

(from #15666)

To Reproduce

from typing import assert_type, Type, Protocol
from abc import ABC, abstractmethod


# == case 1 ==

class Proto(Protocol):
    def meth(self):
        ...


class Concrete(Proto):
    def meth(self):
        pass


var: Type[Proto]
var = Proto    # Error (according to PEP 544)
var = Concrete # OK
var().meth()   # OK

assert_type(Proto, Type[Proto])  # should report error


# == case 2 ==

class PureAbstract(ABC):
    @abstractmethod
    def meth(self):
        ...


class Concrete2(PureAbstract):
    def meth(self):
        pass


var2: Type[PureAbstract]
var2 = PureAbstract # ??? (Should pure abstract class behave like Protocol here?)
var2 = Concrete2    # OK
var().meth()        # OK

assert_type(PureAbstract, Type[PureAbstract])  # should report error if above is error


# == for ref ==

class Abstract(ABC):
    def meth(self):
        pass


class Concrete3(Abstract):
    ...


var3: Type[Abstract]
var3 = Abstract  # OK
var3 = Concrete3 # OK
var().meth()     # OK

assert_type(Abstract, Type[Abstract]) # OK

Expected Behavior

assert_type should confirm that Proto does not have an inferred type of Type[Proto].

Actual Behavior

assert_type accepts Proto to have an inferred type of Type[Proto].

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions