Skip to content

Commit 11c63aa

Browse files
authored
Consistently use type-abstract error code (#14619)
Ref #4717 Although function use case is much more important, the variable assignment should use the same error code, otherwise this may cause confusion.
1 parent 8cc024e commit 11c63aa

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

mypy/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# Keep track of the original error code when the error code of a message is changed.
2626
# This is used to give notes about out-of-date "type: ignore" comments.
27-
original_error_codes: Final = {codes.LITERAL_REQ: codes.MISC}
27+
original_error_codes: Final = {codes.LITERAL_REQ: codes.MISC, codes.TYPE_ABSTRACT: codes.MISC}
2828

2929

3030
class ErrorInfo:

mypy/messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,9 @@ def bad_proto_variance(
17871787

17881788
def concrete_only_assign(self, typ: Type, context: Context) -> None:
17891789
self.fail(
1790-
f"Can only assign concrete classes to a variable of type {format_type(typ)}", context
1790+
f"Can only assign concrete classes to a variable of type {format_type(typ)}",
1791+
context,
1792+
code=codes.TYPE_ABSTRACT,
17911793
)
17921794

17931795
def concrete_only_call(self, typ: Type, context: Context) -> None:

test-data/unit/check-errorcodes.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,11 @@ T = TypeVar("T")
998998
def test(tp: Type[T]) -> T: ...
999999
test(C) # E: Only concrete class can be given where "Type[C]" is expected [type-abstract]
10001000

1001+
class D(C):
1002+
@abc.abstractmethod
1003+
def bar(self) -> None: ...
1004+
cls: Type[C] = D # E: Can only assign concrete classes to a variable of type "Type[C]" [type-abstract]
1005+
10011006
[case testUncheckedAnnotationCodeShown]
10021007
def f():
10031008
x: int = "no" # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]

0 commit comments

Comments
 (0)