Skip to content

Use format_type for msg.type_not_iterable #11490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def unpacking_strings_disallowed(self, context: Context) -> None:
self.fail("Unpacking a string is disallowed", context)

def type_not_iterable(self, type: Type, context: Context) -> None:
self.fail('"{}" object is not iterable'.format(type), context)
self.fail('{} object is not iterable'.format(format_type(type)), context)

def incompatible_operator_assignment(self, op: str,
context: Context) -> None:
Expand Down Expand Up @@ -1613,8 +1613,8 @@ def generate_incompatible_tuple_error(self,
for i, (lhs_t, rhs_t) in enumerate(zip(lhs_types, rhs_types)):
if not is_subtype(lhs_t, rhs_t):
if error_cnt < 3:
notes.append('Expression tuple item {} has type "{}"; "{}" expected; '
.format(str(i), format_type_bare(rhs_t), format_type_bare(lhs_t)))
notes.append('Expression tuple item {} has type {}; {} expected; '
.format(str(i), format_type(rhs_t), format_type(lhs_t)))
error_cnt += 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing from format_type_bare to format_type to keep consistency with other usage of format_type.


error_msg = msg + ' ({} tuple items are incompatible'.format(str(error_cnt))
Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ main:6: error: Need more than 3 values to unpack (4 expected)
[case testInvalidRvalueTypeInInferredMultipleLvarDefinition]
import typing
def f() -> None:
a, b = f # E: "def ()" object is not iterable
c, d = A() # E: "__main__.A" object is not iterable
a, b = f # E: "Callable[[], None]" object is not iterable
c, d = A() # E: "A" object is not iterable
class A: pass
[builtins fixtures/for.pyi]
[out]

[case testInvalidRvalueTypeInInferredNestedTupleAssignment]
import typing
def f() -> None:
a1, (a2, b) = A(), f # E: "def ()" object is not iterable
a3, (c, d) = A(), A() # E: "__main__.A" object is not iterable
a1, (a2, b) = A(), f # E: "Callable[[], None]" object is not iterable
a3, (c, d) = A(), A() # E: "A" object is not iterable
class A: pass
[builtins fixtures/for.pyi]
[out]
Expand Down Expand Up @@ -1004,7 +1004,7 @@ main:4: error: Incompatible types in assignment (expression has type "A", variab
main:5: error: Incompatible types in assignment (expression has type "B", variable has type "C")
main:6: error: Incompatible types in assignment (expression has type "C", variable has type "A")
main:10: error: Need more than 2 values to unpack (3 expected)
main:12: error: "__main__.B" object is not iterable
main:12: error: "B" object is not iterable

[case testInferenceOfFor3]

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -1676,10 +1676,10 @@ reveal_type(n2.b) # N: Revealed type is "builtins.str"
reveal_type(m3.a) # N: Revealed type is "builtins.str"
reveal_type(n3.b) # N: Revealed type is "builtins.str"

x, y = m # E: "types.ModuleType" object is not iterable
x, y = m # E: Module object is not iterable
x, y, z = m, n # E: Need more than 2 values to unpack (3 expected)
x, y = m, m, m # E: Too many values to unpack (2 expected, 3 provided)
x, (y, z) = m, n # E: "types.ModuleType" object is not iterable
x, (y, z) = m, n # E: Module object is not iterable
x, (y, z) = m, (n, n, n) # E: Too many values to unpack (2 expected, 3 provided)

[file m.py]
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ a, b = None, None # type: (A, B)

a1, b1 = a, a # type: (A, B) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a2, b2 = b, b # type: (A, B) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a3, b3 = a # type: (A, B) # E: "__main__.A" object is not iterable
a3, b3 = a # type: (A, B) # E: "A" object is not iterable
a4, b4 = None # type: (A, B) # E: "None" object is not iterable
a5, b5 = a, b, a # type: (A, B) # E: Too many values to unpack (2 expected, 3 provided)

Expand All @@ -421,8 +421,8 @@ a, b = None, None # type: (A, B)
def f(): pass

a, b = None # E: "None" object is not iterable
a, b = a # E: "__main__.A" object is not iterable
a, b = f # E: "def () -> Any" object is not iterable
a, b = a # E: "A" object is not iterable
a, b = f # E: "Callable[[], Any]" object is not iterable

class A: pass
class B: pass
Expand Down Expand Up @@ -1468,7 +1468,7 @@ x9, y9, x10, y10, z5 = *points2, 1, *points2 # E: Contiguous iterable with same
() = [] # E: can't assign to ()

[case testAssignEmptyBogus]
() = 1 # E: "Literal[1]?" object is not iterable
Copy link
Member

@sobolevn sobolevn Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a significant change and I don't think that is an improvement. Literal[1] is more specific, I personally like it more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this is an interesting case. I think it's a different issue that we might need to modify the format_type.

() = 1 # E: "int" object is not iterable
[builtins fixtures/tuple.pyi]

[case testMultiplyTupleByIntegerLiteral]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ reveal_type(d1) # N: Revealed type is "Union[Any, builtins.float]"
reveal_type(d2) # N: Revealed type is "Union[Any, builtins.float]"

e: Union[Any, Tuple[float, float], int]
(e1, e2) = e # E: "builtins.int" object is not iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of places where builtins. is used, and some places where it is not used. I cannot tell what kind of principle is used here 😞

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy keeps the prefix in reveal_type for more verbose information. And according to the original discussion, it's better to discard builtins. to get concise error messages. Also, cpython dosen't output builtins. when error occurs.

(e1, e2) = e # E: "int" object is not iterable
[builtins fixtures/tuple.pyi]

[case testUnionMultiassignNotJoin]
Expand Down Expand Up @@ -694,7 +694,7 @@ reveal_type(d) # N: Revealed type is "builtins.list[builtins.int*]"
from typing import Union
bad: Union[int, str]

x, y = bad # E: "builtins.int" object is not iterable \
x, y = bad # E: "int" object is not iterable \
# E: Unpacking a string is disallowed
reveal_type(x) # N: Revealed type is "Any"
reveal_type(y) # N: Revealed type is "Any"
Expand Down