[ty] Propagate type context through comprehensions#22564
Conversation
|
|
||
| ```py | ||
| # TODO: This should reveal `Literal["a", "b"]` | ||
| # revealed: Unknown | str |
There was a problem hiding this comment.
This and the other todos are astral-sh/ty#2486 (and some will also be fixed indirectly by astral-sh/ty#1240).
| tcx: TypeContext<'db>, | ||
| } | ||
|
|
||
| impl<'db> InferScope<'db> { |
There was a problem hiding this comment.
Ideally we could have a generic InferRegion type, but the salsa macros don't seem to like it.
Typing conformance resultsNo changes detected ✅ |
|
d27b4c6 to
c781969
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
2 | 12 | 137 |
possibly-missing-attribute |
3 | 1 | 71 |
invalid-return-type |
0 | 25 | 37 |
invalid-assignment |
1 | 20 | 35 |
unsupported-operator |
0 | 0 | 32 |
not-iterable |
0 | 0 | 9 |
type-assertion-failure |
0 | 0 | 8 |
not-subscriptable |
0 | 0 | 6 |
unused-ignore-comment |
2 | 1 | 0 |
invalid-await |
2 | 0 | 0 |
unresolved-attribute |
0 | 0 | 2 |
| Total | 10 | 59 | 337 |
carljm
left a comment
There was a problem hiding this comment.
Looks good!
I'm guessing we may want similar treatment for generator expressions at some point, but those have enough of their own wrinkles that it makes sense to handle them separately.
| // Comprehensions are skipped when checking all scopes, so this is our only chance | ||
| // to report diagnostics. | ||
| if let Some(scope_diagnostics) = inference.diagnostics() { | ||
| self.context.extend(scope_diagnostics); | ||
| } | ||
|
|
||
| self.infer_comprehension_specialization(KnownClass::Dict, &[key_type, value_type], tcx) | ||
| self.infer_comprehension_specialization(KnownClass::Set, &[Some(elt)], inference, tcx) | ||
| .unwrap_or_else(|| { | ||
| KnownClass::Set.to_specialized_instance(self.db(), &[Type::unknown()]) | ||
| }) |
There was a problem hiding this comment.
This is still a fair bit to duplicate for all comprehension kinds... seems like KnownClass::Set is the only set-distinct thing in here?
There was a problem hiding this comment.
It's slightly better now with the extend_scope method, I'm not sure we can do much better here. The list and set cases are similar because they both have a single element type, while dictionaries have two.
fa2b058 to
109d2a5
Compare
Summary
Resolves astral-sh/ty#1602.