Skip to content

Commit 163a108

Browse files
InSyncWithFoosharkdp
authored andcommitted
[ty] Support dataclasses.InitVar
1 parent 63d1d33 commit 163a108

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

crates/ty_python_semantic/src/module_resolver/module.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ impl KnownModule {
344344
pub const fn is_importlib(self) -> bool {
345345
matches!(self, Self::ImportLib)
346346
}
347+
348+
pub const fn is_dataclasses(self) -> bool {
349+
matches!(self, Self::Dataclasses)
350+
}
347351
}
348352

349353
impl std::fmt::Display for KnownModule {

crates/ty_python_semantic/src/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5214,7 +5214,7 @@ impl<'db> Type<'db> {
52145214
})
52155215
}
52165216

5217-
SpecialFormType::ClassVar | SpecialFormType::Final => {
5217+
SpecialFormType::ClassVar | SpecialFormType::Final | SpecialFormType::InitVar => {
52185218
Err(InvalidTypeExpressionError {
52195219
invalid_expressions: smallvec::smallvec_inline![
52205220
InvalidTypeExpression::TypeQualifier(*special_form)
@@ -6137,6 +6137,8 @@ bitflags! {
61376137
const CLASS_VAR = 1 << 0;
61386138
/// `typing.Final`
61396139
const FINAL = 1 << 1;
6140+
/// `dataclasses.InitVar`
6141+
const INIT_VAR = 1 << 2;
61406142
}
61416143
}
61426144

crates/ty_python_semantic/src/types/class_base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ impl<'db> ClassBase<'db> {
192192
| SpecialFormType::TypeOf
193193
| SpecialFormType::CallableTypeOf
194194
| SpecialFormType::AlwaysTruthy
195-
| SpecialFormType::AlwaysFalsy => None,
195+
| SpecialFormType::AlwaysFalsy
196+
| SpecialFormType::InitVar => None,
196197

197198
SpecialFormType::Unknown => Some(Self::unknown()),
198199

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9064,6 +9064,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
90649064
Type::SpecialForm(SpecialFormType::Final) => {
90659065
TypeAndQualifiers::new(Type::unknown(), TypeQualifiers::FINAL)
90669066
}
9067+
Type::SpecialForm(SpecialFormType::InitVar) => {
9068+
TypeAndQualifiers::new(Type::unknown(), TypeQualifiers::INIT_VAR)
9069+
}
90679070
_ => name_expr_ty
90689071
.in_type_expression(self.db(), self.scope())
90699072
.unwrap_or_else(|error| {
@@ -9123,7 +9126,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
91239126
}
91249127
}
91259128
Type::SpecialForm(
9126-
type_qualifier @ (SpecialFormType::ClassVar | SpecialFormType::Final),
9129+
type_qualifier @ (SpecialFormType::ClassVar
9130+
| SpecialFormType::Final
9131+
| SpecialFormType::InitVar),
91279132
) => {
91289133
let arguments = if let ast::Expr::Tuple(tuple) = slice {
91299134
&*tuple.elts
@@ -9141,6 +9146,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
91419146
SpecialFormType::Final => {
91429147
type_and_qualifiers.add_qualifier(TypeQualifiers::FINAL);
91439148
}
9149+
SpecialFormType::InitVar => {
9150+
type_and_qualifiers.add_qualifier(TypeQualifiers::INIT_VAR);
9151+
}
91449152
_ => unreachable!(),
91459153
}
91469154
type_and_qualifiers
@@ -10332,7 +10340,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
1033210340
self.infer_type_expression(arguments_slice);
1033310341
todo_type!("`NotRequired[]` type qualifier")
1033410342
}
10335-
SpecialFormType::ClassVar | SpecialFormType::Final => {
10343+
SpecialFormType::ClassVar | SpecialFormType::Final | SpecialFormType::InitVar => {
1033610344
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
1033710345
let diag = builder.into_diagnostic(format_args!(
1033810346
"Type qualifier `{special_form}` is not allowed in type expressions \

crates/ty_python_semantic/src/types/special_form.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub enum SpecialFormType {
105105
TypeIs,
106106
/// The symbol `typing.ReadOnly` (which can also be found as `typing_extensions.ReadOnly`)
107107
ReadOnly,
108+
/// The symbol `dataclasses.InitVar`
109+
InitVar,
108110

109111
/// The symbol `typing.Protocol` (which can also be found as `typing_extensions.Protocol`)
110112
///
@@ -150,7 +152,8 @@ impl SpecialFormType {
150152
| Self::CallableTypeOf
151153
| Self::Protocol // actually `_ProtocolMeta` at runtime but this is what typeshed says
152154
| Self::Generic // actually `type` at runtime but this is what typeshed says
153-
| Self::ReadOnly => KnownClass::SpecialForm,
155+
| Self::ReadOnly
156+
| Self::InitVar => KnownClass::SpecialForm,
154157

155158
Self::List
156159
| Self::Dict
@@ -241,6 +244,8 @@ impl SpecialFormType {
241244
| Self::Intersection
242245
| Self::TypeOf
243246
| Self::CallableTypeOf => module.is_ty_extensions(),
247+
248+
Self::InitVar => module.is_dataclasses(),
244249
}
245250
}
246251

@@ -297,7 +302,8 @@ impl SpecialFormType {
297302
| Self::TypeIs
298303
| Self::ReadOnly
299304
| Self::Protocol
300-
| Self::Generic => false,
305+
| Self::Generic
306+
| Self::InitVar => false,
301307
}
302308
}
303309

@@ -344,6 +350,7 @@ impl SpecialFormType {
344350
SpecialFormType::CallableTypeOf => "ty_extensions.CallableTypeOf",
345351
SpecialFormType::Protocol => "typing.Protocol",
346352
SpecialFormType::Generic => "typing.Generic",
353+
SpecialFormType::InitVar => "dataclasses.InitVar",
347354
}
348355
}
349356
}

0 commit comments

Comments
 (0)