Skip to content

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Aug 21, 2025

Summary

Previously we held off from doing this because we weren't sure that it was worth the added complexity cost. But our code has changed in the months since we made that initial decision, and I think the structure of the code is such that it no longer really leads to much added complexity to add precise inference when unpacking a string literal or a bytes literal.

The improved inference we gain from this has real benefits to users (see the mypy_primer report), and this PR doesn't appear to have a performance impact.

Test plan

mdtests

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 21, 2025
Copy link
Contributor

github-actions bot commented Aug 21, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Copy link
Contributor

github-actions bot commented Aug 21, 2025

mypy_primer results

Changes were detected when running on open source projects
scipy (https://github.com/scipy/scipy)
- scipy/linalg/tests/test_lapack.py:171:41: warning[possibly-unresolved-reference] Name `ref` used when possibly not defined
- Found 6401 diagnostics
+ Found 6400 diagnostics
No memory usage changes detected ✅

@AlexWaygood
Copy link
Member Author

- scipy/linalg/tests/test_lapack.py:171:41: warning[possibly-unresolved-reference] Name `ref` used when possibly not defined

Okay, this one's pretty cool -- ty is now able to infer that all possibilities have been exhaustively covered here, meaning the variable cannot possibly be undefined by the time the code reaches the assertion at the bottom: https://github.com/scipy/scipy/blob/99c0ef6af161a4d8157cae5276a20c30b7677c6f/scipy/linalg/tests/test_lapack.py#L147-L171

@AlexWaygood AlexWaygood force-pushed the alex/more-try-iterate branch 2 times, most recently from 0909589 to b8bc2e4 Compare August 22, 2025 10:53
@AlexWaygood AlexWaygood force-pushed the alex/more-try-iterate branch 2 times, most recently from 33fcb10 to c271331 Compare August 22, 2025 11:31
@AlexWaygood AlexWaygood force-pushed the alex/more-try-iterate branch from c271331 to 80712dc Compare August 22, 2025 11:41
@AlexWaygood AlexWaygood changed the title [ty] Add precise tuple specs when iterating over other kinds of types [ty] Add precise unpacking support for string literals and bytes literals Aug 22, 2025
@AlexWaygood AlexWaygood changed the title [ty] Add precise unpacking support for string literals and bytes literals [ty] Add precise iteration and unpacking inference for string literals and bytes literals Aug 22, 2025
@AlexWaygood AlexWaygood marked this pull request as ready for review August 22, 2025 12:38
@carljm
Copy link
Contributor

carljm commented Aug 22, 2025

ty is now able to infer that all possibilities have been exhaustively covered here

This is a good example of the sort of issue that led us to disable possibly-unresolved-reference by default. Maybe at some point we'll be able to reconsider that!

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@AlexWaygood AlexWaygood merged commit bc6ea68 into main Aug 22, 2025
60 of 61 checks passed
@AlexWaygood AlexWaygood deleted the alex/more-try-iterate branch August 22, 2025 18:33
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
…s and bytes literals (astral-sh#20023)

## Summary

Previously we held off from doing this because we weren't sure that it
was worth the added complexity cost. But our code has changed in the
months since we made that initial decision, and I think the structure of
the code is such that it no longer really leads to much added complexity
to add precise inference when unpacking a string literal or a bytes
literal.

The improved inference we gain from this has real benefits to users (see
the mypy_primer report), and this PR doesn't appear to have a
performance impact.

## Test plan

mdtests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants