python / python/mypy

Union TypeVar Inference Rather Than Most Recent Ancestor

Open
#13,424 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

When two values with different types are constrained to the same type var, mypy automatically looks for the most recent ancestor. My request is for it instead to use the Union type. So, with the following code:

T  = TypeVar("T")
def first(a: T, b: T) -> T:
    return a

class A: 
    pass 
class B(A):
    pass 
class C(A):
    pass

result = first(B(), C())

Right now result is typed as A. I contend that it would be better for it to be typed as Union[B, C].

Pitch

First of all, the cons of my proposal:

  • if there are a lot of values, it can be cleaner and easier to read if there's just one thing representing the type
  • one could argue that its bad practice to use highly differing types for the same type var, and my feature would support such a practice

Those cons aren't meaningless, but I think the first is a small one, and the second one is debatable all together. And on the other side, here are my arguments for this feature:

  • Its more powerful, and still correct. In general, I think the more inferences you can make the better, as long as you aren't sacrificing correctness.
  • Often in python, there is a specific Union type we use often, often defined with a variable (such as SomeType = Union[T1, T2]). This change in typing would work much more nicely if you want to get out values of such types.
  • This final pro is smaller, but the VSCode python extension, which provides types when hovering over values, does this. I understand that Mypy wouldn't want to be beholdent to another codebase's decisions on how to do inference, but all else being equal it is less confusing when there is agreement between different tools. This is especially true when one might use those tools at the same time, which is true for Mypy and the VSCode standard python extension.

Overall, it seems to me that the pros outweigh the cons; especially the first pro. That said, I'm not an expert in python-esque type systems, and so I'd totally believe that there's some philosophy I'm missing.

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

The issue does not name files, tests, or an entry point. Start by locating mypy's TypeVar inference implementation and existing tests for calls with different argument types; done means the example infers Union[B, C] without breaking established inference behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.