Skip to content

Commit 3ae67be

Browse files
authored
Remove some Python 2 type checking logic (#13261)
1 parent e2fb448 commit 3ae67be

File tree

2 files changed

+9
-66
lines changed

2 files changed

+9
-66
lines changed

mypy/checker.py

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,6 @@ def check_first_pass(self) -> None:
469469
seq_str = self.named_generic_type(
470470
"typing.Sequence", [self.named_type("builtins.str")]
471471
)
472-
if self.options.python_version[0] < 3:
473-
seq_str = self.named_generic_type(
474-
"typing.Sequence", [self.named_type("builtins.unicode")]
475-
)
476472
if not is_subtype(all_.type, seq_str):
477473
str_seq_s, all_s = format_type_distinctly(seq_str, all_.type)
478474
self.fail(
@@ -1093,18 +1089,6 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
10931089
if not self.is_generator_return_type(typ.ret_type, defn.is_coroutine):
10941090
self.fail(message_registry.INVALID_RETURN_TYPE_FOR_GENERATOR, typ)
10951091

1096-
# Python 2 generators aren't allowed to return values.
1097-
orig_ret_type = get_proper_type(typ.ret_type)
1098-
if (
1099-
self.options.python_version[0] == 2
1100-
and isinstance(orig_ret_type, Instance)
1101-
and orig_ret_type.type.fullname == "typing.Generator"
1102-
):
1103-
if not isinstance(
1104-
get_proper_type(orig_ret_type.args[2]), (NoneType, AnyType)
1105-
):
1106-
self.fail(message_registry.INVALID_GENERATOR_RETURN_ITEM_TYPE, typ)
1107-
11081092
# Fix the type if decorated with `@types.coroutine` or `@asyncio.coroutine`.
11091093
if defn.is_awaitable_coroutine:
11101094
# Update the return type to AwaitableGenerator.
@@ -1145,7 +1129,6 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
11451129
ref_type = mypy.types.TypeType.make_normalized(ref_type)
11461130
erased = get_proper_type(erase_to_bound(arg_type))
11471131
if not is_subtype(ref_type, erased, ignore_type_params=True):
1148-
note = None
11491132
if (
11501133
isinstance(erased, Instance)
11511134
and erased.type.is_protocol
@@ -1158,23 +1141,13 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
11581141
# the consistency check will be performed at call sites.
11591142
msg = None
11601143
elif typ.arg_names[i] in {"self", "cls"}:
1161-
if (
1162-
self.options.python_version[0] < 3
1163-
and is_same_type(erased, arg_type)
1164-
and not isclass
1165-
):
1166-
msg = message_registry.INVALID_SELF_TYPE_OR_EXTRA_ARG
1167-
note = "(Hint: typically annotations omit the type for self)"
1168-
else:
1169-
msg = message_registry.ERASED_SELF_TYPE_NOT_SUPERTYPE.format(
1170-
erased, ref_type
1171-
)
1144+
msg = message_registry.ERASED_SELF_TYPE_NOT_SUPERTYPE.format(
1145+
erased, ref_type
1146+
)
11721147
else:
11731148
msg = message_registry.MISSING_OR_INVALID_SELF_TYPE
11741149
if msg:
11751150
self.fail(msg, defn)
1176-
if note:
1177-
self.note(note, defn)
11781151
elif isinstance(arg_type, TypeVarType):
11791152
# Refuse covariant parameter type variables
11801153
# TODO: check recursively for inner type variables
@@ -1287,16 +1260,10 @@ def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
12871260
)
12881261

12891262
def is_forward_op_method(self, method_name: str) -> bool:
1290-
if self.options.python_version[0] == 2 and method_name == "__div__":
1291-
return True
1292-
else:
1293-
return method_name in operators.reverse_op_methods
1263+
return method_name in operators.reverse_op_methods
12941264

12951265
def is_reverse_op_method(self, method_name: str) -> bool:
1296-
if self.options.python_version[0] == 2 and method_name == "__rdiv__":
1297-
return True
1298-
else:
1299-
return method_name in operators.reverse_op_method_set
1266+
return method_name in operators.reverse_op_method_set
13001267

13011268
def check_for_missing_annotations(self, fdef: FuncItem) -> None:
13021269
# Check for functions with unspecified/not fully specified types.
@@ -1459,10 +1426,7 @@ def check_reverse_op_method(
14591426
)
14601427
assert len(reverse_type.arg_types) >= 2
14611428

1462-
if self.options.python_version[0] == 2 and reverse_name == "__rdiv__":
1463-
forward_name = "__div__"
1464-
else:
1465-
forward_name = operators.normal_from_reverse_op[reverse_name]
1429+
forward_name = operators.normal_from_reverse_op[reverse_name]
14661430
forward_inst = get_proper_type(reverse_type.arg_types[1])
14671431
if isinstance(forward_inst, TypeVarType):
14681432
forward_inst = get_proper_type(forward_inst.upper_bound)
@@ -4198,20 +4162,10 @@ def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
41984162
self.accept(s.handlers[i])
41994163
var = s.vars[i]
42004164
if var:
4201-
# Exception variables are deleted in python 3 but not python 2.
4202-
# But, since it's bad form in python 2 and the type checking
4203-
# wouldn't work very well, we delete it anyway.
4204-
4165+
# Exception variables are deleted.
42054166
# Unfortunately, this doesn't let us detect usage before the
42064167
# try/except block.
4207-
if self.options.python_version[0] >= 3:
4208-
source = var.name
4209-
else:
4210-
source = (
4211-
'(exception variable "{}", which we do not '
4212-
"accept outside except: blocks even in "
4213-
"python 2)".format(var.name)
4214-
)
4168+
source = var.name
42154169
if isinstance(var.node, Var):
42164170
var.node.type = DeletedType(source=source)
42174171
self.binder.cleanse(var)
@@ -4302,11 +4256,7 @@ def analyze_iterable_item_type(self, expr: Expression) -> Tuple[Type, Type]:
43024256
return iterator, joined
43034257
else:
43044258
# Non-tuple iterable.
4305-
if self.options.python_version[0] >= 3:
4306-
nextmethod = "__next__"
4307-
else:
4308-
nextmethod = "next"
4309-
return iterator, echk.check_method_call_by_name(nextmethod, iterator, [], [], expr)[0]
4259+
return iterator, echk.check_method_call_by_name("__next__", iterator, [], [], expr)[0]
43104260

43114261
def analyze_container_item_type(self, typ: Type) -> Optional[Type]:
43124262
"""Check if a type is a nominal container of a union of such.

mypy/message_registry.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
4545
'The return type of an async generator function should be "AsyncGenerator" or one of its '
4646
"supertypes"
4747
)
48-
INVALID_GENERATOR_RETURN_ITEM_TYPE: Final = ErrorMessage(
49-
"The return type of a generator function must be None in"
50-
" its third type parameter in Python 2"
51-
)
5248
YIELD_VALUE_EXPECTED: Final = ErrorMessage("Yield value expected")
5349
INCOMPATIBLE_TYPES: Final = "Incompatible types"
5450
INCOMPATIBLE_TYPES_IN_ASSIGNMENT: Final = "Incompatible types in assignment"
@@ -203,9 +199,6 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
203199
ERASED_SELF_TYPE_NOT_SUPERTYPE: Final = ErrorMessage(
204200
'The erased type of self "{}" is not a supertype of its class "{}"'
205201
)
206-
INVALID_SELF_TYPE_OR_EXTRA_ARG: Final = ErrorMessage(
207-
"Invalid type for self, or extra argument type in function annotation"
208-
)
209202

210203
# Final
211204
CANNOT_INHERIT_FROM_FINAL: Final = ErrorMessage('Cannot inherit from final class "{}"')

0 commit comments

Comments
 (0)