Skip to content

Covariant status of typevar lost when union in bound. #3228

Description

@lor1113

Summary

https://play.ty.dev/95eb2f4a-476f-40c7-8ea9-6fa12b4d3d15

https://play.ty.dev/5a5b21ac-73b4-4b9a-b3ac-f12029b064ca

The only difference between these two links is that the TypeVar bound changes from A to A | C.

I won't claim to understand python typing semantics and rules well (does anyone?) but I can't see how this makes any sense. Adding a union to a bound should only increase the number of acceptable matches, I can't see how it would make sense to have it invalidate an otherwise valid option.

The full error message given by ty check is

info: `list` is invariant in its type parameter
info: Consider using the covariant supertype `collections.abc.Sequence`
info: For more information, see https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics
info: rule `invalid-argument-type` is enabled by default

which is why I wrote in the title that the covariant status of the typevar seems to be "lost" . . ty is telling me that the list is invariant regardless.

T_co = TypeVar("T_co", A , C, covariant=True) also does not work.

Version

ty 0.0.28

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions