Skip to content

Commit ba55b1a

Browse files
author
Brian Weber
committed
Consistent quote wrapping around MessageBuilder.format as per issue python#3409
1 parent d238ab5 commit ba55b1a

30 files changed

+347
-344
lines changed

mypy/messages.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def format(self, typ: Type, verbosity: int = 0) -> str:
184184
Mostly behave like format_simple below, but never return an empty string.
185185
"""
186186
s = self.format_simple(typ, verbosity)
187-
if s != '':
187+
if s != '' and s != '""':
188188
# If format_simple returns a non-trivial result, use that.
189189
return s
190190
elif isinstance(typ, FunctionLike):
@@ -242,6 +242,9 @@ def format_simple(self, typ: Type, verbosity: int = 0) -> str:
242242
None -> None
243243
callable type -> "" (empty string)
244244
"""
245+
return '"{}"'.format(self._format_simple(typ, verbosity))
246+
247+
def _format_simple(self, typ: Type, verbosity: int = 0) -> str:
245248
if isinstance(typ, Instance):
246249
itype = typ
247250
# Get the short name of the type.
@@ -257,7 +260,7 @@ def format_simple(self, typ: Type, verbosity: int = 0) -> str:
257260
# No type arguments. Place the type name in quotes to avoid
258261
# potential for confusion: otherwise, the type name could be
259262
# interpreted as a normal word.
260-
return '"{}"'.format(base_str)
263+
return base_str
261264
elif itype.type.fullname() == 'builtins.tuple':
262265
item_type_str = strip_quotes(self.format(itype.args[0]))
263266
return 'Tuple[{}, ...]'.format(item_type_str)
@@ -281,48 +284,48 @@ def format_simple(self, typ: Type, verbosity: int = 0) -> str:
281284
return '{}[...]'.format(base_str)
282285
elif isinstance(typ, TypeVarType):
283286
# This is similar to non-generic instance types.
284-
return '"{}"'.format(typ.name)
287+
return typ.name
285288
elif isinstance(typ, TupleType):
286289
# Prefer the name of the fallback class (if not tuple), as it's more informative.
287290
if typ.fallback.type.fullname() != 'builtins.tuple':
288-
return self.format_simple(typ.fallback)
291+
return self._format_simple(typ.fallback)
289292
items = []
290293
for t in typ.items:
291294
items.append(strip_quotes(self.format(t)))
292-
s = '"Tuple[{}]"'.format(', '.join(items))
295+
s = 'Tuple[{}]'.format(', '.join(items))
293296
if len(s) < 400:
294297
return s
295298
else:
296299
return 'tuple(length {})'.format(len(items))
297300
elif isinstance(typ, TypedDictType):
298301
# If the TypedDictType is named, return the name
299302
if typ.fallback.type.fullname() != 'typing.Mapping':
300-
return self.format_simple(typ.fallback)
303+
return self._format_simple(typ.fallback)
301304
items = []
302305
for (item_name, item_type) in typ.items.items():
303306
items.append('{}={}'.format(item_name, strip_quotes(self.format(item_type))))
304-
s = '"TypedDict({})"'.format(', '.join(items))
307+
s = 'TypedDict({})'.format(', '.join(items))
305308
return s
306309
elif isinstance(typ, UnionType):
307310
# Only print Unions as Optionals if the Optional wouldn't have to contain another Union
308311
print_as_optional = (len(typ.items) -
309312
sum(isinstance(t, NoneTyp) for t in typ.items) == 1)
310313
if print_as_optional:
311314
rest = [t for t in typ.items if not isinstance(t, NoneTyp)]
312-
return '"Optional[{}]"'.format(strip_quotes(self.format(rest[0])))
315+
return 'Optional[{}]'.format(strip_quotes(self.format(rest[0])))
313316
else:
314317
items = []
315318
for t in typ.items:
316319
items.append(strip_quotes(self.format(t)))
317-
s = '"Union[{}]"'.format(', '.join(items))
320+
s = 'Union[{}]'.format(', '.join(items))
318321
if len(s) < 400:
319322
return s
320323
else:
321324
return 'union type ({} items)'.format(len(items))
322325
elif isinstance(typ, NoneTyp):
323326
return 'None'
324327
elif isinstance(typ, AnyType):
325-
return '"Any"'
328+
return 'Any'
326329
elif isinstance(typ, DeletedType):
327330
return '<deleted>'
328331
elif isinstance(typ, UninhabitedType):
@@ -332,7 +335,7 @@ def format_simple(self, typ: Type, verbosity: int = 0) -> str:
332335
return '<nothing>'
333336
elif isinstance(typ, TypeType):
334337
return 'Type[{}]'.format(
335-
strip_quotes(self.format_simple(typ.item, verbosity)))
338+
strip_quotes(self._format_simple(typ.item, verbosity)))
336339
elif typ is None:
337340
raise RuntimeError('Type is None')
338341
else:

test-data/unit/check-async-await.test

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def f() -> int:
8484
x = await g()
8585
return x
8686
[out]
87-
main:7: error: Incompatible types in await (actual type Generator[int, None, str], expected type Awaitable[Any])
87+
main:7: error: Incompatible types in await (actual type "Generator[int, None, str]", expected type "Awaitable[Any]")
8888

8989
[case testAwaitIteratorError]
9090

@@ -95,7 +95,7 @@ async def f() -> int:
9595
x = await g()
9696
return x
9797
[out]
98-
main:6: error: Incompatible types in await (actual type Iterator[Any], expected type Awaitable[Any])
98+
main:6: error: Incompatible types in await (actual type "Iterator[Any]", expected type "Awaitable[Any]")
9999

100100
[case testAwaitArgumentError]
101101

@@ -106,7 +106,7 @@ async def f() -> int:
106106
return x
107107
[builtins fixtures/async_await.pyi]
108108
[out]
109-
main:5: error: Incompatible types in await (actual type "int", expected type Awaitable[Any])
109+
main:5: error: Incompatible types in await (actual type "int", expected type "Awaitable[Any]")
110110

111111
[case testAwaitResultError]
112112

@@ -150,7 +150,7 @@ async def f() -> None:
150150
[builtins fixtures/async_await.pyi]
151151
[out]
152152
main:4: error: AsyncIterable expected
153-
main:4: error: List[int] has no attribute "__aiter__"
153+
main:4: error: "List[int]" has no attribute "__aiter__"
154154

155155
[case testAsyncForTypeComments]
156156

@@ -232,13 +232,13 @@ async def wrong_iterable(obj: Iterable[int]):
232232

233233
[out]
234234
main:18: error: AsyncIterable expected
235-
main:18: error: Iterable[int] has no attribute "__aiter__"; maybe "__iter__"?
235+
main:18: error: "Iterable[int]" has no attribute "__aiter__"; maybe "__iter__"?
236236
main:19: error: Iterable expected
237-
main:19: error: asyncify[int] has no attribute "__iter__"; maybe "__aiter__"?
237+
main:19: error: "asyncify[int]" has no attribute "__iter__"; maybe "__aiter__"?
238238
main:20: error: AsyncIterable expected
239-
main:20: error: Iterable[int] has no attribute "__aiter__"; maybe "__iter__"?
239+
main:20: error: "Iterable[int]" has no attribute "__aiter__"; maybe "__iter__"?
240240
main:21: error: Iterable expected
241-
main:21: error: asyncify[int] has no attribute "__iter__"; maybe "__aiter__"?
241+
main:21: error: "asyncify[int]" has no attribute "__iter__"; maybe "__aiter__"?
242242
[builtins fixtures/async_await.pyi]
243243

244244
[case testAsyncWith]
@@ -271,7 +271,7 @@ class C:
271271
def __aenter__(self) -> int: pass
272272
async def __aexit__(self, x, y, z) -> None: pass
273273
async def f() -> None:
274-
async with C() as x: # E: Incompatible types in "async with" for __aenter__ (actual type "int", expected type Awaitable[Any])
274+
async with C() as x: # E: Incompatible types in "async with" for __aenter__ (actual type "int", expected type "Awaitable[Any]")
275275
pass
276276
[builtins fixtures/async_await.pyi]
277277
[out]
@@ -282,7 +282,7 @@ class C:
282282
def __aenter__(self) -> None: pass
283283
async def __aexit__(self, x, y, z) -> None: pass
284284
async def f() -> None:
285-
async with C() as x: # E: None has no attribute "__await__"
285+
async with C() as x: # E: "None" has no attribute "__await__"
286286
pass
287287
[builtins fixtures/async_await.pyi]
288288
[out]
@@ -293,7 +293,7 @@ class C:
293293
async def __aenter__(self) -> int: pass
294294
def __aexit__(self, x, y, z) -> int: pass
295295
async def f() -> None:
296-
async with C() as x: # E: Incompatible types in "async with" for __aexit__ (actual type "int", expected type Awaitable[Any])
296+
async with C() as x: # E: Incompatible types in "async with" for __aexit__ (actual type "int", expected type "Awaitable[Any]")
297297
pass
298298
[builtins fixtures/async_await.pyi]
299299
[out]
@@ -304,7 +304,7 @@ class C:
304304
async def __aenter__(self) -> int: pass
305305
def __aexit__(self, x, y, z) -> None: pass
306306
async def f() -> None:
307-
async with C() as x: # E: None has no attribute "__await__"
307+
async with C() as x: # E: "None" has no attribute "__await__"
308308
pass
309309
[builtins fixtures/async_await.pyi]
310310
[out]
@@ -362,7 +362,7 @@ def g() -> Generator[Any, None, str]:
362362
return x
363363
[builtins fixtures/async_await.pyi]
364364
[out]
365-
main:6: error: "yield from" can't be applied to Awaitable[str]
365+
main:6: error: "yield from" can't be applied to "Awaitable[str]"
366366

367367
[case testAwaitableSubclass]
368368

@@ -523,7 +523,7 @@ def h() -> None:
523523

524524
[out]
525525
main:9: error: Iterable expected
526-
main:9: error: AsyncGenerator[int, None] has no attribute "__iter__"; maybe "__aiter__"?
526+
main:9: error: "AsyncGenerator[int, None]" has no attribute "__iter__"; maybe "__aiter__"?
527527

528528
[case testAsyncGeneratorNoYieldFrom]
529529
# flags: --fast-parser --python-version 3.6
@@ -607,19 +607,19 @@ def plain_host_generator() -> Generator[str, None, None]:
607607
yield 'a'
608608
x = 0
609609
x = yield from plain_generator()
610-
x = yield from plain_coroutine() # E: "yield from" can't be applied to Awaitable[int]
610+
x = yield from plain_coroutine() # E: "yield from" can't be applied to "Awaitable[int]"
611611
x = yield from decorated_generator()
612-
x = yield from decorated_coroutine() # E: "yield from" can't be applied to AwaitableGenerator[Any, Any, int, Awaitable[int]]
612+
x = yield from decorated_coroutine() # E: "yield from" can't be applied to "AwaitableGenerator[Any, Any, int, Awaitable[int]]"
613613
x = yield from other_iterator()
614614
x = yield from other_coroutine() # E: "yield from" can't be applied to "Aw"
615615

616616
async def plain_host_coroutine() -> None:
617617
x = 0
618-
x = await plain_generator() # E: Incompatible types in await (actual type Generator[str, None, int], expected type Awaitable[Any])
618+
x = await plain_generator() # E: Incompatible types in await (actual type "Generator[str, None, int]", expected type "Awaitable[Any]")
619619
x = await plain_coroutine()
620620
x = await decorated_generator()
621621
x = await decorated_coroutine()
622-
x = await other_iterator() # E: Incompatible types in await (actual type "It", expected type Awaitable[Any])
622+
x = await other_iterator() # E: Incompatible types in await (actual type "It", expected type "Awaitable[Any]")
623623
x = await other_coroutine()
624624

625625
@coroutine
@@ -636,11 +636,11 @@ def decorated_host_generator() -> Generator[str, None, None]:
636636
@coroutine
637637
async def decorated_host_coroutine() -> None:
638638
x = 0
639-
x = await plain_generator() # E: Incompatible types in await (actual type Generator[str, None, int], expected type Awaitable[Any])
639+
x = await plain_generator() # E: Incompatible types in await (actual type "Generator[str, None, int]", expected type "Awaitable[Any]")
640640
x = await plain_coroutine()
641641
x = await decorated_generator()
642642
x = await decorated_coroutine()
643-
x = await other_iterator() # E: Incompatible types in await (actual type "It", expected type Awaitable[Any])
643+
x = await other_iterator() # E: Incompatible types in await (actual type "It", expected type "Awaitable[Any]")
644644
x = await other_coroutine()
645645

646646
[builtins fixtures/async_await.pyi]

test-data/unit/check-class-namedtuple.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class MyNamedTuple(NamedTuple):
221221
a: int
222222
b: str
223223

224-
MyNamedTuple.x # E: Type[MyNamedTuple] has no attribute "x"
224+
MyNamedTuple.x # E: "Type[MyNamedTuple]" has no attribute "x"
225225

226226
[case testNewNamedTupleEmptyItems]
227227
# flags: --python-version 3.6
@@ -439,13 +439,13 @@ class HasNone(NamedTuple):
439439
y: Optional[int] = None
440440

441441
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, builtins.None], fallback=__main__.HasNone]'
442-
HasNone(None) # E: Argument 1 to "HasNone" has incompatible type None; expected "int"
442+
HasNone(None) # E: Argument 1 to "HasNone" has incompatible type "None"; expected "int"
443443
HasNone(1, y=None)
444444
HasNone(1, y=2)
445445

446446
class CannotBeNone(NamedTuple):
447447
x: int
448-
y: int = None # E: Incompatible types in assignment (expression has type None, variable has type "int")
448+
y: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
449449

450450
[builtins fixtures/list.pyi]
451451

0 commit comments

Comments
 (0)