Skip to content

Use of covariant type arguments in methods needs to be reported as error #3139

Closed
@pkch

Description

@pkch

Contravariant type args in method return is correctly identified as type errors; but covariant type args in method arguments does not create an error:

T_co = TypeVar('T_co', covariant=True)

class Covariant(Generic[T_co]):
    def f(x: T_co) -> T_co:  # Need an error here
        return x

T_contra = TypeVar('T_contra', contravariant=True)

class Contravariant(Generic[T_contra]):
    def f(x: T_contra) -> T_contra:  # E: Cannot use a contravariant type variable as return type
        return x

It works correctly for normal functions though (not methods).

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