Skip to content

Commit d326952

Browse files
authored
Use double quotes in errors related to NewType, TypeVar, namedTuple and TypedDict (#10359)
* NewType related errors and Cannot redefine * TypeVar related * namedTuple - First argument * TypedDict - First argument
1 parent bdcc562 commit d326952

13 files changed

+37
-37
lines changed

docs/source/error_code_list.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ consistently when using the call-based syntax. Example:
658658
659659
from typing import NamedTuple
660660
661-
# Error: First argument to namedtuple() should be 'Point2D', not 'Point'
661+
# Error: First argument to namedtuple() should be "Point2D", not "Point"
662662
Point2D = NamedTuple("Point", [("x", int), ("y", int)])
663663
664664
Report syntax errors [syntax]

mypy/semanal.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
22042204
self.fail("NamedTuple type as an attribute is not supported", lvalue)
22052205
return False
22062206
if internal_name != name:
2207-
self.fail("First argument to namedtuple() should be '{}', not '{}'".format(
2207+
self.fail('First argument to namedtuple() should be "{}", not "{}"'.format(
22082208
name, internal_name), s.rvalue, code=codes.NAME_MATCH)
22092209
return True
22102210
# Yes, it's a valid namedtuple, but defer if it is not ready.
@@ -2938,7 +2938,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
29382938
# Also give error for another type variable with the same name.
29392939
(isinstance(existing.node, TypeVarExpr) and
29402940
existing.node is call.analyzed)):
2941-
self.fail("Cannot redefine '%s' as a type variable" % name, s)
2941+
self.fail('Cannot redefine "%s" as a type variable' % name, s)
29422942
return False
29432943

29442944
if self.options.disallow_any_unimported:
@@ -2993,7 +2993,7 @@ def check_typevarlike_name(self, call: CallExpr, name: str, context: Context) ->
29932993
context)
29942994
return False
29952995
elif call.args[0].value != name:
2996-
msg = "String argument 1 '{}' to {}(...) does not match variable name '{}'"
2996+
msg = 'String argument 1 "{}" to {}(...) does not match variable name "{}"'
29972997
self.fail(msg.format(call.args[0].value, typevarlike_type, name), context)
29982998
return False
29992999
return True
@@ -3068,20 +3068,20 @@ def process_typevar_parameters(self, args: List[Expression],
30683068
analyzed = PlaceholderType(None, [], context.line)
30693069
upper_bound = get_proper_type(analyzed)
30703070
if isinstance(upper_bound, AnyType) and upper_bound.is_from_error:
3071-
self.fail("TypeVar 'bound' must be a type", param_value)
3071+
self.fail('TypeVar "bound" must be a type', param_value)
30723072
# Note: we do not return 'None' here -- we want to continue
30733073
# using the AnyType as the upper bound.
30743074
except TypeTranslationError:
3075-
self.fail("TypeVar 'bound' must be a type", param_value)
3075+
self.fail('TypeVar "bound" must be a type', param_value)
30763076
return None
30773077
elif param_name == 'values':
30783078
# Probably using obsolete syntax with values=(...). Explain the current syntax.
3079-
self.fail("TypeVar 'values' argument not supported", context)
3079+
self.fail('TypeVar "values" argument not supported', context)
30803080
self.fail("Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))",
30813081
context)
30823082
return None
30833083
else:
3084-
self.fail("Unexpected argument to TypeVar(): {}".format(param_name), context)
3084+
self.fail('Unexpected argument to TypeVar(): "{}"'.format(param_name), context)
30853085
return None
30863086

30873087
if covariant and contravariant:

mypy/semanal_newtype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def analyze_newtype_declaration(self,
127127
# Give a better error message than generic "Name already defined".
128128
if (existing and
129129
not isinstance(existing.node, PlaceholderNode) and not s.rvalue.analyzed):
130-
self.fail("Cannot redefine '%s' as a NewType" % name, s)
130+
self.fail('Cannot redefine "%s" as a NewType' % name, s)
131131

132132
# This dummy NewTypeExpr marks the call as sufficiently analyzed; it will be
133133
# overwritten later with a fully complete NewTypeExpr if there are no other
@@ -153,7 +153,7 @@ def check_newtype_args(self, name: str, call: CallExpr,
153153
self.fail("Argument 1 to NewType(...) must be a string literal", context)
154154
has_failed = True
155155
elif args[0].value != name:
156-
msg = "String argument 1 '{}' to NewType(...) does not match variable name '{}'"
156+
msg = 'String argument 1 "{}" to NewType(...) does not match variable name "{}"'
157157
self.fail(msg.format(args[0].value, name), context)
158158
has_failed = True
159159

mypy/semanal_typeddict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def check_typeddict(self,
200200
else:
201201
if var_name is not None and name != var_name:
202202
self.fail(
203-
"First argument '{}' to TypedDict() does not match variable name '{}'".format(
203+
'First argument "{}" to TypedDict() does not match variable name "{}"'.format(
204204
name, var_name), node, code=codes.NAME_MATCH)
205205
if name != var_name or is_func_scope:
206206
# Give it a unique name derived from the line number.

test-data/unit/check-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4573,7 +4573,7 @@ def parse_ast(name_dict: NameDict) -> None:
45734573
[case testNoCrashForwardRefToBrokenDoubleNewType]
45744574
from typing import Any, Dict, List, NewType
45754575

4576-
Foo = NewType('NotFoo', int) # E: String argument 1 'NotFoo' to NewType(...) does not match variable name 'Foo'
4576+
Foo = NewType('NotFoo', int) # E: String argument 1 "NotFoo" to NewType(...) does not match variable name "Foo"
45774577
Foos = NewType('Foos', List[Foo]) # type: ignore
45784578

45794579
def frob(foos: Dict[Any, Foos]) -> None:

test-data/unit/check-errorcodes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,11 @@ k = [x for x in lst if isinstance(x, int) or foo()] # E: If condition in compre
790790
[case testNamedTupleNameMismatch]
791791
from typing import NamedTuple
792792

793-
Foo = NamedTuple("Bar", []) # E: First argument to namedtuple() should be 'Foo', not 'Bar' [name-match]
793+
Foo = NamedTuple("Bar", []) # E: First argument to namedtuple() should be "Foo", not "Bar" [name-match]
794794
[builtins fixtures/tuple.pyi]
795795

796796
[case testTypedDictNameMismatch]
797797
from typing_extensions import TypedDict
798798

799-
Foo = TypedDict("Bar", {}) # E: First argument 'Bar' to TypedDict() does not match variable name 'Foo' [name-match]
799+
Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not match variable name "Foo" [name-match]
800800
[builtins fixtures/dict.pyi]

test-data/unit/check-incremental.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,9 +5067,9 @@ from typing import NamedTuple
50675067
NT = NamedTuple('BadName', [('x', int)])
50685068
[builtins fixtures/tuple.pyi]
50695069
[out]
5070-
tmp/b.py:2: error: First argument to namedtuple() should be 'NT', not 'BadName'
5070+
tmp/b.py:2: error: First argument to namedtuple() should be "NT", not "BadName"
50715071
[out2]
5072-
tmp/b.py:2: error: First argument to namedtuple() should be 'NT', not 'BadName'
5072+
tmp/b.py:2: error: First argument to namedtuple() should be "NT", not "BadName"
50735073
tmp/a.py:3: note: Revealed type is "Tuple[builtins.int, fallback=b.NT]"
50745074

50755075
[case testNewAnalyzerIncrementalBrokenNamedTupleNested]
@@ -5089,9 +5089,9 @@ def test() -> None:
50895089
NT = namedtuple('BadName', ['x', 'y'])
50905090
[builtins fixtures/list.pyi]
50915091
[out]
5092-
tmp/b.py:4: error: First argument to namedtuple() should be 'NT', not 'BadName'
5092+
tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"
50935093
[out2]
5094-
tmp/b.py:4: error: First argument to namedtuple() should be 'NT', not 'BadName'
5094+
tmp/b.py:4: error: First argument to namedtuple() should be "NT", not "BadName"
50955095

50965096
[case testNewAnalyzerIncrementalMethodNamedTuple]
50975097

test-data/unit/check-namedtuple.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,9 @@ Type1 = NamedTuple('Type1', [('foo', foo)]) # E: Function "b.foo" is not valid
967967
from typing import NamedTuple
968968
from collections import namedtuple
969969

970-
A = NamedTuple('X', [('a', int)]) # E: First argument to namedtuple() should be 'A', not 'X'
971-
B = namedtuple('X', ['a']) # E: First argument to namedtuple() should be 'B', not 'X'
970+
A = NamedTuple('X', [('a', int)]) # E: First argument to namedtuple() should be "A", not "X"
971+
B = namedtuple('X', ['a']) # E: First argument to namedtuple() should be "B", not "X"
972972

973-
C = NamedTuple('X', [('a', 'Y')]) # E: First argument to namedtuple() should be 'C', not 'X'
973+
C = NamedTuple('X', [('a', 'Y')]) # E: First argument to namedtuple() should be "C", not "X"
974974
class Y: ...
975975
[builtins fixtures/tuple.pyi]

test-data/unit/check-newsemanal.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,7 @@ A = Enum('A', ['z', 't']) # E: Name "A" already defined on line 3
20622062
from typing import NewType
20632063

20642064
A = NewType('A', int)
2065-
A = NewType('A', str) # E: Cannot redefine 'A' as a NewType \
2065+
A = NewType('A', str) # E: Cannot redefine "A" as a NewType \
20662066
# E: Name "A" already defined on line 3
20672067

20682068
[case testNewAnalyzerNewTypeForwardClass]
@@ -2160,7 +2160,7 @@ from typing import TypeVar, Generic, Any
21602160

21612161
T = TypeVar('T', bound=B[Any])
21622162
# The "int" error is because of typing fixture.
2163-
T = TypeVar('T', bound=C) # E: Cannot redefine 'T' as a type variable \
2163+
T = TypeVar('T', bound=C) # E: Cannot redefine "T" as a type variable \
21642164
# E: Invalid assignment target \
21652165
# E: "int" not callable
21662166

@@ -2195,7 +2195,7 @@ reveal_type(y.x)
21952195
[out]
21962196
tmp/b.py:8: error: Type argument "builtins.int" of "B" must be a subtype of "b.B[Any]"
21972197
tmp/b.py:10: note: Revealed type is "b.B*[Any]"
2198-
tmp/a.py:5: error: Cannot redefine 'T' as a type variable
2198+
tmp/a.py:5: error: Cannot redefine "T" as a type variable
21992199
tmp/a.py:5: error: Invalid assignment target
22002200
tmp/a.py:5: error: "int" not callable
22012201

@@ -2224,7 +2224,7 @@ reveal_type(y.x)
22242224
[out]
22252225
tmp/b.py:9: error: Type argument "builtins.int" of "B" must be a subtype of "b.B[Any]"
22262226
tmp/b.py:11: note: Revealed type is "b.B*[Any]"
2227-
tmp/a.py:5: error: Cannot redefine 'T' as a type variable
2227+
tmp/a.py:5: error: Cannot redefine "T" as a type variable
22282228
tmp/a.py:5: error: Invalid assignment target
22292229

22302230
[case testNewAnalyzerTypeVarBoundInCycle]

test-data/unit/check-newtype.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ tmp/m.py:14: note: Revealed type is "builtins.int"
269269
[case testNewTypeBadInitializationFails]
270270
from typing import NewType
271271

272-
a = NewType('b', int) # E: String argument 1 'b' to NewType(...) does not match variable name 'a'
272+
a = NewType('b', int) # E: String argument 1 "b" to NewType(...) does not match variable name "a"
273273
b = NewType('b', 3) # E: Argument 2 to NewType(...) must be a valid type
274274
c = NewType(2, int) # E: Argument 1 to NewType(...) must be a string literal
275275
foo = "d"
@@ -317,12 +317,12 @@ from typing import NewType
317317

318318
a = 3
319319
def f(): a
320-
a = NewType('a', int) # E: Cannot redefine 'a' as a NewType \
320+
a = NewType('a', int) # E: Cannot redefine "a" as a NewType \
321321
# E: Name "a" already defined on line 4
322322

323323
b = NewType('b', int)
324324
def g(): b
325-
b = NewType('b', float) # E: Cannot redefine 'b' as a NewType \
325+
b = NewType('b', float) # E: Cannot redefine "b" as a NewType \
326326
# E: Name "b" already defined on line 8
327327

328328
c = NewType('c', str) # type: str # E: Cannot declare the type of a NewType declaration

test-data/unit/check-redefine.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def f() -> None:
274274
reveal_type(x) # N: Revealed type is "builtins.int"
275275
y = 1
276276
# NOTE: '"int" not callable' is due to test stubs
277-
y = TypeVar('y') # E: Cannot redefine 'y' as a type variable \
277+
y = TypeVar('y') # E: Cannot redefine "y" as a type variable \
278278
# E: "int" not callable
279279
def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \
280280
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

test-data/unit/check-typeddict.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ Point = TypedDict('Point', {'x': 1, 'y': 1}) # E: Invalid type: try using Liter
12301230

12311231
[case testCannotCreateTypedDictTypeWithInvalidName]
12321232
from mypy_extensions import TypedDict
1233-
X = TypedDict('Y', {'x': int}) # E: First argument 'Y' to TypedDict() does not match variable name 'X'
1233+
X = TypedDict('Y', {'x': int}) # E: First argument "Y" to TypedDict() does not match variable name "X"
12341234
[builtins fixtures/dict.pyi]
12351235

12361236

test-data/unit/semanal-errors.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,12 @@ from typing import TypeVar
10071007
a = TypeVar() # E: Too few arguments for TypeVar()
10081008
b = TypeVar(x='b') # E: TypeVar() expects a string literal as first argument
10091009
c = TypeVar(1) # E: TypeVar() expects a string literal as first argument
1010-
d = TypeVar('D') # E: String argument 1 'D' to TypeVar(...) does not match variable name 'd'
1011-
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): x
1010+
d = TypeVar('D') # E: String argument 1 "D" to TypeVar(...) does not match variable name "d"
1011+
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): "x"
10121012
f = TypeVar('f', (int, str), int) # E: Type expected
10131013
g = TypeVar('g', int) # E: TypeVar cannot have only a single constraint
1014-
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to TypeVar(): x
1015-
i = TypeVar('i', bound=1) # E: TypeVar 'bound' must be a type
1014+
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to TypeVar(): "x"
1015+
i = TypeVar('i', bound=1) # E: TypeVar "bound" must be a type
10161016
[out]
10171017

10181018
[case testMoreInvalidTypevarArguments]
@@ -1032,7 +1032,7 @@ c = TypeVar('c', int, 2) # E: Invalid type: try using Literal[2] instead?
10321032
from typing import TypeVar
10331033
a = TypeVar('a', values=(int, str))
10341034
[out]
1035-
main:2: error: TypeVar 'values' argument not supported
1035+
main:2: error: TypeVar "values" argument not supported
10361036
main:2: error: Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))
10371037

10381038
[case testLocalTypevarScope]
@@ -1052,7 +1052,7 @@ def g(x: T) -> None: pass # E: Name "T" is not defined
10521052
[case testRedefineVariableAsTypevar]
10531053
from typing import TypeVar
10541054
x = 0
1055-
x = TypeVar('x') # E: Cannot redefine 'x' as a type variable
1055+
x = TypeVar('x') # E: Cannot redefine "x" as a type variable
10561056
[out]
10571057

10581058
[case testTypevarWithType]
@@ -1406,7 +1406,7 @@ def g() -> None:
14061406
from typing_extensions import ParamSpec
14071407

14081408
TParams = ParamSpec('TParams')
1409-
TP = ParamSpec('?') # E: String argument 1 '?' to ParamSpec(...) does not match variable name 'TP'
1409+
TP = ParamSpec('?') # E: String argument 1 "?" to ParamSpec(...) does not match variable name "TP"
14101410
TP2: int = ParamSpec('TP2') # E: Cannot declare the type of a parameter specification
14111411

14121412
[out]

0 commit comments

Comments
 (0)