python / python/mypy

Constrained TypeVars with Any default

Open
#20,184 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported TypeVar declaration and the “TypeVar default must be one of the constraint types” error using mypy 1.18.2. Start by locating the validation for constrained TypeVar defaults; done means a default of Any is accepted for this case without weakening other default checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.