Skip to content

[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 1 commit into from
Jul 9, 2025
Merged

[ty] dataclasses.field support #19140

merged 1 commit into from
Jul 9, 2025

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Jul 4, 2025

Summary

Add an initial set of tests for dataclasses.field.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Jul 4, 2025
Copy link
Contributor

github-actions bot commented Jul 4, 2025

mypy_primer results

Changes were detected when running on open source projects
trio (https://github.com/python-trio/trio)
-     memo fields = ~156MB
+     memo fields = ~142MB

paasta (https://github.com/yelp/paasta)
-     memo fields = ~156MB
+     memo fields = ~171MB

mkdocs (https://github.com/mkdocs/mkdocs)
- TOTAL MEMORY USAGE: ~129MB
+ TOTAL MEMORY USAGE: ~117MB

isort (https://github.com/pycqa/isort)
- TOTAL MEMORY USAGE: ~88MB
+ TOTAL MEMORY USAGE: ~80MB

# 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
```
Copy link
Contributor

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)

Copy link
Contributor Author

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.field look different from the ones for attrs.field. Here's a simplified version of the dataclasses.field overloads:

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 @Todo type), it works fine:

https://play.ty.dev/3d16e359-69db-4265-b253-5c1d6d0e3b3b

Copy link
Contributor

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.field and dataclasses.field. Thank you for the examples!

The issue I linked above will still come to bite us later on though, since ty should ideally also understand attrs.field.

@sharkdp sharkdp added internal An internal refactor or improvement testing Related to testing Ruff itself labels Jul 9, 2025
@sharkdp sharkdp marked this pull request as ready for review July 9, 2025 07:16
@sharkdp
Copy link
Contributor Author

sharkdp commented Jul 9, 2025

I guess it doesn't hurt to have these tests in place, so we'll notice when something changes in type inference for the return type of field. Also, @abhijeetbodas2001 plans to work on dataclass fields, so this seems like a good place to start.

@sharkdp sharkdp merged commit 68106dd into main Jul 9, 2025
38 checks passed
@sharkdp sharkdp deleted the david/dataclass-fields branch July 9, 2025 07:18
UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request Jul 10, 2025
…re_help

* 'main' of https://github.com/astral-sh/ruff: (34 commits)
  [docs] add capital one to who's using ruff (astral-sh#19248)
  [`pyupgrade`] Keyword arguments in `super` should suppress the `UP008` fix (astral-sh#19131)
  [`flake8-use-pathlib`] Add autofixes for `PTH100`, `PTH106`, `PTH107`, `PTH108`, `PTH110`, `PTH111`, `PTH112`, `PTH113`, `PTH114`, `PTH115`, `PTH117`, `PTH119`, `PTH120` (astral-sh#19213)
  [ty] Do not run `mypy_primer.yaml` when all changed files are Markdown files (astral-sh#19244)
  [`flake8-bandit`] Make example error out-of-the-box (`S412`) (astral-sh#19241)
  [`pydoclint`] Make example error out-of-the-box (`DOC501`) (astral-sh#19218)
  [ty] Add "kind" to completion suggestions
  [ty] Add type information to `all_members` API
  [ty] Expand API of `all_members` to return a struct
  [ty] Ecosystem analyzer PR comment workflow (astral-sh#19237)
  [ty] Merge `ty_macros` into `ruff_macros` (astral-sh#19229)
  [ty] Fix `ClassLiteral.into_callable` for dataclasses (astral-sh#19192)
  [ty] `dataclasses.field` support (astral-sh#19140)
  [ty] Fix panic for attribute expressions with empty value (astral-sh#19069)
  [ty] Return `CallableType` from `BoundMethodType.into_callable_type` (astral-sh#19193)
  [`flake8-bugbear`] Support non-context-manager calls in `B017` (astral-sh#19063)
  [ty] Improved diagnostic for reassignments of `Final` symbols (astral-sh#19214)
  [ty] Use full range for assignment definitions (astral-sh#19211)
  [`pylint`] Update `missing-maxsplit-arg` docs and error to suggest proper usage (`PLC0207`) (astral-sh#18949)
  [ty] Add `set -eu` to mypy-primer script (astral-sh#19212)
  ...

# Conflicts:
#	crates/ty_python_semantic/src/types/class.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement testing Related to testing Ruff itself ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants