-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] dataclasses.field support
#19140
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
crates/ty_python_semantic/resources/mdtest/dataclasses/fields.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Dataclass fields | ||
|
|
||
| ## Basic | ||
|
|
||
| ```py | ||
| from dataclasses import dataclass, field | ||
|
|
||
| @dataclass | ||
| class Member: | ||
| name: str | ||
| role: str = field(default="user") | ||
| tag: str | None = field(default=None, init=False) | ||
|
|
||
| # TODO: this should not include the `tag` parameter, since it has `init=False` set | ||
| # revealed: (self: Member, name: str, role: str = Unknown, tag: str | None = Unknown) -> None | ||
| reveal_type(Member.__init__) | ||
|
|
||
| alice = Member(name="Alice", role="admin") | ||
| reveal_type(alice.role) # revealed: str | ||
| alice.role = "moderator" | ||
|
|
||
| # TODO: this should be an error, `tag` has `init=False` | ||
| bob = Member(name="Bob", tag="VIP") | ||
| ``` | ||
|
|
||
| ## The `field` function | ||
|
|
||
| ```py | ||
| from dataclasses import field | ||
|
|
||
| # TODO: this should be `Literal[1]`. This is currently blocked on enum support, because | ||
| # the `dataclasses.field` overloads make use of a `_MISSING_TYPE` enum, for which we | ||
| # infer a @Todo type, and therefore pick the wrong overload. | ||
| reveal_type(field(default=1)) # revealed: Unknown | ||
| ``` | ||
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.
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 thought this was because of a different issue in the overload evaluation code itself? Ref astral-sh/ty#267 (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.
Thanks, forgot about that. Not sure if it's really the same though? The overloads for
dataclasses.fieldlook different from the ones forattrs.field. Here's a simplified version of thedataclasses.fieldoverloads:https://play.ty.dev/c5424d2e-ac73-4759-a31e-e3fb4eed1420
If I change that to use a simple class as a sentinel value, instead of an enum (for which we infer a dynamic
@Todotype), it works fine:https://play.ty.dev/3d16e359-69db-4265-b253-5c1d6d0e3b3b
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.
Oh yes, these indeed look to be different issues. It seems I was conflating
attrs.fieldanddataclasses.field. Thank you for the examples!The issue I linked above will still come to bite us later on though, since
tyshould ideally also understandattrs.field.