Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/ty_python_semantic/resources/mdtest/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,20 @@ C(1) < C(2) # ok

### Using `dataclass` as a function

To do
```py
from dataclasses import dataclass

class B:
x: int

# error: [missing-argument]
dataclass(B)()

# error: [invalid-argument-type]
dataclass(B)("a")

reveal_type(dataclass(B)(3).x) # revealed: int
```

## Internals

Expand Down
19 changes: 17 additions & 2 deletions crates/ty_python_semantic/src/types/call/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::types::function::{DataclassTransformerParams, FunctionDecorators, Kno
use crate::types::generics::{Specialization, SpecializationBuilder, SpecializationError};
use crate::types::signatures::{Parameter, ParameterForm};
use crate::types::{
BoundMethodType, DataclassParams, KnownClass, KnownInstanceType, MethodWrapperKind,
PropertyInstanceType, SpecialFormType, TupleType, TypeMapping, UnionType,
BoundMethodType, ClassLiteral, DataclassParams, KnownClass, KnownInstanceType,
MethodWrapperKind, PropertyInstanceType, SpecialFormType, TupleType, TypeMapping, UnionType,
WrapperDescriptorKind, ide_support, todo_type,
};
use ruff_db::diagnostic::{Annotation, Diagnostic, Severity, SubDiagnostic};
Expand Down Expand Up @@ -839,6 +839,21 @@ impl<'db> Bindings<'db> {

overload.set_return_type(Type::DataclassDecorator(params));
}

// `dataclass` being used as a non-decorator
if let [Some(Type::ClassLiteral(class_literal))] =
overload.parameter_types()
{
let params = DataclassParams::default();
overload.set_return_type(Type::from(ClassLiteral::new(
db,
class_literal.name(db),
class_literal.body_scope(db),
class_literal.known(db),
Some(params),
class_literal.dataclass_transformer_params(db),
)));
}
}

Some(KnownFunction::DataclassTransform) => {
Expand Down
Loading