Skip to content

Commit 59828df

Browse files
rchen152facebook-github-bot
authored andcommitted
Extract a helper to generate a (key, default: _T) -> ValueType | _T overload
Summary: Another code cleanup. Reviewed By: samwgoldman Differential Revision: D80870287 fbshipit-source-id: e486f12d706fa37b774c5ab52d1257297199d8e1
1 parent 24d82f3 commit 59828df

1 file changed

Lines changed: 46 additions & 61 deletions

File tree

pyrefly/lib/alt/class/typed_dict.rs

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,44 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
347347
}))
348348
}
349349

350+
/// Get a (key, default: T) -> ValueType | T overload.
351+
fn get_overload_with_default(
352+
&self,
353+
metadata: &FuncMetadata,
354+
self_param: &Param,
355+
name: Option<&Name>,
356+
ty: Type,
357+
) -> OverloadType {
358+
let q = Quantified::type_var(
359+
Name::new("_T"),
360+
self.uniques,
361+
None,
362+
Restriction::Unrestricted,
363+
);
364+
let tparams = vec![TParam {
365+
quantified: q.clone(),
366+
variance: PreInferenceVariance::PInvariant,
367+
}];
368+
OverloadType::Forall(Forall {
369+
tparams: Arc::new(TParams::new(tparams)),
370+
body: Function {
371+
signature: Callable::list(
372+
ParamList::new(vec![
373+
self_param.clone(),
374+
self.key_param(name),
375+
Param::PosOnly(
376+
Some(DEFAULT_PARAM.clone()),
377+
q.clone().to_type(),
378+
Required::Required,
379+
),
380+
]),
381+
self.union(ty, q.to_type()),
382+
),
383+
metadata: metadata.clone(),
384+
},
385+
})
386+
}
387+
350388
fn get_typed_dict_get(
351389
&self,
352390
cls: &Class,
@@ -390,36 +428,12 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
390428
metadata: metadata.clone(),
391429
}));
392430
// (self, key: Literal["key"], default: T) -> ValueType | T
393-
let q = Quantified::type_var(
394-
Name::new("_T"),
395-
self.uniques,
396-
None,
397-
Restriction::Unrestricted,
398-
);
399-
400-
let tparams = vec![TParam {
401-
quantified: q.clone(),
402-
variance: PreInferenceVariance::PInvariant,
403-
}];
404-
405-
literal_signatures.push(OverloadType::Forall(Forall {
406-
tparams: Arc::new(TParams::new(tparams)),
407-
body: Function {
408-
signature: Callable::list(
409-
ParamList::new(vec![
410-
self_param.clone(),
411-
key_param.clone(),
412-
Param::PosOnly(
413-
Some(DEFAULT_PARAM.clone()),
414-
q.clone().to_type(),
415-
Required::Required,
416-
),
417-
]),
418-
self.union(field.ty.clone(), q.to_type()),
419-
),
420-
metadata: metadata.clone(),
421-
},
422-
}));
431+
literal_signatures.push(self.get_overload_with_default(
432+
&metadata,
433+
&self_param,
434+
Some(name),
435+
field.ty,
436+
));
423437
}
424438
}
425439
let signatures = Vec1::from_vec_push(
@@ -476,47 +490,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
476490
// do not pop required or read-only keys
477491
return;
478492
}
479-
let key_param = self.key_param(name);
480-
481-
let q = Quantified::type_var(
482-
Name::new("_T"),
483-
self.uniques,
484-
None,
485-
Restriction::Unrestricted,
486-
);
487-
let tparams = vec![TParam {
488-
quantified: q.clone(),
489-
variance: PreInferenceVariance::PInvariant,
490-
}];
491493

492494
// 1) no default: (self, key: Literal["field_name"]) -> FieldType
493495
overloads.push(OverloadType::Function(Function {
494496
signature: Callable::list(
495-
ParamList::new(vec![self_param.clone(), key_param.clone()]),
497+
ParamList::new(vec![self_param.clone(), self.key_param(name)]),
496498
ty.clone(),
497499
),
498500
metadata: metadata.clone(),
499501
}));
500502

501503
// 2) default: (self, key: Literal["field_name"], default: _T) -> FieldType | _T
502-
overloads.push(OverloadType::Forall(Forall {
503-
tparams: Arc::new(TParams::new(tparams.clone())),
504-
body: Function {
505-
signature: Callable::list(
506-
ParamList::new(vec![
507-
self_param.clone(),
508-
key_param.clone(),
509-
Param::PosOnly(
510-
Some(DEFAULT_PARAM.clone()),
511-
q.clone().to_type(),
512-
Required::Required,
513-
),
514-
]),
515-
self.union(ty, q.clone().to_type()),
516-
),
517-
metadata: metadata.clone(),
518-
},
519-
}));
504+
overloads.push(self.get_overload_with_default(metadata, self_param, name, ty));
520505
}
521506

522507
/// Synthesize a method for every non-required field. Thus, this method returns None if all fields are required since no methods are synthesized

0 commit comments

Comments
 (0)