Skip to content

Commit c5af2a8

Browse files
migeed-zfacebook-github-bot
authored andcommitted
Make Type Params carry pre inference variance info instead of post inference info
Summary: We are constructing type parameters in the class definition, function definitions and type aliases constructions. We don't need post inference variance information there as we can see from this diff (I think we don't need variance info at all there). We need variance info for the subtype check. In this diff, I am using preinfence variance everywhere, because at all those places we mentioned above, we don't have enough info to infer variance. And when it's time to do the subtyping check, I convert the representation. Things to look into: 1- I need to find a way to somehow thread a variance map to the subtyping check, where I can query the specific parameter I want by supplying the class name and the parameter I need. Right now, I don't see the class info there, so that's the next thing to look into 2- Do we need the variance info at all in TParam and TParamInfo? my guess is no, but I can't get rid of it until I figure out item #1. Reviewed By: rchen152 Differential Revision: D75485457 fbshipit-source-id: bcf530a1a61661e38dfb8c972b6ab2c6fa90a6ae
1 parent 6dd81f7 commit c5af2a8

File tree

6 files changed

+10
-11
lines changed

6 files changed

+10
-11
lines changed

pyrefly/lib/alt/class/typed_dict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use crate::types::class::Class;
3737
use crate::types::class::Substitution;
3838
use crate::types::literal::Lit;
3939
use crate::types::quantified::Quantified;
40+
use crate::types::type_var::PreInferenceVariance;
4041
use crate::types::type_var::Restriction;
41-
use crate::types::type_var::Variance;
4242
use crate::types::typed_dict::TypedDict;
4343
use crate::types::typed_dict::TypedDictField;
4444
use crate::types::types::Forall;
@@ -261,7 +261,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
261261

262262
let tparams = vec![TParam {
263263
quantified: q.clone(),
264-
variance: Variance::Invariant,
264+
variance: PreInferenceVariance::PInvariant,
265265
}];
266266

267267
literal_signatures.push(OverloadType::Forall(Forall {

pyrefly/lib/alt/solve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::alt::answers::AnswersSolver;
2929
use crate::alt::answers::LookupAnswer;
3030
use crate::alt::callable::CallArg;
3131
use crate::alt::class::class_field::ClassField;
32-
use crate::alt::class::variance_inference::pre_to_post_variance;
3332
use crate::alt::types::class_metadata::ClassMetadata;
3433
use crate::alt::types::class_metadata::ClassSynthesizedFields;
3534
use crate::alt::types::decorated_function::DecoratedFunction;
@@ -1018,7 +1017,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
10181017
quantified: tparam.quantified,
10191018
// Classes set the variance before getting here. For functions and aliases, the variance isn't meaningful;
10201019
// it doesn't matter what we set it to as long as we make it non-None to indicate that it's not missing.
1021-
variance: pre_to_post_variance(tparam.variance),
1020+
variance: tparam.variance,
10221021
});
10231022
}
10241023

pyrefly/lib/solver/subset.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ruff_python_ast::name::Name;
1616
use starlark_map::small_map::SmallMap;
1717

1818
use crate::alt::answers::LookupAnswer;
19+
use crate::alt::class::variance_inference::pre_to_post_variance;
1920
use crate::dunder;
2021
use crate::solver::solver::Subset;
2122
use crate::types::callable::Callable;
@@ -1012,7 +1013,8 @@ impl<'a, Ans: LookupAnswer> Subset<'a, Ans> {
10121013
let result = if param.quantified.kind() == QuantifiedKind::TypeVarTuple {
10131014
self.is_equal(got_arg, want_arg)
10141015
} else {
1015-
match param.variance {
1016+
let param_variance = pre_to_post_variance(param.variance);
1017+
match param_variance {
10161018
Variance::Covariant => self.is_subset_eq(got_arg, want_arg),
10171019
Variance::Contravariant => self.is_subset_eq(want_arg, got_arg),
10181020
Variance::Invariant => self.is_equal(got_arg, want_arg),

pyrefly/lib/types/display.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ pub mod tests {
364364
use crate::types::type_var::PreInferenceVariance;
365365
use crate::types::type_var::Restriction;
366366
use crate::types::type_var::TypeVar;
367-
use crate::types::type_var::Variance;
368367
use crate::types::typed_dict::TypedDict;
369368
use crate::types::types::TParam;
370369
use crate::types::types::TParams;
@@ -396,7 +395,7 @@ pub mod tests {
396395
default: None,
397396
},
398397
),
399-
variance: Variance::Invariant,
398+
variance: PreInferenceVariance::PInvariant,
400399
}
401400
}
402401

pyrefly/lib/types/equality.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ mod tests {
273273
use crate::types::quantified::Quantified;
274274
use crate::types::quantified::QuantifiedInfo;
275275
use crate::types::quantified::QuantifiedKind;
276+
use crate::types::type_var::PreInferenceVariance;
276277
use crate::types::type_var::Restriction;
277-
use crate::types::type_var::Variance;
278278
use crate::types::types::Forallable;
279279
use crate::types::types::TParam;
280280
use crate::types::types::TParams;
@@ -362,7 +362,7 @@ mod tests {
362362

363363
let tparams = TParams::new(vec![TParam {
364364
quantified: q.clone(),
365-
variance: Variance::Invariant,
365+
variance: PreInferenceVariance::PInvariant,
366366
}]);
367367

368368
Forallable::Function(Function {

pyrefly/lib/types/types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use crate::types::tuple::Tuple;
4747
use crate::types::type_var::PreInferenceVariance;
4848
use crate::types::type_var::Restriction;
4949
use crate::types::type_var::TypeVar;
50-
use crate::types::type_var::Variance;
5150
use crate::types::type_var_tuple::TypeVarTuple;
5251
use crate::types::typed_dict::TypedDict;
5352

@@ -87,7 +86,7 @@ pub struct TParamInfo {
8786
#[derive(Visit, VisitMut, TypeEq)]
8887
pub struct TParam {
8988
pub quantified: Quantified,
90-
pub variance: Variance,
89+
pub variance: PreInferenceVariance,
9190
}
9291

9392
impl Display for TParam {

0 commit comments

Comments
 (0)