From 8941484275a22fe482a5251171ed27add03f8ffb Mon Sep 17 00:00:00 2001 From: Shantanu Jain Date: Sat, 28 Dec 2024 03:23:47 -0800 Subject: [PATCH 1/2] Don't erase type object args in diagnostics Fixes https://github.com/python/mypy/issues/16875 --- mypy/messages.py | 2 +- test-data/unit/check-generics.test | 3 +-- test-data/unit/check-inference.test | 2 +- test-data/unit/check-newsemanal.test | 6 ++---- test-data/unit/fine-grained-inspect.test | 2 +- 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 6b0760cd79c6..40b0e7ee695a 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -2700,7 +2700,7 @@ def format_literal_value(typ: LiteralType) -> str: if func.is_type_obj(): # The type of a type object type can be derived from the # return type (this always works). - return format(TypeType.make_normalized(erase_type(func.items[0].ret_type))) + return format(TypeType.make_normalized(func.items[0].ret_type)) elif isinstance(func, CallableType): if func.type_guard is not None: return_type = f"TypeGuard[{format(func.type_guard)}]" diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 08dfb3b54b3a..5d6ad8e19631 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1773,8 +1773,7 @@ T = TypeVar('T') class C(Generic[T]): def __init__(self) -> None: pass x = C # type: Callable[[], C[int]] -y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type "Type[C[Any]]", variable has type "Callable[[], int]") - +y = C # type: Callable[[], int] # E: Incompatible types in assignment (expression has type "Type[C[T]]", variable has type "Callable[[], int]") -- Special cases -- ------------- diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index bec3a9a07593..560092ed1a43 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -2488,7 +2488,7 @@ T = TypeVar('T') class C(Sequence[T], Generic[T]): pass C[0] = 0 [out] -main:4: error: Unsupported target for indexed assignment ("Type[C[Any]]") +main:4: error: Unsupported target for indexed assignment ("Type[C[T]]") main:4: error: Invalid type: try using Literal[0] instead? [case testNoCrashOnPartialMember] diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index 81b0066dbf81..7ac90d07e504 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -2743,13 +2743,11 @@ T = TypeVar('T') class C(Generic[T]): pass -# TODO: Error message is confusing + C = C[int] # E: Cannot assign to a type \ - # E: Incompatible types in assignment (expression has type "Type[C[Any]]", variable has type "Type[C[Any]]") + # E: Incompatible types in assignment (expression has type "Type[C[int]]", variable has type "Type[C[T]]") x: C reveal_type(x) # N: Revealed type is "__main__.C[Any]" -[out] -[out2] [case testNewAnalyzerClassVariableOrdering] def foo(x: str) -> None: pass diff --git a/test-data/unit/fine-grained-inspect.test b/test-data/unit/fine-grained-inspect.test index ed89f2f099f9..0e05769370a2 100644 --- a/test-data/unit/fine-grained-inspect.test +++ b/test-data/unit/fine-grained-inspect.test @@ -23,7 +23,7 @@ NameExpr -> "C[T]" MemberExpr -> "T" NameExpr -> "C[T]" MemberExpr -> "T" -12:5:12:5 -> "Type[foo.C[Any]]" +12:5:12:5 -> "Type[foo.C[builtins.int]]" 12:5:12:9 -> "foo.C[builtins.int]" 12:1:12:10 -> "builtins.int" CallExpr:12:5:12:9 -> "C[int]" From 6b072b4ce25d90ce111004fcee8cd35ab84934dd Mon Sep 17 00:00:00 2001 From: Shantanu Jain Date: Sat, 28 Dec 2024 03:47:59 -0800 Subject: [PATCH 2/2] pyeval --- test-data/unit/pythoneval.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 70003545754c..66ceafb91370 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -823,7 +823,7 @@ class MyDDict(t.DefaultDict[int,T], t.Generic[T]): MyDDict(dict)['0'] MyDDict(dict)[0] [out] -_program.py:7: error: Argument 1 to "defaultdict" has incompatible type "Type[List[Any]]"; expected "Optional[Callable[[], str]]" +_program.py:7: error: Argument 1 to "defaultdict" has incompatible type "Type[List[_T]]"; expected "Optional[Callable[[], str]]" _program.py:10: error: Invalid index type "str" for "defaultdict[int, str]"; expected type "int" _program.py:10: error: Incompatible types in assignment (expression has type "int", target has type "str") _program.py:20: error: Argument 1 to "tst" has incompatible type "defaultdict[str, List[Never]]"; expected "defaultdict[int, List[Never]]"