[ty] Recognize __dataclass_transform__ to support SQLModel by rayzeller · Pull Request #23070 · astral-sh/ruff (original) (raw)

@rayzeller @claude

For backwards compatibility with pre-3.11 Python, ty now recognizes any function named __dataclass_transform__ as equivalent to typing.dataclass_transform, regardless of which module it is defined in. This matches pyright's behavior and enables proper type checking for libraries like SQLModel that use this pattern.

See: microsoft/pyright#5402

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

@sharkdp sharkdp marked this pull request as ready for review

February 4, 2026 16:02

@rayzeller @claude

This changes how we extract parameters from dataclass_transform() calls from positional pattern matching to named parameter lookup. This allows custom __dataclass_transform__ functions (like SQLModel's) with different signatures to work correctly.

The previous code used if let [6 params] which silently failed for functions with fewer parameters. Named lookup with defaults is more robust and matches how Python interprets the keyword-only parameters.

Also adds a fallback from field_descriptors to field_specifiers for SQLModel compatibility.

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

@ntBre ntBre added the ty

Multi-file analysis & type inference

label

Feb 5, 2026

@sharkdp

@sharkdp sharkdp changed the title[ty] Recognize __dataclass_transform__ as dataclass_transform [ty] Recognize __dataclass_transform__ to support SQLModel

Feb 5, 2026

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

[ Show hidden characters]({{ revealButtonHref }})