Skip to content

Decorator with constrained TypeVar specializes to first constraint #20437

@nkemnitz

Description

@nkemnitz

Bug Report

Changing the order of constraints of TypeVar T changes whether or not mypy passes. The wrapped method always gets narrowed to whatever the first constraint type is set to.

To Reproduce

from collections.abc import Callable
from typing import TypeVar, reveal_type


T = TypeVar("T", str, bytes)  # fails
# T = TypeVar("T", bytes, str)  # passes

def decorator(fn: Callable[[T], T]) -> Callable[[T], T]:
    def wrapped(data: T) -> T:
        return fn(data)
    return wrapped

@decorator
def process(data: T) -> T:
    return data

b: bytes = b"test"
result = process(b)

Or see mypy playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=12fd2e05764d26a16cd1d16a10897671

Expected Behavior

Ideally pass for both versions, or consistently fail regardless of the constraint order.
On pyright playground the revealed types match my expectations and pass with either TypeVar order

Actual Behavior

mypy passes or fails depending on TypeVar constraint order, or the data input type

Your Environment

Same behavior with all tested mypy versions (1.6.0 .. 1.19.1) and python 3.11+

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-callsFunction calls, *args, **kwargs, defaults

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions