Skip to content

Commit

Permalink
Don't erase type object args in diagnostics (#18352)
Browse files Browse the repository at this point in the history
Fixes #16875
  • Loading branch information
hauntsaninja authored Dec 28, 2024
1 parent 670f486 commit 44bf7e5
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}]"
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- -------------
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 2 additions & 4 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained-inspect.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]]"
Expand Down

0 comments on commit 44bf7e5

Please sign in to comment.