Skip to content

Constrained TypeVars with Any default #20184

Open

Description

@srittau

Bug Report

In python/typeshed#13683 I'm trying to change a type var to the following:

_I = TypeVar("_I", int, bytes, float, bool, default=Any)  # possibly memoryview types

Unfortunately, mypy complains: error: TypeVar default must be one of the constraint types [misc]. While this is of course generally sound advice, a default of Any should always be permissible, as this allows memoryview to stand for any possible memoryview type. Without it, users are forced to write memoryview[Any] in every instance, which is unreasonable.

Your Environment

  • Mypy version used: 1.18.2

Activity

  1. sterliakov commented on Nov 7, 2025

    @sterliakov
    Collaborator

    I agree with your position, yet the spec seems to disallow that:

    For constrained TypeVars, the default needs to be one of the constraints. A type checker should generate an error even if it is a subtype of one of the constraints.

  2. srittau commented on Nov 10, 2025

    @srittau
    ContributorAuthor

    Thanks, for pointing this out. I've opened a discussion at https://discuss.python.org/t/constrained-type-vars-with-any-default/104799.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions