From 2c6ddb342221dbbeb7d4c15ce4317be7fa90c420 Mon Sep 17 00:00:00 2001 From: "Rebecca Chen (Python)" Date: Thu, 2 Jan 2025 10:42:36 -0800 Subject: [PATCH] Fix ClassDef/ClassType mismatch causing assert_type failures Reviewed By: ndmitchell Differential Revision: D67764061 fbshipit-source-id: 05e0c6dfc8d541144e62bc8f658062330daff875 --- pyre2/conformance/third_party/conformance.exp | 40 +++++-------------- .../third_party/conformance.result | 14 +++---- pyre2/pyre2/bin/alt/expr.rs | 5 ++- pyre2/pyre2/bin/test/narrow.rs | 6 +-- pyre2/pyre2/bin/test/simple.rs | 9 +++++ 5 files changed, 32 insertions(+), 42 deletions(-) diff --git a/pyre2/conformance/third_party/conformance.exp b/pyre2/conformance/third_party/conformance.exp index a6cfd15fe9..425b80a2d6 100644 --- a/pyre2/conformance/third_party/conformance.exp +++ b/pyre2/conformance/third_party/conformance.exp @@ -9537,8 +9537,8 @@ { "code": -2, "column": 1, - "concise_description": "assert_type(type[NoNonDefaults], type[NoNonDefaults[str, int]]) failed", - "description": "assert_type(type[NoNonDefaults], type[NoNonDefaults[str, int]]) failed", + "concise_description": "assert_type(type[NoNonDefaults[Error, Error]], type[NoNonDefaults[str, int]]) failed", + "description": "assert_type(type[NoNonDefaults[Error, Error]], type[NoNonDefaults[str, int]]) failed", "line": 30, "name": "PyreError", "stop_column": 58, @@ -9547,8 +9547,8 @@ { "code": -2, "column": 1, - "concise_description": "assert_type(type[AllTheDefaults], type[AllTheDefaults[Any, Any, str, int, bool]]) failed", - "description": "assert_type(type[AllTheDefaults], type[AllTheDefaults[Any, Any, str, int, bool]]) failed", + "concise_description": "assert_type(type[AllTheDefaults[Error, Error, Error, Error, Error]], type[AllTheDefaults[Any, Any, str, int, bool]]) failed", + "description": "assert_type(type[AllTheDefaults[Error, Error, Error, Error, Error]], type[AllTheDefaults[Any, Any, str, int, bool]]) failed", "line": 45, "name": "PyreError", "stop_column": 76, @@ -9574,16 +9574,6 @@ "stop_column": 39, "stop_line": 76 }, - { - "code": -2, - "column": 1, - "concise_description": "assert_type(type[Class_ParamSpec], type[Class_ParamSpec]) failed", - "description": "assert_type(type[Class_ParamSpec], type[Class_ParamSpec]) failed", - "line": 79, - "name": "PyreError", - "stop_column": 62, - "stop_line": 79 - }, { "code": -2, "column": 35, @@ -9644,16 +9634,6 @@ "stop_column": 44, "stop_line": 91 }, - { - "code": -2, - "column": 1, - "concise_description": "assert_type(type[Class_TypeVarTuple], type[Class_TypeVarTuple]) failed", - "description": "assert_type(type[Class_TypeVarTuple], type[Class_TypeVarTuple]) failed", - "line": 94, - "name": "PyreError", - "stop_column": 76, - "stop_line": 94 - }, { "code": -2, "column": 38, @@ -9879,8 +9859,8 @@ { "code": -2, "column": 1, - "concise_description": "assert_type(type[slice], type[slice[int, int, int | None]]) failed", - "description": "assert_type(type[slice], type[slice[int, int, int | None]]) failed", + "concise_description": "assert_type(type[slice[Error, Error, Error]], type[slice[int, int, int | None]]) failed", + "description": "assert_type(type[slice[Error, Error, Error]], type[slice[int, int, int | None]]) failed", "line": 23, "name": "PyreError", "stop_column": 54, @@ -9939,8 +9919,8 @@ { "code": -2, "column": 1, - "concise_description": "assert_type(type[Bar], type[Bar[Any, list[Any]]]) failed", - "description": "assert_type(type[Bar], type[Bar[Any, list[Any]]]) failed", + "concise_description": "assert_type(type[Bar[Error, Error]], type[Bar[Any, list[Any]]]) failed", + "description": "assert_type(type[Bar[Error, Error]], type[Bar[Any, list[Any]]]) failed", "line": 94, "name": "PyreError", "stop_column": 44, @@ -9991,8 +9971,8 @@ { "code": -2, "column": 1, - "concise_description": "assert_type(type[Bar], type[Bar[str]]) failed", - "description": "assert_type(type[Bar], type[Bar[str]]) failed", + "concise_description": "assert_type(type[Bar[Error]], type[Bar[str]]) failed", + "description": "assert_type(type[Bar[Error]], type[Bar[str]]) failed", "line": 45, "name": "PyreError", "stop_column": 33, diff --git a/pyre2/conformance/third_party/conformance.result b/pyre2/conformance/third_party/conformance.result index 3d85885248..bb58eb1fbd 100644 --- a/pyre2/conformance/third_party/conformance.result +++ b/pyre2/conformance/third_party/conformance.result @@ -676,14 +676,14 @@ "generics_defaults.py": [ "Line 104: Expected 1 errors", "Line 111: Expected 1 errors", - "Line 30: Unexpected errors ['assert_type(type[NoNonDefaults], type[NoNonDefaults[str, int]]) failed']", - "Line 45: Unexpected errors ['assert_type(type[AllTheDefaults], type[AllTheDefaults[Any, Any, str, int, bool]]) failed']", + "Line 30: Unexpected errors ['assert_type(type[NoNonDefaults[Error, Error]], type[NoNonDefaults[str, int]]) failed']", + "Line 45: Unexpected errors ['assert_type(type[AllTheDefaults[Error, Error, Error, Error, Error]], type[AllTheDefaults[Any, Any, str, int, bool]]) failed']", "Line 76: Unexpected errors ['untype, got ParamSpec']", - "Line 79: Unexpected errors ['assert_type(type[Class_ParamSpec], type[Class_ParamSpec]) failed', 'Expected 0 type arguments for class `Class_ParamSpec`, got 2.']", + "Line 79: Unexpected errors ['Expected 0 type arguments for class `Class_ParamSpec`, got 2.']", "Line 80: Unexpected errors ['Expected 0 type arguments for class `Class_ParamSpec`, got 2.']", "Line 81: Unexpected errors ['Expected 0 type arguments for class `Class_ParamSpec`, got 1.', 'untype, got list[type[bool]]', 'Expected 0 type arguments for class `Class_ParamSpec`, got 2.']", "Line 91: Unexpected errors ['TODO: Starred(ExprStarred - Answers::expr_infer']", - "Line 94: Unexpected errors ['assert_type(type[Class_TypeVarTuple], type[Class_TypeVarTuple]) failed', 'Expected 0 type arguments for class `Class_TypeVarTuple`, got 1.', 'TODO: Starred(ExprStarred - Answers::expr_infer']", + "Line 94: Unexpected errors ['Expected 0 type arguments for class `Class_TypeVarTuple`, got 1.', 'TODO: Starred(ExprStarred - Answers::expr_infer']", "Line 95: Unexpected errors ['Expected 0 type arguments for class `Class_TypeVarTuple`, got 2.']", "Line 96: Unexpected errors ['Expected 0 type arguments for class `Class_TypeVarTuple`, got 2.', 'Expected 0 type arguments for class `Class_TypeVarTuple`, got 2.']", "Line 127: Unexpected errors ['assert_type(Any, int) failed']", @@ -699,10 +699,10 @@ "Line 68: Expected 1 errors", "Line 74: Expected 1 errors", "Line 78: Expected 1 errors", - "Line 23: Unexpected errors ['assert_type(type[slice], type[slice[int, int, int | None]]) failed']", + "Line 23: Unexpected errors ['assert_type(type[slice[Error, Error, Error]], type[slice[int, int, int | None]]) failed']", "Line 24: Unexpected errors ['assert_type(slice[Any, Any, Any], slice[int, int, int | None]) failed']", "Line 25: Unexpected errors ['assert_type(slice[str, TypeVar[StartT], int | None], slice[str, str, int | None]) failed']", - "Line 94: Unexpected errors ['assert_type(type[Bar], type[Bar[Any, list[Any]]]) failed']", + "Line 94: Unexpected errors ['assert_type(type[Bar[Error, Error]], type[Bar[Any, list[Any]]]) failed']", "Line 95: Unexpected errors ['assert_type(type[Bar[int, list[TypeVar[Z1]]]], type[Bar[int, list[int]]]) failed']", "Line 96: Unexpected errors ['assert_type(Bar[int, list[TypeVar[Z1]]], Bar[int, list[int]]) failed']" ], @@ -710,7 +710,7 @@ "Line 30: Expected 1 errors", "Line 25: Unexpected errors ['untype, got Forall[DefaultStrT, LegacyExplicitTypeAlias[MyAlias, type[SomethingWithNoDefaults[int, DefaultStrT]]]]']", "Line 26: Unexpected errors ['assert_type(Any, SomethingWithNoDefaults[int, str]) failed']", - "Line 45: Unexpected errors ['assert_type(type[Bar], type[Bar[str]]) failed']", + "Line 45: Unexpected errors ['assert_type(type[Bar[Error]], type[Bar[str]]) failed']", "Line 46: Unexpected errors ['assert_type(Bar[Any], Bar[str]) failed']" ], "generics_paramspec_basic.py": [ diff --git a/pyre2/pyre2/bin/alt/expr.rs b/pyre2/pyre2/bin/alt/expr.rs index b8fa51121c..9850129cd0 100644 --- a/pyre2/pyre2/bin/alt/expr.rs +++ b/pyre2/pyre2/bin/alt/expr.rs @@ -955,7 +955,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { let expr_b = &x.arguments.args[1]; let a = self.expr_infer(expr_a); let b = self.expr_untype(expr_b); - let a = self.solver().deep_force(a).explicit_any(); + let a = self.canonicalize_all_class_types( + self.solver().deep_force(a).explicit_any(), + expr_a.range(), + ); let b = self.canonicalize_all_class_types( self.solver().deep_force(b).explicit_any(), expr_b.range(), diff --git a/pyre2/pyre2/bin/test/narrow.rs b/pyre2/pyre2/bin/test/narrow.rs index 4e5e7ed852..c18476906d 100644 --- a/pyre2/pyre2/bin/test/narrow.rs +++ b/pyre2/pyre2/bin/test/narrow.rs @@ -6,7 +6,6 @@ */ use crate::testcase; -use crate::testcase_with_bug; testcase!( test_is, @@ -31,8 +30,7 @@ def f(x: str | None): "#, ); -// TODO: fix the assert_type bug that is causing this assertion to fail. -testcase_with_bug!( +testcase!( test_is_subtype, r#" from typing import assert_type @@ -40,7 +38,7 @@ class A: pass class B(A): pass def f(x: type[A]): if x is B: - assert_type(x, type[B]) # E: assert_type(type[B], type[B]) failed + assert_type(x, type[B]) "#, ); diff --git a/pyre2/pyre2/bin/test/simple.rs b/pyre2/pyre2/bin/test/simple.rs index 6ea9be88a5..782b477aef 100644 --- a/pyre2/pyre2/bin/test/simple.rs +++ b/pyre2/pyre2/bin/test/simple.rs @@ -1031,3 +1031,12 @@ def f(x: int): assert_type(y, int) "#, ); + +testcase!( + test_type_of_class, + r#" +from typing import assert_type +class A: pass +assert_type(A, type[A]) + "#, +);