[ty] support kw_only=True for dataclass() and field() (#19677)
Some checks are pending
CI / mkdocs (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary
https://github.com/astral-sh/ty/issues/111

adds support for `@dataclass(kw_only=True)`
(https://docs.python.org/3/library/dataclasses.html)

## Test Plan
- new mdtests
- triaged conformance diffs (notes here:
https://diffswarm.dev/d-01k2gknwyq82f6x17zqf3apjxc)
- `mypy_primer` no-op
This commit is contained in:
justin 2025-08-14 11:02:55 -04:00 committed by GitHub
parent 9aaa82d037
commit dc2e8ab377
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 155 additions and 10 deletions

View file

@ -6787,6 +6787,9 @@ pub struct FieldInstance<'db> {
/// Whether this field is part of the `__init__` signature, or not.
pub init: bool,
/// Whether or not this field can only be passed as a keyword argument to `__init__`.
pub kw_only: Option<bool>,
}
// The Salsa heap is tracked separately.
@ -6798,6 +6801,7 @@ impl<'db> FieldInstance<'db> {
db,
self.default_type(db).normalized_impl(db, visitor),
self.init(db),
self.kw_only(db),
)
}
}

View file

@ -12,6 +12,7 @@ use ruff_db::parsed::parsed_module;
use smallvec::{SmallVec, smallvec, smallvec_inline};
use super::{Argument, CallArguments, CallError, CallErrorKind, InferContext, Signature, Type};
use crate::Program;
use crate::db::Db;
use crate::dunder_all::dunder_all_names;
use crate::place::{Boundness, Place};
@ -33,7 +34,7 @@ use crate::types::{
WrapperDescriptorKind, enums, ide_support, todo_type,
};
use ruff_db::diagnostic::{Annotation, Diagnostic, SubDiagnostic, SubDiagnosticSeverity};
use ruff_python_ast as ast;
use ruff_python_ast::{self as ast, PythonVersion};
/// Binding information for a possible union of callables. At a call site, the arguments must be
/// compatible with _all_ of the types in the union for the call to be valid.
@ -860,7 +861,11 @@ impl<'db> Bindings<'db> {
params |= DataclassParams::MATCH_ARGS;
}
if to_bool(kw_only, false) {
params |= DataclassParams::KW_ONLY;
if Program::get(db).python_version(db) >= PythonVersion::PY310 {
params |= DataclassParams::KW_ONLY;
} else {
// TODO: emit diagnostic
}
}
if to_bool(slots, false) {
params |= DataclassParams::SLOTS;
@ -919,7 +924,9 @@ impl<'db> Bindings<'db> {
}
Some(KnownFunction::Field) => {
if let [default, default_factory, init, ..] = overload.parameter_types()
// TODO this will break on Python 3.14 -- we should match by parameter name instead
if let [default, default_factory, init, .., kw_only] =
overload.parameter_types()
{
let default_ty = match (default, default_factory) {
(Some(default_ty), _) => *default_ty,
@ -933,6 +940,14 @@ impl<'db> Bindings<'db> {
.map(|init| !init.bool(db).is_always_false())
.unwrap_or(true);
let kw_only = if Program::get(db).python_version(db)
>= PythonVersion::PY310
{
kw_only.map(|kw_only| !kw_only.bool(db).is_always_false())
} else {
None
};
// `typeshed` pretends that `dataclasses.field()` returns the type of the
// default value directly. At runtime, however, this function returns an
// instance of `dataclasses.Field`. We also model it this way and return
@ -942,7 +957,7 @@ impl<'db> Bindings<'db> {
// to `T`. Otherwise, we would error on `name: str = field(default="")`.
overload.set_return_type(Type::KnownInstance(
KnownInstanceType::Field(FieldInstance::new(
db, default_ty, init,
db, default_ty, init, kw_only,
)),
));
}

View file

@ -1171,6 +1171,9 @@ pub(crate) struct Field<'db> {
/// Whether or not this field should appear in the signature of `__init__`.
pub(crate) init: bool,
/// Whether or not this field can only be passed as a keyword argument to `__init__`.
pub(crate) kw_only: Option<bool>,
}
/// Representation of a class definition statement in the AST: either a non-generic class, or a
@ -1922,6 +1925,7 @@ impl<'db> ClassLiteral<'db> {
mut default_ty,
init_only: _,
init,
kw_only,
},
) in self.fields(db, specialization, field_policy)
{
@ -1986,7 +1990,12 @@ impl<'db> ClassLiteral<'db> {
}
}
let mut parameter = if kw_only_field_seen || name == "__replace__" {
let is_kw_only = name == "__replace__"
|| kw_only.unwrap_or(
has_dataclass_param(DataclassParams::KW_ONLY) || kw_only_field_seen,
);
let mut parameter = if is_kw_only {
Parameter::keyword_only(field_name)
} else {
Parameter::positional_or_keyword(field_name)
@ -2005,6 +2014,10 @@ impl<'db> ClassLiteral<'db> {
parameters.push(parameter);
}
// In the event that we have a mix of keyword-only and positional parameters, we need to sort them
// so that the keyword-only parameters appear after positional parameters.
parameters.sort_by_key(Parameter::is_keyword_only);
let mut signature = Signature::new(Parameters::new(parameters), return_ty);
signature.inherited_generic_context = self.generic_context(db);
Some(CallableType::function_like(db, signature))
@ -2316,9 +2329,11 @@ impl<'db> ClassLiteral<'db> {
default_ty.map(|ty| ty.apply_optional_specialization(db, specialization));
let mut init = true;
let mut kw_only = None;
if let Some(Type::KnownInstance(KnownInstanceType::Field(field))) = default_ty {
default_ty = Some(field.default_type(db));
init = field.init(db);
kw_only = field.kw_only(db);
}
attributes.insert(
@ -2328,6 +2343,7 @@ impl<'db> ClassLiteral<'db> {
default_ty,
init_only: attr.is_init_var(),
init,
kw_only,
},
);
}