Skip to content

Commit f324586

Browse files
rchen152facebook-github-bot
authored andcommitted
Set TArgs to TParams rather than Any when creating Type::SelfType
Summary: This fixes a few bugs around generic classes. Reviewed By: grievejia Differential Revision: D72358521 fbshipit-source-id: f746c081abbc4a6895c9dc2cf2964f3fced801ec
1 parent 9ec8d76 commit f324586

File tree

7 files changed

+38
-42
lines changed

7 files changed

+38
-42
lines changed

conformance/third_party/conformance.exp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,13 +5484,23 @@
54845484
{
54855485
"code": -2,
54865486
"column": 1,
5487-
"concise_description": "assert_type((Self) -> Self, (Foo7[int]) -> Foo7[int]) failed",
5488-
"description": "assert_type((Self) -> Self, (Foo7[int]) -> Foo7[int]) failed",
5487+
"concise_description": "assert_type(Any, (Foo7[int]) -> Foo7[int]) failed",
5488+
"description": "assert_type(Any, (Foo7[int]) -> Foo7[int]) failed",
54895489
"line": 166,
54905490
"name": "assert-type",
54915491
"stop_column": 57,
54925492
"stop_line": 166
54935493
},
5494+
{
5495+
"code": -2,
5496+
"column": 13,
5497+
"concise_description": "Generic attribute `meth` of class `Foo7` is not visible on the class",
5498+
"description": "Generic attribute `meth` of class `Foo7` is not visible on the class",
5499+
"line": 166,
5500+
"name": "missing-attribute",
5501+
"stop_column": 22,
5502+
"stop_line": 166
5503+
},
54945504
{
54955505
"code": -2,
54965506
"column": 1,
@@ -6959,26 +6969,6 @@
69596969
}
69606970
],
69616971
"generics_typevartuple_basic.py": [
6962-
{
6963-
"code": -2,
6964-
"column": 38,
6965-
"concise_description": "`?Shape` is not assignable to attribute `_shape` with type `Unknown`",
6966-
"description": "`?Shape` is not assignable to attribute `_shape` with type `Unknown`",
6967-
"line": 25,
6968-
"name": "bad-assignment",
6969-
"stop_column": 43,
6970-
"stop_line": 25
6971-
},
6972-
{
6973-
"code": -2,
6974-
"column": 16,
6975-
"concise_description": "Returned type `Unknown` is not assignable to declared return type `?Shape`",
6976-
"description": "Returned type `Unknown` is not assignable to declared return type `?Shape`",
6977-
"line": 28,
6978-
"name": "bad-return",
6979-
"stop_column": 27,
6980-
"stop_line": 28
6981-
},
69826972
{
69836973
"code": -2,
69846974
"column": 34,

conformance/third_party/conformance.result

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
"Line 148: Unexpected errors ['Class `Foo6` uses type variables not specified in `Generic` or `Protocol` base']",
319319
"Line 151: Unexpected errors ['assert_type(type[Foo6[Ellipsis, tuple[str]]], type[Foo6[Ellipsis, tuple[str, (float, bool)]]]) failed', 'Expected a valid ParamSpec expression', 'Expected a valid ParamSpec expression']",
320320
"Line 152: Unexpected errors ['Expected a valid ParamSpec expression', 'Expected a valid ParamSpec expression']",
321-
"Line 166: Unexpected errors ['assert_type((Self) -> Self, (Foo7[int]) -> Foo7[int]) failed']",
321+
"Line 166: Unexpected errors ['assert_type(Any, (Foo7[int]) -> Foo7[int]) failed', 'Generic attribute `meth` of class `Foo7` is not visible on the class']",
322322
"Line 167: Unexpected errors ['assert_type(Any, int) failed', 'Instance-only attribute `attr` of class `Foo7` is not visible on the class']"
323323
],
324324
"generics_defaults_referential.py": [
@@ -417,8 +417,6 @@
417417
"generics_type_erasure.py": [],
418418
"generics_typevartuple_args.py": [],
419419
"generics_typevartuple_basic.py": [
420-
"Line 25: Unexpected errors ['`?Shape` is not assignable to attribute `_shape` with type `Unknown`']",
421-
"Line 28: Unexpected errors ['Returned type `Unknown` is not assignable to declared return type `?Shape`']",
422420
"Line 85: Unexpected errors ['Argument `tuple[float]` is not assignable to parameter `arg2` with type `tuple[int]` in function `func2`']"
423421
],
424422
"generics_typevartuple_callable.py": [],

conformance/third_party/results.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"pass": 41,
44
"fail": 92,
55
"pass_rate": 0.31,
6-
"differences": 473,
6+
"differences": 471,
77
"passing": [
88
"annotations_coroutines.py",
99
"callables_protocol.py",
@@ -100,7 +100,7 @@
100100
"generics_syntax_declarations.py": 5,
101101
"generics_syntax_infer_variance.py": 7,
102102
"generics_syntax_scoping.py": 8,
103-
"generics_typevartuple_basic.py": 3,
103+
"generics_typevartuple_basic.py": 1,
104104
"generics_typevartuple_concat.py": 1,
105105
"generics_typevartuple_specialization.py": 7,
106106
"generics_upper_bound.py": 1,

pyre2/lib/alt/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
179179
None
180180
} else {
181181
defining_cls.as_ref().map(|cls| {
182-
let cls_type = ClassType::new(cls.dupe(), self.create_default_targs(cls, None));
182+
let cls_type = ClassType::new(cls.dupe(), cls.tparams_as_targs());
183183
Type::SelfType(cls_type)
184184
})
185185
};

pyre2/lib/alt/solve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
209209
{
210210
let class = &*self.get_idx(*class_key);
211211
if let Some(cls) = &class.0 {
212-
let cls_type =
213-
ClassType::new(cls.dupe(), self.create_default_targs(cls, None));
212+
let cls_type = ClassType::new(cls.dupe(), cls.tparams_as_targs());
214213
ty.subst_self_special_form_mut(&Type::SelfType(cls_type));
215214
}
216215
}

pyre2/lib/test/constructors.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,45 @@ Box[int]("oops") # E: Argument `Literal['oops']` is not assignable to parameter
5050
);
5151

5252
testcase!(
53-
bug = "Inside __init__, self should be parameterized with the same T as x, instead of defaulting to Unknown",
5453
test_self_in_generic_class,
5554
r#"
5655
from typing import reveal_type
5756
class A[T]:
5857
x: T
5958
def __init__(self, x: T):
6059
reveal_type(self) # E: revealed type: Self@A
61-
reveal_type(self.x) # E: revealed type: Unknown
62-
self.x = 1 # This shouldn't be allowed
60+
reveal_type(self.x) # E: revealed type: ?T
61+
self.x = 1 # E: `Literal[1]` is not assignable to attribute `x` with type `?T`
6362
self.x = x # OK
6463
"#,
6564
);
6665

6766
testcase!(
68-
bug = "int should not be assignable to type var with upper bound = int",
6967
test_bounded_self_in_generic_class,
7068
r#"
7169
from typing import reveal_type
7270
class A[T: int]:
7371
x: T
7472
def __init__(self, x: T):
7573
reveal_type(self) # E: revealed type: Self@A
76-
reveal_type(self.x) # E: revealed type: int
77-
self.x = 1 # Not OK
74+
reveal_type(self.x) # E: revealed type: ?T
75+
self.x = 1 # E: `Literal[1]` is not assignable to attribute `x` with type `?T`
7876
self.x = x # OK
7977
"#,
8078
);
8179

80+
testcase!(
81+
test_typing_self_param_in_generic_class,
82+
r#"
83+
from typing import Self, reveal_type
84+
class A[T]:
85+
x: T
86+
def __init__(self, other: Self):
87+
reveal_type(other.x) # E: revealed type: ?T
88+
self.x = other.x # OK
89+
"#,
90+
);
91+
8292
testcase!(
8393
test_generic_init_in_generic_class,
8494
r#"

pyre2/lib/test/type_var_tuple.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ Ts = TypeVarTuple('Ts')
4848
);
4949

5050
testcase!(
51-
bug = "C2 shouldn't give an error here",
5251
test_type_var_tuple_class_field_and_constructor,
5352
r#"
5453
class C1[T]:
55-
x: tuple[T]
56-
def __init__(self, x: tuple[T]) -> None:
54+
x: tuple[T, ...]
55+
def __init__(self, x: tuple[T, ...]) -> None:
5756
self.x = x
58-
self.y: T = x
57+
self.y: T = x # E: `tuple[?T, ...]` is not assignable to attribute `y` with type `?T`
5958
class C2[*Ts]:
6059
x: tuple[*Ts]
6160
def __init__(self, x: tuple[*Ts]) -> None:
62-
self.x = x # E: `?Ts` is not assignable to attribute `x` with type `Unknown`
63-
self.y: tuple[*Ts] = x # E: `?Ts` is not assignable to attribute `y` with type `Unknown`
61+
self.x = x
62+
self.y: tuple[*Ts] = x
6463
"#,
6564
);
6665

0 commit comments

Comments
 (0)