-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Infer list[T] when unpacking non-tuple type
#18438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
list[T] when unpacking non-literal listlist[T] when unpacking non-tuple type
81fca33 to
bdf83fd
Compare
|
Yeah, I noticed that as well. I'm not exactly sure why that is happening. I plan to look at it in a bit. |
|
All of the diagnostics removed in the ecosystem analysis are false positives because ty was inferring |
AlexWaygood
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with the unpacking code, but this looks reasonable and the primer report looks great!
Some other edge cases that look like they're maybe not-yet covered in unpacking.md, and could be good to add as tests. It looks like we have the correct behaviour on all of them:
() = []
[] = ()
(*a,) = ()
reveal_type(a) # revealed: list[Unknown]
(*a,) = (1,)
reveal_type(b) # revealed: list[Literal[1]]
Thanks! I'm not exactly sure what's the value of the empty list / tuple test case. What is it that you had in mind to test that for? I remember there being similar test cases in the corpus source which seems more useful for these as it would test whether the inference is being done as expected or not. The single unpacking elements examples that you've provided is being tested by way of being surrounded by other elements, not in isolation. For example, this is the I'll go ahead and merge this PR but happy to follow-up with the empty list / tuple test cases if you think it's useful. |
It's just a weird edge case that only works by virtue of unpacking at runtime: a zero-length list on the right-hand side is assigned to a zero-length target list on the right-hand side. It's useful to show that we model the runtime accurately: that we understand such code as valid and do not emit any diagnostics on it. |
…aration * origin/main: [ty] Infer `list[T]` when unpacking non-tuple type (#18438) [ty] Meta-type of type variables should be type[..] (#18439) [`pyupgrade`] Make fix unsafe if it deletes comments (`UP050`) (#18390) [`pyupgrade`] Make fix unsafe if it deletes comments (`UP004`) (#18393) [ty] Support using legacy typing aliases for generic classes in type annotations (#18404) Use ty's completions in playground (#18425) Update editor setup docs about Neovim and Vim (#18324) Update NPM Development dependencies (#18423) Infer `list[T]` for starred target in unpacking (#18401) [`refurb`] Mark `FURB180` fix unsafe when class has bases (#18149) [`fastapi`] Avoid false positive for class dependencies (`FAST003`) (#18271)
## Summary This PR is to address this comment: #18438 (comment) ## Test Plan Run mdtest
* main: [ty] Add tests for empty list/tuple unpacking (astral-sh#18451) [ty] Argument type expansion for overload call evaluation (astral-sh#18382) [ty] Minor cleanup for `site-packages` discovery logic (astral-sh#18446) [ty] Add generic inference for dataclasses (astral-sh#18443) [ty] dataclasses: Allow using dataclasses.dataclass as a function. (astral-sh#18440) [ty] Create separate `FunctionLiteral` and `FunctionType` types (astral-sh#18360) [ty] Infer `list[T]` when unpacking non-tuple type (astral-sh#18438) [ty] Meta-type of type variables should be type[..] (astral-sh#18439) [`pyupgrade`] Make fix unsafe if it deletes comments (`UP050`) (astral-sh#18390) [`pyupgrade`] Make fix unsafe if it deletes comments (`UP004`) (astral-sh#18393) [ty] Support using legacy typing aliases for generic classes in type annotations (astral-sh#18404) Use ty's completions in playground (astral-sh#18425) Update editor setup docs about Neovim and Vim (astral-sh#18324) Update NPM Development dependencies (astral-sh#18423)

Summary
Follow-up from #18401, I was looking at whether that would fix the issue at astral-sh/ty#247 (comment) and it didn't, which made me realize that the PR only inferred
list[T]when the value type was tuple but it could be other types as well.This PR fixes the actual issue by inferring
list[T]for the non-tuple type case.Test Plan
Add test cases for starred expression involved with non-tuple type. I also added a few test cases for list type and list literal.
I also verified that the example in the linked issue comment works: