python / python/mypy

Variance inference gets lost with union types

Open
#17,744 0 comments 1 reaction 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

Variance inference works out whether a type variable should be co-, contra- or in-variant, based on the way it is used as an argument type, a result type, or both.

But it seems it cannot see through a Union type used in a result. In this example, it correctly determines that T in class Example is used as both argument and result, therefore is invariant, and does not complain becuase the TypeVar is (by default) invariant. But in class Example2, T is still used as both argument and result, albeit through a Union. It seems that mypy cannot see through the union, and reports errors just as though the method had no variables in its return type. For comparison, in Example3, the type variables are not used at all in the return type, so it correctly infers that T should be contravariant; mypy gives the same error messages as Example2, suggesting that it doesn't see S|T as containing any type variables at all.

To Reproduce

from typing import Generic, Protocol, TypeVar

S = TypeVar("S")
T = TypeVar("T")

class Example(Protocol, Generic[T]):
    def method(self, arg: T) -> T:
        return arg

class Example2(Protocol, Generic[S, T]):
    def method(self, arg: T) -> S|T:        # <-- bug here
        return arg
        
class Example3(Protocol, Generic[S, T]):
    def method(self, arg: T) -> int:
        return 7

Expected Behavior

In the class Example2, I expect mypy to recognize that T is used positively in method arguments and result types, and therefore to infer that T should be an invariant variable. (The inference for S is not important for this report; but the bug does not repro if the other type is a concrete type like int.)

Actual Behavior

$ mypy example.py
example.py:10: error: Invariant type variable "S" used in protocol where covariant one is expected  [misc]
example.py:10: error: Invariant type variable "T" used in protocol where contravariant one is expected  [misc]
example.py:14: error: Invariant type variable "S" used in protocol where covariant one is expected  [misc]
example.py:14: error: Invariant type variable "T" used in protocol where contravariant one is expected  [misc]

Your Environment

  • Mypy version used: 1.11.2 (compiled: yes)
  • Mypy command-line flags: none, just the python file name
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.14

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

Start by reproducing the report with the provided example.py and the mypy command shown, using the stated mypy 1.11.2 environment. Trace variance inference for the Example2 Protocol and compare it with Example and Example3; done means the union-containing return type is analyzed correctly without changing the expected diagnostics for the other examples.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.