Skip to content

Use double quotes errors 'is not defined' and 'Cannot instantiate' #10278

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 2 commits into from
Apr 4, 2021
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
2 changes: 1 addition & 1 deletion docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ version of Python considers legal code. These can result in some of the
following errors when trying to run your code:

* ``ImportError`` from circular imports
* ``NameError: name 'X' is not defined`` from forward references
* ``NameError: name "X" is not defined`` from forward references
* ``TypeError: 'type' object is not subscriptable`` from types that are not generic at runtime
* ``ImportError`` or ``ModuleNotFoundError`` from use of stub definitions not available at runtime
* ``TypeError: unsupported operand type(s) for |: 'type' and 'type'`` from use of new syntax
Expand Down
4 changes: 2 additions & 2 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ This example accidentally calls ``sort()`` instead of :py:func:`sorted`:

.. code-block:: python

x = sort([3, 2, 4]) # Error: Name 'sort' is not defined [name-defined]
x = sort([3, 2, 4]) # Error: Name "sort" is not defined [name-defined]

Check arguments in calls [call-arg]
-----------------------------------
Expand Down Expand Up @@ -565,7 +565,7 @@ Example:

... # No "save" method

# Error: Cannot instantiate abstract class 'Thing' with abstract attribute 'save' [abstract]
# Error: Cannot instantiate abstract class "Thing" with abstract attribute "save" [abstract]
t = Thing()

Check the target of NewType [valid-newtype]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/runtime_troubles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defined (aka forward reference). Thus this code does not work as expected:

.. code-block:: python

def f(x: A) -> None: ... # NameError: name 'A' is not defined
def f(x: A) -> None: ... # NameError: name "A" is not defined
class A: ...

Starting from Python 3.7, you can add ``from __future__ import annotations`` to
Expand Down
6 changes: 3 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,9 @@ def incompatible_conditional_function_def(self, defn: FuncDef) -> None:
def cannot_instantiate_abstract_class(self, class_name: str,
abstract_attributes: List[str],
context: Context) -> None:
attrs = format_string_list(["'%s'" % a for a in abstract_attributes])
self.fail("Cannot instantiate abstract class '%s' with abstract "
"attribute%s %s" % (class_name, plural_s(abstract_attributes),
attrs = format_string_list(['"%s"' % a for a in abstract_attributes])
self.fail('Cannot instantiate abstract class "%s" with abstract '
'attribute%s %s' % (class_name, plural_s(abstract_attributes),
attrs),
context, code=codes.ABSTRACT)

Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4731,7 +4731,7 @@ def name_not_defined(self, name: str, ctx: Context, namespace: Optional[str] = N
# later on. Defer current target.
self.record_incomplete_ref()
return
message = "Name '{}' is not defined".format(name)
message = 'Name "{}" is not defined'.format(name)
self.fail(message, ctx, code=codes.NAME_DEFINED)

if 'builtins.{}'.format(name) in SUGGESTED_TEST_FIXTURES:
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
if self.api.is_incomplete_namespace('builtins'):
self.api.record_incomplete_ref()
else:
self.fail("Name 'tuple' is not defined", t)
self.fail('Name "tuple" is not defined', t)
return AnyType(TypeOfAny.special_form)
if len(t.args) == 0 and not t.empty_tuple_index:
# Bare 'Tuple' is same as 'tuple'
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def load_static_checked(self, typ: RType, identifier: str, module_name: Optional
line: int = -1,
error_msg: Optional[str] = None) -> Value:
if error_msg is None:
error_msg = "name '{}' is not defined".format(identifier)
error_msg = 'name "{}" is not defined'.format(identifier)
ok_block, error_block = BasicBlock(), BasicBlock()
value = self.add(LoadStatic(typ, identifier, module_name, namespace, line=line))
self.add(Branch(value, error_block, ok_block, Branch.IS_ERROR, rare=True))
Expand Down
2 changes: 1 addition & 1 deletion mypyc/test-data/analysis.test
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ L6:
(6, 0) {a} {a}

[case testError]
def f(x: List[int]) -> None: pass # E: Name 'List' is not defined \
def f(x: List[int]) -> None: pass # E: Name "List" is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import List")

[case testExceptUndefined_Liveness]
Expand Down
4 changes: 2 additions & 2 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,12 @@ def f() -> None:
return 1 # E: No return value expected

[case testReportSemanticaAnalysisError1]
def f(x: List[int]) -> None: pass # E: Name 'List' is not defined \
def f(x: List[int]) -> None: pass # E: Name "List" is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import List")

[case testReportSemanticaAnalysisError2]
def f() -> None:
x # E: Name 'x' is not defined
x # E: Name "x" is not defined

[case testElif]
def f(n: int) -> int:
Expand Down
2 changes: 1 addition & 1 deletion mypyc/test-data/refcount.test
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ L0:
return r1

[case testError]
def f(x: List[int]) -> None: pass # E: Name 'List' is not defined \
def f(x: List[int]) -> None: pass # E: Name "List" is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import List")

[case testNewList]
Expand Down
34 changes: 17 additions & 17 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class B(metaclass=ABCMeta):
@abstractmethod
def f(self): pass
A() # OK
B() # E: Cannot instantiate abstract class 'B' with abstract attribute 'f'
B() # E: Cannot instantiate abstract class "B" with abstract attribute "f"
[out]

[case testInstantiatingClassWithInheritedAbstractMethod]
Expand All @@ -169,7 +169,7 @@ class A(metaclass=ABCMeta):
@abstractmethod
def g(self): pass
class B(A): pass
B() # E: Cannot instantiate abstract class 'B' with abstract attributes 'f' and 'g'
B() # E: Cannot instantiate abstract class "B" with abstract attributes "f" and "g"
[out]

[case testInstantiationAbstractsInTypeForFunctions]
Expand All @@ -187,7 +187,7 @@ class C(B):
def f(cls: Type[A]) -> A:
return cls() # OK
def g() -> A:
return A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'm'
return A() # E: Cannot instantiate abstract class "A" with abstract attribute "m"

f(A) # E: Only concrete class can be given where "Type[A]" is expected
f(B) # E: Only concrete class can be given where "Type[A]" is expected
Expand All @@ -213,7 +213,7 @@ def f(cls: Type[A]) -> A:

Alias = A
GoodAlias = C
Alias() # E: Cannot instantiate abstract class 'A' with abstract attribute 'm'
Alias() # E: Cannot instantiate abstract class "A" with abstract attribute "m"
GoodAlias()
f(Alias) # E: Only concrete class can be given where "Type[A]" is expected
f(GoodAlias)
Expand Down Expand Up @@ -293,7 +293,7 @@ class A(metaclass=ABCMeta):
def i(self): pass
@abstractmethod
def j(self): pass
a = A() # E: Cannot instantiate abstract class 'A' with abstract attributes 'a', 'b', ... and 'j' (7 methods suppressed)
a = A() # E: Cannot instantiate abstract class "A" with abstract attributes "a", "b", ... and "j" (7 methods suppressed)
[out]


Expand Down Expand Up @@ -524,8 +524,8 @@ class D(A, B):
class E(A, B):
def f(self) -> None: pass
def g(self) -> None: pass
C() # E: Cannot instantiate abstract class 'C' with abstract attribute 'g'
D() # E: Cannot instantiate abstract class 'D' with abstract attribute 'f'
C() # E: Cannot instantiate abstract class "C" with abstract attribute "g"
D() # E: Cannot instantiate abstract class "D" with abstract attribute "f"
E()

[case testInconsistentMro]
Expand Down Expand Up @@ -555,7 +555,7 @@ class B(A):
def f(self, x: int) -> int: pass
@overload
def f(self, x: str) -> str: pass
A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'f'
A() # E: Cannot instantiate abstract class "A" with abstract attribute "f"
B()
B().f(1)
a = B() # type: A
Expand Down Expand Up @@ -585,7 +585,7 @@ class B(A):
def f(self, x: int) -> int: pass
@overload
def f(self, x: str) -> str: pass
A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'f'
A() # E: Cannot instantiate abstract class "A" with abstract attribute "f"
B()
B().f(1)
a = B() # type: A
Expand Down Expand Up @@ -738,7 +738,7 @@ class A(metaclass=ABCMeta):
@abstractproperty
def x(self) -> int: pass
class B(A): pass
b = B() # E: Cannot instantiate abstract class 'B' with abstract attribute 'x'
b = B() # E: Cannot instantiate abstract class "B" with abstract attribute "x"

[case testInstantiateClassWithReadWriteAbstractProperty]
from abc import abstractproperty, ABCMeta
Expand All @@ -748,7 +748,7 @@ class A(metaclass=ABCMeta):
@x.setter
def x(self, x: int) -> None: pass
class B(A): pass
b = B() # E: Cannot instantiate abstract class 'B' with abstract attribute 'x'
b = B() # E: Cannot instantiate abstract class "B" with abstract attribute "x"

[case testImplementAbstractPropertyViaProperty]
from abc import abstractproperty, ABCMeta
Expand Down Expand Up @@ -803,7 +803,7 @@ b.x.y # E
[builtins fixtures/property.pyi]
[out]
main:7: error: Property "x" defined in "A" is read-only
main:8: error: Cannot instantiate abstract class 'B' with abstract attribute 'x'
main:8: error: Cannot instantiate abstract class "B" with abstract attribute "x"
main:9: error: "int" has no attribute "y"

[case testSuperWithAbstractProperty]
Expand Down Expand Up @@ -952,15 +952,15 @@ class A:

class C(B): pass

A.B() # E: Cannot instantiate abstract class 'B' with abstract attribute 'f'
A.C() # E: Cannot instantiate abstract class 'C' with abstract attribute 'f'
A.B() # E: Cannot instantiate abstract class "B" with abstract attribute "f"
A.C() # E: Cannot instantiate abstract class "C" with abstract attribute "f"

[case testAbstractNewTypeAllowed]
from typing import NewType, Mapping

Config = NewType('Config', Mapping[str, str])

bad = Mapping[str, str]() # E: Cannot instantiate abstract class 'Mapping' with abstract attribute '__iter__'
bad = Mapping[str, str]() # E: Cannot instantiate abstract class "Mapping" with abstract attribute "__iter__"
default = Config({'cannot': 'modify'}) # OK

default[1] = 2 # E: Unsupported target for indexed assignment ("Config")
Expand Down Expand Up @@ -1008,9 +1008,9 @@ a.do()
b = my_concrete_types['B']()
b.do()

c = my_abstract_types['A']() # E: Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'
c = my_abstract_types['A']() # E: Cannot instantiate abstract class "MyAbstractType" with abstract attribute "do"
c.do()
d = my_abstract_types['B']() # E: Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'
d = my_abstract_types['B']() # E: Cannot instantiate abstract class "MyAbstractType" with abstract attribute "do"
d.do()

[builtins fixtures/dict.pyi]
6 changes: 3 additions & 3 deletions test-data/unit/check-annotated.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ reveal_type(x) # N: Revealed type is "builtins.int"

[case testAnnotatedBadType]
from typing_extensions import Annotated
x: Annotated[XXX, ...] # E: Name 'XXX' is not defined
x: Annotated[XXX, ...] # E: Name "XXX" is not defined
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

Expand Down Expand Up @@ -56,7 +56,7 @@ reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"

[case testAnnotatedNestedBadType]
from typing_extensions import Annotated
x: Annotated[Annotated[XXX, ...], ...] # E: Name 'XXX' is not defined
x: Annotated[Annotated[XXX, ...], ...] # E: Name "XXX" is not defined
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

Expand All @@ -73,7 +73,7 @@ reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNoImport]
x: Annotated[int, ...] # E: Name 'Annotated' is not defined
x: Annotated[int, ...] # E: Name "Annotated" is not defined
reveal_type(x) # N: Revealed type is "Any"

[case testAnnotatedDifferentName]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ import attr

@attr.s
class C:
total = attr.ib(type=Bad) # E: Name 'Bad' is not defined
total = attr.ib(type=Bad) # E: Name "Bad" is not defined
[builtins fixtures/bool.pyi]

[case testTypeInAttrForwardInRuntime]
Expand Down Expand Up @@ -1329,7 +1329,7 @@ import attr

@attr.s(frozen=True)
class C:
total = attr.ib(type=Bad) # E: Name 'Bad' is not defined
total = attr.ib(type=Bad) # E: Name "Bad" is not defined

C(0).total = 1 # E: Property "total" defined in "C" is read-only
[builtins fixtures/bool.pyi]
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class D(object):
class A(object):
def f(self) -> None:
self.attr = 1
attr # E: Name 'attr' is not defined
attr # E: Name "attr" is not defined

class B(object):
attr = 0
Expand Down Expand Up @@ -2918,7 +2918,7 @@ C(arg=0)

[case testErrorMapToSupertype]
import typing
class X(Nope): pass # E: Name 'Nope' is not defined
class X(Nope): pass # E: Name "Nope" is not defined
a, b = X() # Used to crash here (#2244)


Expand Down Expand Up @@ -5239,7 +5239,7 @@ b = object()
@b.nothing # E: "object" has no attribute "nothing"
class C: pass

@undefined # E: Name 'undefined' is not defined
@undefined # E: Name "undefined" is not defined
class D: pass

[case testSlotsCompatibility]
Expand Down Expand Up @@ -6120,9 +6120,9 @@ class B(A):
class C(B):
def __init__(self, a: int) -> None:
self.c = a
a = A(1) # E: Cannot instantiate abstract class 'A' with abstract attribute '__init__'
a = A(1) # E: Cannot instantiate abstract class "A" with abstract attribute "__init__"
A.c # E: "Type[A]" has no attribute "c"
b = B(2) # E: Cannot instantiate abstract class 'B' with abstract attribute '__init__'
b = B(2) # E: Cannot instantiate abstract class "B" with abstract attribute "__init__"
B.c # E: "Type[B]" has no attribute "c"
c = C(3)
c.c
Expand Down Expand Up @@ -6619,7 +6619,7 @@ reveal_type(Foo().y) # N: Revealed type is "builtins.list[Any]"

class Foo:
def bad(): # E: Method must have at least one argument
self.x = 0 # E: Name 'self' is not defined
self.x = 0 # E: Name "self" is not defined

[case testTypeAfterAttributeAccessWithDisallowAnyExpr]
# flags: --disallow-any-expr
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ if int():
pass

[case testColumnNameIsNotDefined]
((x)) # E:3: Name 'x' is not defined
((x)) # E:3: Name "x" is not defined

[case testColumnNeedTypeAnnotation]
if 1:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ from dataclasses import dataclass

@dataclass
class B:
x: Undefined # E: Name 'Undefined' is not defined
y = undefined() # E: Name 'undefined' is not defined
x: Undefined # E: Name "Undefined" is not defined
y = undefined() # E: Name "undefined" is not defined

reveal_type(B) # N: Revealed type is "def (x: Any) -> __main__.B"
[builtins fixtures/list.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ main:2: error: Too few arguments for Enum()
main:3: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:4: error: Too many arguments for Enum()
main:5: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:5: error: Name 'foo' is not defined
main:5: error: Name "foo" is not defined
main:7: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:8: error: Too few arguments for IntEnum()
main:9: error: IntEnum() expects a string, tuple, list or dict literal as the second argument
Expand Down
Loading