Widening generic types when calling methods in context. #2108
Unanswered
randolf-scholz
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am wondering if there is a general recipe to annotate methods that return a new instance, so that it allows widening the type based on the outer context.
Question: Is it possible, in the current type system as of python 3.14, to annotate this method:
such that the following test passes without type errors
Context
Consider the following example:
According to PEP 584 – Add Union Operators To dict,
x | yis essentially equivalent to{**x, **y}, so the first surprise here is that the type checkers except formypyaccept one but not the other. This can be due to special casing of the literal constructor.However, it raises the question of how to communicate to the type checker when a method returns a new instance, and widening the type is OK.
My Attempt
The best I could come up with was adding a "slack-variable" to the output, but it doesn't work. I hoped that a type checker would infer
K :> objectif necessary, and so allowy: dict[object, int] = join(left, right), but none of the type checkers I tested allows this. Onlytyseems to partially accept this, but requires getting rid of the defaultK=Neverand consequently emitsdict[Unknown | int | str, Unknown | inton thereveal_typeMy current thinking is that this possibly requires HKT (#548), or more specifically lower bounding a
TypeVarby anotherTypeVar, so that for example the join method could be typed as:Beta Was this translation helpful? Give feedback.
All reactions