Skip to content

Commit

Permalink
Use double quotes in errors related to NewType, TypeVar, namedTuple a…
Browse files Browse the repository at this point in the history
…nd TypedDict (#10359)

* NewType related errors and Cannot redefine

* TypeVar related

* namedTuple - First argument

* TypedDict - First argument
  • Loading branch information
dixith authored Apr 22, 2021
1 parent bdcc562 commit d326952
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ consistently when using the call-based syntax. Example:
from typing import NamedTuple
# Error: First argument to namedtuple() should be 'Point2D', not 'Point'
# Error: First argument to namedtuple() should be "Point2D", not "Point"
Point2D = NamedTuple("Point", [("x", int), ("y", int)])
Report syntax errors [syntax]
Expand Down
14 changes: 7 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
self.fail("NamedTuple type as an attribute is not supported", lvalue)
return False
if internal_name != name:
self.fail("First argument to namedtuple() should be '{}', not '{}'".format(
self.fail('First argument to namedtuple() should be "{}", not "{}"'.format(
name, internal_name), s.rvalue, code=codes.NAME_MATCH)
return True
# Yes, it's a valid namedtuple, but defer if it is not ready.
Expand Down Expand Up @@ -2938,7 +2938,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
# Also give error for another type variable with the same name.
(isinstance(existing.node, TypeVarExpr) and
existing.node is call.analyzed)):
self.fail("Cannot redefine '%s' as a type variable" % name, s)
self.fail('Cannot redefine "%s" as a type variable' % name, s)
return False

if self.options.disallow_any_unimported:
Expand Down Expand Up @@ -2993,7 +2993,7 @@ def check_typevarlike_name(self, call: CallExpr, name: str, context: Context) ->
context)
return False
elif call.args[0].value != name:
msg = "String argument 1 '{}' to {}(...) does not match variable name '{}'"
msg = 'String argument 1 "{}" to {}(...) does not match variable name "{}"'
self.fail(msg.format(call.args[0].value, typevarlike_type, name), context)
return False
return True
Expand Down Expand Up @@ -3068,20 +3068,20 @@ def process_typevar_parameters(self, args: List[Expression],
analyzed = PlaceholderType(None, [], context.line)
upper_bound = get_proper_type(analyzed)
if isinstance(upper_bound, AnyType) and upper_bound.is_from_error:
self.fail("TypeVar 'bound' must be a type", param_value)
self.fail('TypeVar "bound" must be a type', param_value)
# Note: we do not return 'None' here -- we want to continue
# using the AnyType as the upper bound.
except TypeTranslationError:
self.fail("TypeVar 'bound' must be a type", param_value)
self.fail('TypeVar "bound" must be a type', param_value)
return None
elif param_name == 'values':
# Probably using obsolete syntax with values=(...). Explain the current syntax.
self.fail("TypeVar 'values' argument not supported", context)
self.fail('TypeVar "values" argument not supported', context)
self.fail("Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))",
context)
return None
else:
self.fail("Unexpected argument to TypeVar(): {}".format(param_name), context)
self.fail('Unexpected argument to TypeVar(): "{}"'.format(param_name), context)
return None

if covariant and contravariant:
Expand Down
4 changes: 2 additions & 2 deletions mypy/semanal_newtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def analyze_newtype_declaration(self,
# Give a better error message than generic "Name already defined".
if (existing and
not isinstance(existing.node, PlaceholderNode) and not s.rvalue.analyzed):
self.fail("Cannot redefine '%s' as a NewType" % name, s)
self.fail('Cannot redefine "%s" as a NewType' % name, s)

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

Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def check_typeddict(self,
else:
if var_name is not None and name != var_name:
self.fail(
"First argument '{}' to TypedDict() does not match variable name '{}'".format(
'First argument "{}" to TypedDict() does not match variable name "{}"'.format(
name, var_name), node, code=codes.NAME_MATCH)
if name != var_name or is_func_scope:
# Give it a unique name derived from the line number.
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4573,7 +4573,7 @@ def parse_ast(name_dict: NameDict) -> None:
[case testNoCrashForwardRefToBrokenDoubleNewType]
from typing import Any, Dict, List, NewType

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

def frob(foos: Dict[Any, Foos]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,11 @@ k = [x for x in lst if isinstance(x, int) or foo()] # E: If condition in compre
[case testNamedTupleNameMismatch]
from typing import NamedTuple

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

[case testTypedDictNameMismatch]
from typing_extensions import TypedDict

Foo = TypedDict("Bar", {}) # E: First argument 'Bar' to TypedDict() does not match variable name 'Foo' [name-match]
Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not match variable name "Foo" [name-match]
[builtins fixtures/dict.pyi]
8 changes: 4 additions & 4 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -5067,9 +5067,9 @@ from typing import NamedTuple
NT = NamedTuple('BadName', [('x', int)])
[builtins fixtures/tuple.pyi]
[out]
tmp/b.py:2: error: First argument to namedtuple() should be 'NT', not 'BadName'
tmp/b.py:2: error: First argument to namedtuple() should be "NT", not "BadName"
[out2]
tmp/b.py:2: error: First argument to namedtuple() should be 'NT', not 'BadName'
tmp/b.py:2: error: First argument to namedtuple() should be "NT", not "BadName"
tmp/a.py:3: note: Revealed type is "Tuple[builtins.int, fallback=b.NT]"

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

[case testNewAnalyzerIncrementalMethodNamedTuple]

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -967,9 +967,9 @@ Type1 = NamedTuple('Type1', [('foo', foo)]) # E: Function "b.foo" is not valid
from typing import NamedTuple
from collections import namedtuple

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

C = NamedTuple('X', [('a', 'Y')]) # E: First argument to namedtuple() should be 'C', not 'X'
C = NamedTuple('X', [('a', 'Y')]) # E: First argument to namedtuple() should be "C", not "X"
class Y: ...
[builtins fixtures/tuple.pyi]
8 changes: 4 additions & 4 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ A = Enum('A', ['z', 't']) # E: Name "A" already defined on line 3
from typing import NewType

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

[case testNewAnalyzerNewTypeForwardClass]
Expand Down Expand Up @@ -2160,7 +2160,7 @@ from typing import TypeVar, Generic, Any

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

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

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

[case testNewAnalyzerTypeVarBoundInCycle]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ tmp/m.py:14: note: Revealed type is "builtins.int"
[case testNewTypeBadInitializationFails]
from typing import NewType

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

a = 3
def f(): a
a = NewType('a', int) # E: Cannot redefine 'a' as a NewType \
a = NewType('a', int) # E: Cannot redefine "a" as a NewType \
# E: Name "a" already defined on line 4

b = NewType('b', int)
def g(): b
b = NewType('b', float) # E: Cannot redefine 'b' as a NewType \
b = NewType('b', float) # E: Cannot redefine "b" as a NewType \
# E: Name "b" already defined on line 8

c = NewType('c', str) # type: str # E: Cannot declare the type of a NewType declaration
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-redefine.test
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def f() -> None:
reveal_type(x) # N: Revealed type is "builtins.int"
y = 1
# NOTE: '"int" not callable' is due to test stubs
y = TypeVar('y') # E: Cannot redefine 'y' as a type variable \
y = TypeVar('y') # E: Cannot redefine "y" as a type variable \
# E: "int" not callable
def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ Point = TypedDict('Point', {'x': 1, 'y': 1}) # E: Invalid type: try using Liter

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


Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -1007,12 +1007,12 @@ from typing import TypeVar
a = TypeVar() # E: Too few arguments for TypeVar()
b = TypeVar(x='b') # E: TypeVar() expects a string literal as first argument
c = TypeVar(1) # E: TypeVar() expects a string literal as first argument
d = TypeVar('D') # E: String argument 1 'D' to TypeVar(...) does not match variable name 'd'
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): x
d = TypeVar('D') # E: String argument 1 "D" to TypeVar(...) does not match variable name "d"
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): "x"
f = TypeVar('f', (int, str), int) # E: Type expected
g = TypeVar('g', int) # E: TypeVar cannot have only a single constraint
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to TypeVar(): x
i = TypeVar('i', bound=1) # E: TypeVar 'bound' must be a type
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to TypeVar(): "x"
i = TypeVar('i', bound=1) # E: TypeVar "bound" must be a type
[out]

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

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

[case testTypevarWithType]
Expand Down Expand Up @@ -1406,7 +1406,7 @@ def g() -> None:
from typing_extensions import ParamSpec

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

[out]

0 comments on commit d326952

Please sign in to comment.