Skip to content

Covariant generic type context results in confusing message with __add__ #5971

@JukkaL

Description

@JukkaL

This generates a false positive on the last line:

from typing import TypeVar, Generic, Union, Any

T = TypeVar('T')
Tcov = TypeVar('Tcov', covariant=True)
S = TypeVar('S')

class A(Generic[Tcov]): pass

class B(A[T]):
    def __add__(self, x: B[S]) -> B[Union[T, S]]: ...

a: A[object]
b1: B[Any]
b2: B[int]

a = b1 + b2  # Unsupported operand types for + ("B[Any]" and "B[int]")

The error message is confusing, since B[Any] and B[int] are compatible.

We perhaps shouldn't use the type context from a for type inference in this case.

This issue affects built-in list and Sequence type context with the latest typeshed master.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions