Skip to content

Commit c3733e1

Browse files
rchen152facebook-github-bot
authored andcommitted
Move TypedDictMetadata calculation into its own function
Reviewed By: stroxler Differential Revision: D80368515 fbshipit-source-id: 53c7c15f1e12369b237611ba69fc926c6e7e9ce1
1 parent 225063c commit c3733e1

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

pyrefly/lib/alt/class/class_metadata.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,30 +216,8 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
216216
format!("`{}` is not a typed dictionary. Typed dictionary definitions may only extend other typed dictionaries.", bad.0.name()),
217217
);
218218
}
219-
let typed_dict_metadata = if is_typed_dict {
220-
// Validate that only 'total' keyword is allowed for TypedDict and determine is_total
221-
let mut is_total = true;
222-
for (name, value) in &keywords {
223-
if name.as_str() != "total" {
224-
self.error(
225-
errors,
226-
cls.range(),
227-
ErrorInfo::Kind(ErrorKind::BadTypedDict),
228-
format!(
229-
"TypedDict does not support keyword argument `{}`",
230-
name.as_str()
231-
),
232-
);
233-
} else if matches!(value, Type::Literal(Lit::Bool(false))) {
234-
is_total = false;
235-
}
236-
}
237-
let fields =
238-
self.calculate_typed_dict_metadata_fields(cls, &bases_with_metadata, is_total);
239-
Some(TypedDictMetadata { fields })
240-
} else {
241-
None
242-
};
219+
let typed_dict_metadata =
220+
self.typed_dict_metadata(cls, &bases_with_metadata, &keywords, is_typed_dict, errors);
243221
let base_metaclasses = bases_with_metadata
244222
.iter()
245223
.filter_map(|(b, metadata)| metadata.metaclass().map(|m| (b.name(), m)))
@@ -481,6 +459,40 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
481459
})
482460
}
483461

462+
fn typed_dict_metadata(
463+
&self,
464+
cls: &Class,
465+
bases_with_metadata: &[(Class, Arc<ClassMetadata>)],
466+
keywords: &[(Name, Type)],
467+
is_typed_dict: bool,
468+
errors: &ErrorCollector,
469+
) -> Option<TypedDictMetadata> {
470+
if is_typed_dict {
471+
// Validate that only 'total' keyword is allowed for TypedDict and determine is_total
472+
let mut is_total = true;
473+
for (name, value) in keywords {
474+
if name.as_str() != "total" {
475+
self.error(
476+
errors,
477+
cls.range(),
478+
ErrorInfo::Kind(ErrorKind::BadTypedDict),
479+
format!(
480+
"TypedDict does not support keyword argument `{}`",
481+
name.as_str()
482+
),
483+
);
484+
} else if matches!(value, Type::Literal(Lit::Bool(false))) {
485+
is_total = false;
486+
}
487+
}
488+
let fields =
489+
self.calculate_typed_dict_metadata_fields(cls, bases_with_metadata, is_total);
490+
Some(TypedDictMetadata { fields })
491+
} else {
492+
None
493+
}
494+
}
495+
484496
fn enum_metadata(
485497
&self,
486498
cls: &Class,

0 commit comments

Comments
 (0)