mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-27 02:16:54 +00:00
[ty] Respect dataclass_transform parameters for metaclass-based models (#20780)
Some checks are pending
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 / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (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 (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
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 / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (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 (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary Respect parameters such as `frozen_default` for metaclass-based `@dataclass_transformer` models. Related to: https://github.com/astral-sh/ty/issues/1260 ## Typing conformance changes Those are all correct (new true positives) ## Test Plan New Markdown tests
This commit is contained in:
parent
f0d0b57900
commit
75f3c0e8e6
3 changed files with 151 additions and 26 deletions
|
|
@ -176,7 +176,7 @@ fn try_metaclass_cycle_initial<'db>(
|
|||
#[derive(Clone, Copy, Debug, PartialEq, salsa::Update, get_size2::GetSize)]
|
||||
pub(crate) enum CodeGeneratorKind {
|
||||
/// Classes decorated with `@dataclass` or similar dataclass-like decorators
|
||||
DataclassLike,
|
||||
DataclassLike(Option<DataclassTransformerParams>),
|
||||
/// Classes inheriting from `typing.NamedTuple`
|
||||
NamedTuple,
|
||||
/// Classes inheriting from `typing.TypedDict`
|
||||
|
|
@ -194,12 +194,10 @@ impl CodeGeneratorKind {
|
|||
db: &'db dyn Db,
|
||||
class: ClassLiteral<'db>,
|
||||
) -> Option<CodeGeneratorKind> {
|
||||
if class.dataclass_params(db).is_some()
|
||||
|| class
|
||||
.try_metaclass(db)
|
||||
.is_ok_and(|(_, transformer_params)| transformer_params.is_some())
|
||||
{
|
||||
Some(CodeGeneratorKind::DataclassLike)
|
||||
if class.dataclass_params(db).is_some() {
|
||||
Some(CodeGeneratorKind::DataclassLike(None))
|
||||
} else if let Ok((_, Some(transformer_params))) = class.try_metaclass(db) {
|
||||
Some(CodeGeneratorKind::DataclassLike(Some(transformer_params)))
|
||||
} else if class
|
||||
.explicit_bases(db)
|
||||
.contains(&Type::SpecialForm(SpecialFormType::NamedTuple))
|
||||
|
|
@ -233,7 +231,12 @@ impl CodeGeneratorKind {
|
|||
}
|
||||
|
||||
pub(super) fn matches(self, db: &dyn Db, class: ClassLiteral<'_>) -> bool {
|
||||
CodeGeneratorKind::from_class(db, class) == Some(self)
|
||||
matches!(
|
||||
(CodeGeneratorKind::from_class(db, class), self),
|
||||
(Some(Self::DataclassLike(_)), Self::DataclassLike(_))
|
||||
| (Some(Self::NamedTuple), Self::NamedTuple)
|
||||
| (Some(Self::TypedDict), Self::TypedDict)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2152,11 +2155,21 @@ impl<'db> ClassLiteral<'db> {
|
|||
name: &str,
|
||||
) -> Option<Type<'db>> {
|
||||
let dataclass_params = self.dataclass_params(db);
|
||||
let has_dataclass_param =
|
||||
|param| dataclass_params.is_some_and(|params| params.contains(param));
|
||||
|
||||
let field_policy = CodeGeneratorKind::from_class(db, self)?;
|
||||
|
||||
let transformer_params =
|
||||
if let CodeGeneratorKind::DataclassLike(Some(transformer_params)) = field_policy {
|
||||
Some(DataclassParams::from(transformer_params))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let has_dataclass_param = |param| {
|
||||
dataclass_params.is_some_and(|params| params.contains(param))
|
||||
|| transformer_params.is_some_and(|params| params.contains(param))
|
||||
};
|
||||
|
||||
let instance_ty =
|
||||
Type::instance(db, self.apply_optional_specialization(db, specialization));
|
||||
|
||||
|
|
@ -2269,13 +2282,8 @@ impl<'db> ClassLiteral<'db> {
|
|||
};
|
||||
|
||||
match (field_policy, name) {
|
||||
(CodeGeneratorKind::DataclassLike, "__init__") => {
|
||||
let has_synthesized_dunder_init = has_dataclass_param(DataclassParams::INIT)
|
||||
|| self
|
||||
.try_metaclass(db)
|
||||
.is_ok_and(|(_, transformer_params)| transformer_params.is_some());
|
||||
|
||||
if !has_synthesized_dunder_init {
|
||||
(CodeGeneratorKind::DataclassLike(_), "__init__") => {
|
||||
if !has_dataclass_param(DataclassParams::INIT) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
|
@ -2289,7 +2297,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
.with_annotated_type(KnownClass::Type.to_instance(db));
|
||||
signature_from_fields(vec![cls_parameter], Some(Type::none(db)))
|
||||
}
|
||||
(CodeGeneratorKind::DataclassLike, "__lt__" | "__le__" | "__gt__" | "__ge__") => {
|
||||
(CodeGeneratorKind::DataclassLike(_), "__lt__" | "__le__" | "__gt__" | "__ge__") => {
|
||||
if !has_dataclass_param(DataclassParams::ORDER) {
|
||||
return None;
|
||||
}
|
||||
|
|
@ -2332,7 +2340,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
)
|
||||
})
|
||||
}
|
||||
(CodeGeneratorKind::DataclassLike, "__replace__")
|
||||
(CodeGeneratorKind::DataclassLike(_), "__replace__")
|
||||
if Program::get(db).python_version(db) >= PythonVersion::PY313 =>
|
||||
{
|
||||
let self_parameter = Parameter::positional_or_keyword(Name::new_static("self"))
|
||||
|
|
@ -2340,7 +2348,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
|
||||
signature_from_fields(vec![self_parameter], Some(instance_ty))
|
||||
}
|
||||
(CodeGeneratorKind::DataclassLike, "__setattr__") => {
|
||||
(CodeGeneratorKind::DataclassLike(_), "__setattr__") => {
|
||||
if has_dataclass_param(DataclassParams::FROZEN) {
|
||||
let signature = Signature::new(
|
||||
Parameters::new([
|
||||
|
|
@ -2356,7 +2364,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
}
|
||||
None
|
||||
}
|
||||
(CodeGeneratorKind::DataclassLike, "__slots__") => {
|
||||
(CodeGeneratorKind::DataclassLike(_), "__slots__") => {
|
||||
has_dataclass_param(DataclassParams::SLOTS).then(|| {
|
||||
let fields = self.fields(db, specialization, field_policy);
|
||||
let slots = fields.keys().map(|name| Type::string_literal(db, name));
|
||||
|
|
@ -2794,7 +2802,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
|
||||
let kind = match field_policy {
|
||||
CodeGeneratorKind::NamedTuple => FieldKind::NamedTuple { default_ty },
|
||||
CodeGeneratorKind::DataclassLike => FieldKind::Dataclass {
|
||||
CodeGeneratorKind::DataclassLike(_) => FieldKind::Dataclass {
|
||||
default_ty,
|
||||
init_only: attr.is_init_var(),
|
||||
init,
|
||||
|
|
|
|||
|
|
@ -851,7 +851,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
}
|
||||
|
||||
// (6) Check that a dataclass does not have more than one `KW_ONLY`.
|
||||
if let Some(field_policy @ CodeGeneratorKind::DataclassLike) =
|
||||
if let Some(field_policy @ CodeGeneratorKind::DataclassLike(_)) =
|
||||
CodeGeneratorKind::from_class(self.db(), class)
|
||||
{
|
||||
let specialization = None;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue