Skip to content

Behavior of mypy not matching example in PEP 483 #8611

Closed
@mbenbernard

Description

@mbenbernard

It appears that there's a discrepancy between one of the examples given in PEP 483 and the actual behavior of mypy (0.750).

So I ran mypy on the following code snippet:

class Employee: ...
class Manager(Employee): ..


T_co = TypeVar('T_co', Employee, Manager, covariant=True)
T_contra = TypeVar('T_contra', Employee, Manager, contravariant=True)


class Base(Generic[T_contra]):
    ...


class Derived(Base[T_co]):
    ...

def func1(a: Base[Employee]): ...

func1(Derived[Manager]())      # error: Argument 1 to "func1" has incompatible type "Derived[Manager]"; expected "Base[Employee]"

def func2(a: Base[Manager]): ...

func2(Derived[Employee]())     # OK; Derived[Employee] < Base[Manager]

As you can see, mypy complains about the call to func1(), but not for the one to func2().

These results seem to contradict what is stated in PEP 483: "[...] a type checker will also find that, e.g., Derived[Manager] is a subtype of Base[Employee]." But mypy seems to tell us instead that Derived[Employee] < Base[Manager].

Am I understanding things correctly? Is this a bug in mypy, or maybe an error/typo in PEP 483?

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