Skip to content

Fix (some of ) #7445 #10053

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 5 commits into from
Feb 11, 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/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ potentially problematic or redundant in some way.
.. code-block:: python

def process(x: int) -> None:
# Error: Right operand of 'or' is never evaluated
# Error: Right operand of "or" is never evaluated
if isinstance(x, int) or x > 7:
# Error: Unsupported operand types for + ("int" and "str")
print(x + "bad")
Expand Down
2 changes: 1 addition & 1 deletion docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Example with an error:

class Bundle:
def __init__(self) -> None:
# Error: Need type annotation for 'items'
# Error: Need type annotation for "items"
# (hint: "items: List[<type>] = ...") [var-annotated]
self.items = []

Expand Down
4 changes: 2 additions & 2 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ incorrect control flow or conditional checks that are accidentally always true o
# mypy: warn-unreachable

def example(x: int) -> None:
# Error: Right operand of 'or' is never evaluated [unreachable]
# Error: Right operand of "or" is never evaluated [unreachable]
assert isinstance(x, int) or x == 'unused'

return
Expand All @@ -205,7 +205,7 @@ mypy generates an error if it thinks that an expression is redundant.
# mypy: enable-error-code redundant-expr

def example(x: int) -> None:
# Error: Left operand of 'and' is always true [redundant-expr]
# Error: Left operand of "and" is always true [redundant-expr]
if isinstance(x, int) and x > 0:
pass

Expand Down
2 changes: 1 addition & 1 deletion docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ for example, when assigning an empty dictionary to some global value:

.. code-block:: python

my_global_dict = {} # Error: Need type annotation for 'my_global_dict'
my_global_dict = {} # Error: Need type annotation for "my_global_dict"

You can teach mypy what type ``my_global_dict`` is meant to have by giving it
a type hint. For example, if you knew this variable is supposed to be a dict
Expand Down
4 changes: 2 additions & 2 deletions docs/source/type_inference_and_annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ without some help:

.. code-block:: python

l = [] # Error: Need type annotation for 'l'
l = [] # Error: Need type annotation for "l"

In these cases you can give the type explicitly using a type annotation:

Expand Down Expand Up @@ -162,7 +162,7 @@ in the following statement:
def foo(arg: List[int]) -> None:
print('Items:', ', '.join(arg))

a = [] # Error: Need type annotation for 'a'
a = [] # Error: Need type annotation for "a"
Copy link
Member

Choose a reason for hiding this comment

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

Great job also updating the documentation

foo(a)

Working around the issue is easy by adding a type annotation:
Expand Down
26 changes: 14 additions & 12 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def deleted_as_rvalue(self, typ: DeletedType, context: Context) -> None:
if typ.source is None:
s = ""
else:
s = " '{}'".format(typ.source)
s = ' "{}"'.format(typ.source)
self.fail('Trying to read deleted variable{}'.format(s), context)

def deleted_as_lvalue(self, typ: DeletedType, context: Context) -> None:
Expand All @@ -699,7 +699,7 @@ def deleted_as_lvalue(self, typ: DeletedType, context: Context) -> None:
if typ.source is None:
s = ""
else:
s = " '{}'".format(typ.source)
s = ' "{}"'.format(typ.source)
self.fail('Assignment to variable{} outside except: block'.format(s), context)

def no_variant_matches_arguments(self,
Expand Down Expand Up @@ -1101,7 +1101,7 @@ def need_annotation_for_var(self, node: SymbolNode, context: Context,
else:
needed = 'comment'

self.fail("Need type {} for '{}'{}".format(needed, unmangle(node.name), hint), context,
self.fail('Need type {} for "{}"{}'.format(needed, unmangle(node.name), hint), context,
code=codes.VAR_ANNOTATED)

def explicit_any(self, ctx: Context) -> None:
Expand Down Expand Up @@ -1160,10 +1160,12 @@ def typeddict_key_not_found(
self.fail('\'{}\' is not a valid TypedDict key; expected one of {}'.format(
item_name, format_item_name_list(typ.items.keys())), context)
else:
self.fail("TypedDict {} has no key '{}'".format(format_type(typ), item_name), context)
self.fail('TypedDict {} has no key "{}"'.format(
format_type(typ), item_name), context)
matches = best_matches(item_name, typ.items.keys())
if matches:
self.note("Did you mean {}?".format(pretty_seq(matches[:3], "or")), context)
self.note("Did you mean {}?".format(
pretty_seq(matches[:3], "or")), context)

def typeddict_context_ambiguous(
self,
Expand All @@ -1179,10 +1181,10 @@ def typeddict_key_cannot_be_deleted(
item_name: str,
context: Context) -> None:
if typ.is_anonymous():
self.fail("TypedDict key '{}' cannot be deleted".format(item_name),
self.fail('TypedDict key "{}" cannot be deleted'.format(item_name),
context)
else:
self.fail("Key '{}' of TypedDict {} cannot be deleted".format(
self.fail('Key "{}" of TypedDict {} cannot be deleted'.format(
item_name, format_type(typ)), context)

def typeddict_setdefault_arguments_inconsistent(
Expand Down Expand Up @@ -1235,8 +1237,8 @@ def typed_function_untyped_decorator(self, func_name: str, context: Context) ->

def bad_proto_variance(self, actual: int, tvar_name: str, expected: int,
context: Context) -> None:
msg = capitalize("{} type variable '{}' used in protocol where"
" {} one is expected".format(variance_string(actual),
msg = capitalize('{} type variable "{}" used in protocol where'
' {} one is expected'.format(variance_string(actual),
tvar_name,
variance_string(expected)))
self.fail(msg, context)
Expand Down Expand Up @@ -1280,14 +1282,14 @@ def redundant_left_operand(self, op_name: str, context: Context) -> None:
it does not change the truth value of the entire condition as a whole.
'op_name' should either be the string "and" or the string "or".
"""
self.redundant_expr("Left operand of '{}'".format(op_name), op_name == 'and', context)
self.redundant_expr('Left operand of "{}"'.format(op_name), op_name == 'and', context)

def unreachable_right_operand(self, op_name: str, context: Context) -> None:
"""Indicates that the right operand of a boolean expression is redundant:
it does not change the truth value of the entire condition as a whole.
'op_name' should either be the string "and" or the string "or".
"""
self.fail("Right operand of '{}' is never evaluated".format(op_name),
self.fail('Right operand of "{}" is never evaluated'.format(op_name),
context, code=codes.UNREACHABLE)

def redundant_condition_in_comprehension(self, truthiness: bool, context: Context) -> None:
Expand Down Expand Up @@ -1352,7 +1354,7 @@ def report_protocol_problems(self,
missing = get_missing_protocol_members(subtype, supertype)
if (missing and len(missing) < len(supertype.type.protocol_members) and
len(missing) <= MAX_ITEMS):
self.note("'{}' is missing following '{}' protocol member{}:"
self.note('"{}" is missing following "{}" protocol member{}:'
.format(subtype.type.name, supertype.type.name, plural_s(missing)),
context,
code=code)
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/check-attr.test
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ A(1, [2], '3', 4, 5) # E: Too many arguments for "A"
import attr
@attr.s
class A:
a = attr.ib() # E: Need type annotation for 'a'
_b = attr.ib() # E: Need type annotation for '_b'
c = attr.ib(18) # E: Need type annotation for 'c'
_d = attr.ib(validator=None, default=18) # E: Need type annotation for '_d'
a = attr.ib() # E: Need type annotation for "a"
_b = attr.ib() # E: Need type annotation for "_b"
c = attr.ib(18) # E: Need type annotation for "c"
_d = attr.ib(validator=None, default=18) # E: Need type annotation for "_d"
E = 18
[builtins fixtures/bool.pyi]

Expand Down Expand Up @@ -959,9 +959,9 @@ class A:
a: int
b = 17
# The following forms are not allowed with auto_attribs=True
c = attr.ib() # E: Need type annotation for 'c'
d, e = attr.ib(), attr.ib() # E: Need type annotation for 'd' # E: Need type annotation for 'e'
f = g = attr.ib() # E: Need type annotation for 'f' # E: Need type annotation for 'g'
c = attr.ib() # E: Need type annotation for "c"
d, e = attr.ib(), attr.ib() # E: Need type annotation for "d" # E: Need type annotation for "e"
f = g = attr.ib() # E: Need type annotation for "f" # E: Need type annotation for "g"
[builtins fixtures/bool.pyi]

[case testAttrsRepeatedName]
Expand Down Expand Up @@ -1253,7 +1253,7 @@ import attr

@attr.s
class B:
x = attr.ib() # E: Need type annotation for 'x'
x = attr.ib() # E: Need type annotation for "x"

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-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ class C:
x = C.x
[builtins fixtures/list.pyi]
[out]
main:2: error: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
main:2: error: Need type annotation for "x" (hint: "x: List[<type>] = ...")

[case testAccessingGenericClassAttribute]
from typing import Generic, TypeVar
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ if int():

[case testColumnNeedTypeAnnotation]
if 1:
x = [] # E:5: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
x = [] # E:5: Need type annotation for "x" (hint: "x: List[<type>] = ...")
[builtins fixtures/list.pyi]

[case testColumnCallToUntypedFunction]
Expand Down Expand Up @@ -254,7 +254,7 @@ t: D = {'x':
'y'} # E:5: Incompatible types (expression has type "str", TypedDict item "x" has type "int")

if int():
del t['y'] # E:5: TypedDict "D" has no key 'y'
del t['y'] # E:5: TypedDict "D" has no key "y"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

Expand Down Expand Up @@ -297,7 +297,7 @@ class C:
p: P
if int():
p = C() # E:9: Incompatible types in assignment (expression has type "C", variable has type "P") \
# N:9: 'C' is missing following 'P' protocol member: \
# N:9: "C" is missing following "P" protocol member: \
# N:9: y

[case testColumnRedundantCast]
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ from typing import TypeVar

T = TypeVar('T')
def f() -> T: pass
x = f() # E: Need type annotation for 'x' [var-annotated]
y = [] # E: Need type annotation for 'y' (hint: "y: List[<type>] = ...") [var-annotated]
x = f() # E: Need type annotation for "x" [var-annotated]
y = [] # E: Need type annotation for "y" (hint: "y: List[<type>] = ...") [var-annotated]
[builtins fixtures/list.pyi]

[case testErrorCodeBadOverride]
Expand Down Expand Up @@ -778,8 +778,8 @@ def foo() -> bool: ...

lst = [1, 2, 3, 4]

b = False or foo() # E: Left operand of 'or' is always false [redundant-expr]
c = True and foo() # E: Left operand of 'and' is always true [redundant-expr]
b = False or foo() # E: Left operand of "or" is always false [redundant-expr]
c = True and foo() # E: Left operand of "and" is always true [redundant-expr]
g = 3 if True else 4 # E: If condition is always true [redundant-expr]
h = 3 if False else 4 # E: If condition is always false [redundant-expr]
i = [x for x in lst if True] # E: If condition in comprehension is always true [redundant-expr]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2170,7 +2170,7 @@ d5 = dict(a=1, b='') # type: Dict[str, Any]

[case testDictWithoutKeywordArgs]
from typing import Dict
d = dict() # E: Need type annotation for 'd' (hint: "d: Dict[<type>, <type>] = ...")
d = dict() # E: Need type annotation for "d" (hint: "d: Dict[<type>, <type>] = ...")
d2 = dict() # type: Dict[int, str]
dict(undefined) # E: Name 'undefined' is not defined
[builtins fixtures/dict.pyi]
Expand Down Expand Up @@ -2390,8 +2390,8 @@ class B: ...
[builtins fixtures/dict.pyi]

[case testTypeAnnotationNeededMultipleAssignment]
x, y = [], [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...") \
# E: Need type annotation for 'y' (hint: "y: List[<type>] = ...")
x, y = [], [] # E: Need type annotation for "x" (hint: "x: List[<type>] = ...") \
# E: Need type annotation for "y" (hint: "y: List[<type>] = ...")
[builtins fixtures/list.pyi]

[case testStrictEqualityEq]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ def g(l: List[str]) -> None: pass # no error
def h(l: List[List]) -> None: pass # E: Missing type parameters for generic type "List"
def i(l: List[List[List[List]]]) -> None: pass # E: Missing type parameters for generic type "List"

x = [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
x = [] # E: Need type annotation for "x" (hint: "x: List[<type>] = ...")
y: List = [] # E: Missing type parameters for generic type "List"
[builtins fixtures/list.pyi]

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ if g(C()):
def f(x: B) -> B: pass

[case testRedefineFunctionDefinedAsVariableInitializedToEmptyList]
f = [] # E: Need type annotation for 'f' (hint: "f: List[<type>] = ...")
f = [] # E: Need type annotation for "f" (hint: "f: List[<type>] = ...")
if object():
def f(): pass # E: Incompatible redefinition
f() # E: "List[Any]" not callable
Expand Down Expand Up @@ -2344,7 +2344,7 @@ def make_list() -> List[T]: pass

l: List[int] = make_list()

bad = make_list() # E: Need type annotation for 'bad' (hint: "bad: List[<type>] = ...")
bad = make_list() # E: Need type annotation for "bad" (hint: "bad: List[<type>] = ...")
[builtins fixtures/list.pyi]

[case testAnonymousArgumentError]
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class B: pass
from typing import TypeVar, Generic
T = TypeVar('T')
def g() -> None:
x = f() # E: Need type annotation for 'x'
x = f() # E: Need type annotation for "x"

def f() -> 'A[T]': pass
class A(Generic[T]): pass
Expand Down Expand Up @@ -425,7 +425,7 @@ class B(A): pass
[case testLocalVariableInferenceFromEmptyList]
import typing
def f() -> None:
a = [] # E: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
a = [] # E: Need type annotation for "a" (hint: "a: List[<type>] = ...")
b = [None]
c = [B()]
if int():
Expand Down
Loading