Skip to content

Commit 222ac7b

Browse files
authored
Remove Python 2 logic from checkexpr (#13264)
1 parent e67fedf commit 222ac7b

File tree

3 files changed

+11
-68
lines changed

3 files changed

+11
-68
lines changed

mypy/checkexpr.py

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
)
9797
from mypy.sametypes import is_same_type
9898
from mypy.semanal_enum import ENUM_BASES
99-
from mypy.subtypes import is_equivalent, is_proper_subtype, is_subtype, non_method_protocol_members
99+
from mypy.subtypes import is_equivalent, is_subtype, non_method_protocol_members
100100
from mypy.traverser import has_await_expression
101101
from mypy.typeanal import (
102102
check_for_explicit_any,
@@ -2561,15 +2561,7 @@ def visit_complex_expr(self, e: ComplexExpr) -> Type:
25612561

25622562
def visit_ellipsis(self, e: EllipsisExpr) -> Type:
25632563
"""Type check '...'."""
2564-
if self.chk.options.python_version[0] >= 3:
2565-
return self.named_type("builtins.ellipsis")
2566-
else:
2567-
# '...' is not valid in normal Python 2 code, but it can
2568-
# be used in stubs. The parser makes sure that we only
2569-
# get this far if we are in a stub, and we can safely
2570-
# return 'object' as ellipsis is special cased elsewhere.
2571-
# The builtins.ellipsis type does not exist in Python 2.
2572-
return self.named_type("builtins.object")
2564+
return self.named_type("builtins.ellipsis")
25732565

25742566
def visit_op_expr(self, e: OpExpr) -> Type:
25752567
"""Type check a binary operator expression."""
@@ -2596,7 +2588,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
25962588
return self.concat_tuples(proper_left_type, proper_right_type)
25972589

25982590
if e.op in operators.op_methods:
2599-
method = self.get_operator_method(e.op)
2591+
method = operators.op_methods[e.op]
26002592
result, method_type = self.check_op(method, left_type, e.right, e, allow_reverse=True)
26012593
e.method_type = method_type
26022594
return result
@@ -2673,7 +2665,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
26732665
else:
26742666
self.msg.add_errors(local_errors.filtered_errors())
26752667
elif operator in operators.op_methods:
2676-
method = self.get_operator_method(operator)
2668+
method = operators.op_methods[operator]
26772669

26782670
with ErrorWatcher(self.msg.errors) as w:
26792671
sub_result, method_type = self.check_op(
@@ -2779,11 +2771,10 @@ def dangerous_comparison(
27792771
left = remove_optional(left)
27802772
right = remove_optional(right)
27812773
left, right = get_proper_types((left, right))
2782-
py2 = self.chk.options.python_version < (3, 0)
27832774
if (
27842775
original_container
2785-
and has_bytes_component(original_container, py2)
2786-
and has_bytes_component(left, py2)
2776+
and has_bytes_component(original_container)
2777+
and has_bytes_component(left)
27872778
):
27882779
# We need to special case bytes and bytearray, because 97 in b'abc', b'a' in b'abc',
27892780
# b'a' in bytearray(b'abc') etc. all return True (and we want to show the error only
@@ -2805,12 +2796,6 @@ def dangerous_comparison(
28052796
return False
28062797
return not is_overlapping_types(left, right, ignore_promotions=False)
28072798

2808-
def get_operator_method(self, op: str) -> str:
2809-
if op == "/" and self.chk.options.python_version[0] == 2:
2810-
return "__truediv__" if self.chk.tree.is_future_flag_set("division") else "__div__"
2811-
else:
2812-
return operators.op_methods[op]
2813-
28142799
def check_method_call_by_name(
28152800
self,
28162801
method: str,
@@ -2973,7 +2958,7 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
29732958
# STEP 1:
29742959
# We start by getting the __op__ and __rop__ methods, if they exist.
29752960

2976-
rev_op_name = self.get_reverse_op_method(op_name)
2961+
rev_op_name = operators.reverse_op_methods[op_name]
29772962

29782963
left_op = lookup_operator(op_name, left_type)
29792964
right_op = lookup_operator(rev_op_name, right_type)
@@ -2986,7 +2971,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
29862971
# We store the determined order inside the 'variants_raw' variable,
29872972
# which records tuples containing the method, base type, and the argument.
29882973

2989-
bias_right = is_proper_subtype(right_type, left_type)
29902974
if op_name in operators.op_methods_that_shortcut and is_same_type(left_type, right_type):
29912975
# When we do "A() + A()", for example, Python will only call the __add__ method,
29922976
# never the __radd__ method.
@@ -3019,22 +3003,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
30193003

30203004
variants_raw = [(left_op, left_type, right_expr), (right_op, right_type, left_expr)]
30213005

3022-
# STEP 2b:
3023-
# When running Python 2, we might also try calling the __cmp__ method.
3024-
3025-
is_python_2 = self.chk.options.python_version[0] == 2
3026-
if is_python_2 and op_name in operators.ops_falling_back_to_cmp:
3027-
cmp_method = operators.comparison_fallback_method
3028-
left_cmp_op = lookup_operator(cmp_method, left_type)
3029-
right_cmp_op = lookup_operator(cmp_method, right_type)
3030-
3031-
if bias_right:
3032-
variants_raw.append((right_cmp_op, right_type, left_expr))
3033-
variants_raw.append((left_cmp_op, left_type, right_expr))
3034-
else:
3035-
variants_raw.append((left_cmp_op, left_type, right_expr))
3036-
variants_raw.append((right_cmp_op, right_type, left_expr))
3037-
30383006
# STEP 3:
30393007
# We now filter out all non-existent operators. The 'variants' list contains
30403008
# all operator methods that are actually present, in the order that Python
@@ -3217,12 +3185,6 @@ def check_op(
32173185
context=context,
32183186
)
32193187

3220-
def get_reverse_op_method(self, method: str) -> str:
3221-
if method == "__div__" and self.chk.options.python_version[0] == 2:
3222-
return "__rdiv__"
3223-
else:
3224-
return operators.reverse_op_methods[method]
3225-
32263188
def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
32273189
"""Type check a boolean operation ('and' or 'or')."""
32283190

@@ -3543,8 +3505,6 @@ def visit_enum_index_expr(
35433505
self, enum_type: TypeInfo, index: Expression, context: Context
35443506
) -> Type:
35453507
string_type: Type = self.named_type("builtins.str")
3546-
if self.chk.options.python_version[0] < 3:
3547-
string_type = UnionType.make_union([string_type, self.named_type("builtins.unicode")])
35483508
self.chk.check_subtype(
35493509
self.accept(index),
35503510
string_type,
@@ -4176,10 +4136,7 @@ def _super_arg_types(self, e: SuperExpr) -> Union[Type, Tuple[Type, Type]]:
41764136
if not self.chk.in_checked_function():
41774137
return AnyType(TypeOfAny.unannotated)
41784138
elif len(e.call.args) == 0:
4179-
if self.chk.options.python_version[0] == 2:
4180-
self.chk.fail(message_registry.TOO_FEW_ARGS_FOR_SUPER, e)
4181-
return AnyType(TypeOfAny.from_error)
4182-
elif not e.info:
4139+
if not e.info:
41834140
# This has already been reported by the semantic analyzer.
41844141
return AnyType(TypeOfAny.from_error)
41854142
elif self.chk.scope.active_class():
@@ -4528,7 +4485,7 @@ def is_valid_var_arg(self, typ: Type) -> bool:
45284485

45294486
def is_valid_keyword_var_arg(self, typ: Type) -> bool:
45304487
"""Is a type valid as a **kwargs argument?"""
4531-
ret = (
4488+
return (
45324489
is_subtype(
45334490
typ,
45344491
self.chk.named_generic_type(
@@ -4544,15 +4501,6 @@ def is_valid_keyword_var_arg(self, typ: Type) -> bool:
45444501
)
45454502
or isinstance(typ, ParamSpecType)
45464503
)
4547-
if self.chk.options.python_version[0] < 3:
4548-
ret = ret or is_subtype(
4549-
typ,
4550-
self.chk.named_generic_type(
4551-
"typing.Mapping",
4552-
[self.named_type("builtins.unicode"), AnyType(TypeOfAny.special_form)],
4553-
),
4554-
)
4555-
return ret
45564504

45574505
def has_member(self, typ: Type, member: str) -> bool:
45584506
"""Does type have member with the given name?"""
@@ -5152,13 +5100,10 @@ def is_expr_literal_type(node: Expression) -> bool:
51525100
return False
51535101

51545102

5155-
def has_bytes_component(typ: Type, py2: bool = False) -> bool:
5103+
def has_bytes_component(typ: Type) -> bool:
51565104
"""Is this one of builtin byte types, or a union that contains it?"""
51575105
typ = get_proper_type(typ)
5158-
if py2:
5159-
byte_types = {"builtins.str", "builtins.bytearray"}
5160-
else:
5161-
byte_types = {"builtins.bytes", "builtins.bytearray"}
5106+
byte_types = {"builtins.bytes", "builtins.bytearray"}
51625107
if isinstance(typ, UnionType):
51635108
return any(has_bytes_component(t) for t in typ.items)
51645109
if isinstance(typ, Instance) and typ.type.fullname in byte_types:

mypy/message_registry.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
173173

174174
# Super
175175
TOO_MANY_ARGS_FOR_SUPER: Final = ErrorMessage('Too many arguments for "super"')
176-
TOO_FEW_ARGS_FOR_SUPER: Final = ErrorMessage('Too few arguments for "super"', codes.CALL_ARG)
177176
SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED: Final = ErrorMessage(
178177
'"super" with a single argument not supported'
179178
)

mypy/operators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
op_methods_to_symbols: Final = {v: k for (k, v) in op_methods.items()}
3131
op_methods_to_symbols["__div__"] = "/"
3232

33-
comparison_fallback_method: Final = "__cmp__"
3433
ops_falling_back_to_cmp: Final = {"__ne__", "__eq__", "__lt__", "__le__", "__gt__", "__ge__"}
3534

3635

0 commit comments

Comments
 (0)