python / python/mypy

TypeVar with Type-based constraints misbehaving when used to annotate *args

Open
#9,569 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

It seems that the below combination of *args, and TypeVar with constraints related to Type, is misbehaving. I reached out on the Gitter and was instructed that this is probably a bug.

I tried replacing the Types with ints (observable in the linked mypy-playground) and that seems to work fine.

To Reproduce

from typing import TypeVar, List, Type


TypeArgT = TypeVar("TypeArgT", Type, List[Type])

def type_a(*args: TypeArgT):
    ...
    
    
def type_b(*args: List[Type]):
    ...
    
type_a(int, str, float)             # test t1
type_a([str, float], [int])         # test t2   <-- this does not work

type_b([bool, bool], [str, bool])   # test t3

https://mypy-play.net/?mypy=0.780&python=3.8&gist=847224369f6bad4d74a1ccfd672fb250

Actual Behavior

Test t2 in the above snippet throws the following mypy error:

main.py:14: error: Value of type variable "TypeArgT" of "type_a" cannot be "object"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.790
  • Python version used: 3.8

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

No project file or test is named. Start by running the reproduction against mypy 0.790, then trace inference for constrained TypeVar values used with *args; done means t2 is accepted without changing the existing t1 and t3 behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.