-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
union distrubution of calls doesn't apply to constraints #831
Comments
So on other words,
But in you example you only cover the first two cases -- the This means that (many of these issues around "typevars with constraints" can be solved with a disjoint union, i.e. |
and
for the case of intersections, it will just match the first (for overloads) or closest (for constraints) and not distribute: class A: ...
class B: ...
class AB(A, B): ...
def f1(x: A) -> list[A]: ...
def f1(x: B) -> list[B]: ...
def f1(x): return [x]
def f2[T: (A, B)](x: T) -> list[T]: return [x]
a_and_b: A & B
f1(a_and_b) # list[A] match first overload
f2(a_and_b) # list[A] T=A
ab: AB
f1(ab) # list[A] match first overload
f2(ab) # list[A] T=A can you break these semantics and cause a runtime type error? |
Ok ok I understand what you're saying, and I probably wasn't explaining it well enough. So the main assumption I'm making with all this, is that a So in the case of @overload
def f2(x: A) -> list[A]: ...
@overload
def f2(x: B) -> list[B]: ... So if we try to pass ... So I checked this, and apparently I don't understand overloads: the same stuff happens with your So where did the Any idea what I'm missing here? |
mypys logic is:
mypy calls this 'union math', i call it 'union distribution' |
annoyingly, constraints behave slightly differently to overloads, in that the constraints themselves have different ordering rules to overloads: @overload
def f1(x: int | str) -> int | str: ...
@overload
def f1(x: int) -> int: ...
def f2[T: (int | str, int)](t: T) -> T: ...
data: int
f1(data) # int | str
f2(data) # int |
I guess that fits in my "framework" if you first do a topological-ish sorting (order by specificity) of the of the constraints before converting them |
The text was updated successfully, but these errors were encountered: