You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Contextual typing works only with the first element in a union #793
I was surprised to not see an issue for this yet (unless I just missed it?). Opening one for V1 tracking. Contextual typing doesn't play well with union types, as seen here:
bug = "Both assignments should be allowed. When decomposing the contextual hint, we eagerly resolve vars to the 'first' branch of the union. Note: due to the union's sorted representation, the first branch is not necessarily the first in source order.",
test_contextual_typing_against_unions,
r#"
class A: ...
class B: ...
class B2(B): ...
class C: ...
x: list[A] | list[B] = [B2()] # E: `list[B2]` is not assignable to `list[A] | list[B]`
y: list[B] | list[C] = [B2()]
"#,
);
Sandbox Link
No response
(Only applicable for extension issues) IDE Information
Yeah, I started working on this but put it down to work on overloaded constructor calls. There are a couple open issues assigned to me with the same root cause. I can take this or you can take it over if you have already started :)
I'm not working on this right now - just opened an issue because I couldn't find one and I knew this is a bug we care about :)
But once I get #750 sorted out, I may pick up #164 if it's still open, since that affects our ability to type-check usages of pydantic.ConfigDict. I looked into it briefly, and I think the TypedDict bug is related to this union issue but not the same thing. We skip these TypedDict cases in expr_infer entirely when the hint is a union:
Grabbing this one because my work on #105 affects how Varible::Unwrap works, so I may be able to fix this (or at least it's probably not a great idea for someone else to try to fix it in parallel with my changes).
In addition to splitting union hints, do we need to split Type::Type and/or Type::Quantified restrictions?
I don't think we need to split Type::Type, because type is covariant. I wasn't able to find an example in which not splitting gives us the wrong behavior.
Activity
samwgoldman commentedon Jul 31, 2025
Yeah, I started working on this but put it down to work on overloaded constructor calls. There are a couple open issues assigned to me with the same root cause. I can take this or you can take it over if you have already started :)
rchen152 commentedon Jul 31, 2025
I'm not working on this right now - just opened an issue because I couldn't find one and I knew this is a bug we care about :)
But once I get #750 sorted out, I may pick up #164 if it's still open, since that affects our ability to type-check usages of
pydantic.ConfigDict. I looked into it briefly, and I think the TypedDict bug is related to this union issue but not the same thing. We skip these TypedDict cases inexpr_inferentirely when the hint is a union:pyrefly/pyrefly/lib/alt/expr.rs
Lines 455 to 472 in 0c7797e
Handle unions including TypedDict when inferring a type for a dict li…
rchen152 commentedon Jan 14, 2026
Here's another example:
sandbox
hot123s commentedon Jan 28, 2026
I've encountered the same problem
sandbox
Add github workflow for v1 issue ranking
Add github workflow for v1 issue ranking
rchen152 commentedon Mar 15, 2026
Grabbing this one because my work on #105 affects how
Varible::Unwrapworks, so I may be able to fix this (or at least it's probably not a great idea for someone else to try to fix it in parallel with my changes).Replace all remaining uses of HintRefOld with HintRef
rchen152 commentedon Apr 30, 2026
Closing the loop on the outstanding tasks/questions that remained on this issue:
HintRefis slow #3268.Type::Typeand/orType::Quantifiedrestrictions?Type::Type, becausetypeis covariant. I wasn't able to find an example in which not splitting gives us the wrong behavior.