[ty] Add assertions to ensure that we never call KnownClass::Tuple.to_instance() or similar#21027
Merged
AlexWaygood merged 1 commit intomainfrom Oct 22, 2025
Merged
[ty] Add assertions to ensure that we never call KnownClass::Tuple.to_instance() or similar#21027AlexWaygood merged 1 commit intomainfrom
KnownClass::Tuple.to_instance() or similar#21027AlexWaygood merged 1 commit intomainfrom
Conversation
Contributor
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
Contributor
|
…o_instance()` or similar
79b02bb to
1a6311a
Compare
dcreager
added a commit
that referenced
this pull request
Oct 22, 2025
* main: (65 commits) [ty] Some more simplifications when rendering constraint sets (#21009) [ty] Make `attributes.md` mdtests faster (#21030) [ty] Set `INSTA_FORCE_PASS` and `INSTA_OUTPUT` environment variables from mdtest.py (#21029) [ty] Fall back to `Divergent` for deeply nested specializations (#20988) [`ruff`] Autogenerate TypeParam nodes (#21028) [ty] Add assertions to ensure that we never call `KnownClass::Tuple.to_instance()` or similar (#21027) [`ruff`] Auto generate ast Pattern nodes (#21024) [`flake8-simplify`] Skip `SIM911` when unknown arguments are present (#20697) Render a diagnostic for syntax errors introduced in formatter tests (#21021) [ty] Support goto-definition on vendored typeshed stubs (#21020) [ty] Implement go-to for binary and unary operators (#21001) [ty] Avoid ever-growing default types (#20991) [syntax-errors] Name is parameter and global (#20426) [ty] Disable panicking mdtest (#21016) [ty] Fix completions at end of file (#20993) [ty] Fix out-of-order semantic token for function with regular argument after kwargs (#21013) [ty] Fix auto import for files with `from __future__` import (#20987) [`fastapi`] Handle ellipsis defaults in FAST002 autofix (`FAST002`) (#20810) [`ruff`] Skip autofix for keyword and `__debug__` path params (#20960) [`flake8-bugbear`] Skip `B905` and `B912` if <2 iterables and no starred arguments (#20998) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This came up in #20988. It's usually a mistake to call
KnownClass::Tuple.to_instance(); it will usually be more efficient and more correct to callType::homogeneous_tuple()orType::heterogeneous_tuple()instead. Add some debug assertions to ensure that we don't make this mistake.I considered making these non-debug assertions, as the equality check should be pretty cheap, but
KnownClass::to_instance()is a pretty hot method, and I think it's exceedingly likely that debug assertions will catch this bug cropping up in the future.I also removed the private helper method
Type::non_tuple_instance()frominstance.rs. It wasn't a zero-cost abstraction, it didn't save that much code, and it had to have a big warning doc-comment above it telling us never to make it public to code outside the module. All things considered, it was no longer worth it to keep it around.Test Plan
cargo test -p ty_python_semantic