[ty] Use the top materialization of classes for if type(x) is y narrowing#22553
[ty] Use the top materialization of classes for if type(x) is y narrowing#22553AlexWaygood merged 2 commits intomainfrom
if type(x) is y narrowing#22553Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
d06805b to
bdbb990
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-assignment |
5 | 0 | 5 |
invalid-return-type |
1 | 0 | 7 |
invalid-argument-type |
2 | 2 | 3 |
possibly-unresolved-reference |
6 | 0 | 0 |
unused-ignore-comment |
2 | 3 | 0 |
possibly-missing-attribute |
0 | 3 | 1 |
invalid-await |
0 | 2 | 0 |
unresolved-attribute |
0 | 0 | 2 |
redundant-cast |
1 | 0 | 0 |
unsupported-operator |
1 | 0 | 0 |
| Total | 18 | 10 | 18 |
|
Okay, so the first homeassistant hit looks like this: def get_events_json(self):
"""Return a batch of events formatted for writing."""
queue_seconds = QUEUE_BACKLOG_SECONDS + self.max_tries * RETRY_DELAY
count = 0
json = []
dropped = 0
with suppress(queue.Empty):
while len(json) < BATCH_BUFFER_SIZE and not self.shutdown:
timeout = None if count == 0 else self.batch_timeout()
item = self.queue.get(timeout=timeout)
count += 1
if item is None:
self.shutdown = True
elif type(item) is tuple:
timestamp, event = item
age = time.monotonic() - timestampand we're now emitting a diagnostic on the last line where we didn't before. Adding some reveal_type calls in there shows this with the and this on my PR branch: So this is basically a textbook case of astral-sh/ty#1578. This PR makes our I'm guessing the other new ecosystem diagnostics are like this too. I still think the answer here is probably better explanatory diagnostics that tell the user why we've unexpectedly inferred |
| place, | ||
| NarrowingConstraint::regular( | ||
| Type::instance(self.db, rhs_class.unknown_specialization(self.db)) | ||
| Type::instance(self.db, rhs_class.top_materialization(self.db)) |
There was a problem hiding this comment.
So hard to review these massive diffs...


Summary
This is consistent with our
isinstance()/issubclass()narrowing, fixes some open TODOs in our mdtests, and leads to clearly better results.Test Plan
Added mdtests that fail on
main, and updated existing mdtests that had TODOs.