Skip to content

Secret is unnecessarily invariant, can be made covariant #12007

Closed

Description

@bluenote10

I noticed that the generic Secret type is unnecessarily invariant, which means that valid usages like this fail to type check:

from pydantic import Secret

def takes_secret(scalar_secret: Secret[str | int | float | bool]):
    ...

def secret_usage() -> None:
    secret: Secret[str] = Secret('my secret')
    takes_secret(secret)

Currently that last line does not type check, because the invariance prevents to accept e.g. a Secret[str] where a Secret[str | ...] is expected. Note that this restriction is unnecessary: Secret is an immutable type, both practically and conceptually. It's a typical covariant use case and there is no need to enforce invariance. The type can be made covariant similarly to how e.g. Sequence or Mapping are covariant thanks to immutability.

I can prepare a small PR for fixing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions