[ty] struct.unpack return type inference#22562
Conversation
struct.unpack Implementation attempt (with AI)struct.unpack return type inference
Typing conformance resultsNo changes detected ✅ |
|
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
struct.unpack return type inferencestruct.unpack return type inference
f7dd858 to
ac47c91
Compare
|
Rolled up into 1 commit to make git history nicer if this gets merged |
sharkdp
left a comment
There was a problem hiding this comment.
Thank you very much, this looks great.
I made some minor adjustments myself (proper thresholding, whitespace handling, some more tests, some refactoring).
| elements.push(ty); | ||
| } else { | ||
| // To prevent OOM errors, we cap the number of repetitions by escaping to Unknown. | ||
| if count >= 2 ^ 16 { |
There was a problem hiding this comment.
2 ^ 16 means 2 XOR 16 in Rust, so this is equivalent to count >= 18 (https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a290d2ec64af9b8124fe1a55c41052ca), probably not what you intended.
2¹⁶ would actually be way too high of a limit, I think. That would mean we would construct tuple types with 65536 entries. I'll change the limit to 32 for now and add some tests that this thresholding kicks in at the right position.
| let mut escaped = false; | ||
| let mut chars = core.chars().peekable(); | ||
|
|
||
| while chars.peek().is_some() { |
There was a problem hiding this comment.
We could also skip whitespace here:
Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unsupported-operator |
80 | 70 | 49 |
not-subscriptable |
96 | 1 | 0 |
possibly-missing-attribute |
24 | 5 | 35 |
invalid-argument-type |
9 | 3 | 9 |
no-matching-overload |
0 | 21 | 0 |
invalid-return-type |
0 | 5 | 2 |
invalid-assignment |
4 | 1 | 1 |
not-iterable |
0 | 0 | 2 |
unresolved-attribute |
0 | 1 | 0 |
| Total | 213 | 107 | 98 |
|
There are a few new diagnostics in the ecosystem (filtering out all the non-deterministic diagnosic noise), and they all look like true positives to me — nice! Looks like this will help users find some bugs. |
Recently the `D` and `F` format characters for complex types were added to `struct.unpack()`. See the following links for details: - https://docs.python.org/3/whatsnew/3.14.html#struct - https://docs.python.org/3/library/struct.html#format-characters Refs astral-sh/ty#2437 Refs astral-sh#22562
Recently the `D` and `F` format characters for complex types were added to `struct.unpack()`. See the following links for details: - https://docs.python.org/3/whatsnew/3.14.html#struct - https://docs.python.org/3/library/struct.html#format-characters This adds support for these new formats when using Python 3.14+ Refs astral-sh/ty#2437 Refs astral-sh#22562
## Summary Recently the `D` and `F` format characters for complex types were added to `struct.unpack()`. See the following links for details: - https://docs.python.org/3/whatsnew/3.14.html#struct - https://docs.python.org/3/library/struct.html#format-characters This adds support for these new formats when using Python 3.14+ Refs astral-sh/ty#2437 Refs #22562 ## Test Plan Update to existing mdtest.
Fixes astral-sh/ty#2437
Summary
Allow the checker to analyze the format string for
struct.unpackto ensure correct types are generated instead ofAny.Test Plan
Added mdtest cases from original issue.