96
96
)
97
97
from mypy .sametypes import is_same_type
98
98
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
100
100
from mypy .traverser import has_await_expression
101
101
from mypy .typeanal import (
102
102
check_for_explicit_any ,
@@ -2561,15 +2561,7 @@ def visit_complex_expr(self, e: ComplexExpr) -> Type:
2561
2561
2562
2562
def visit_ellipsis (self , e : EllipsisExpr ) -> Type :
2563
2563
"""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" )
2573
2565
2574
2566
def visit_op_expr (self , e : OpExpr ) -> Type :
2575
2567
"""Type check a binary operator expression."""
@@ -2596,7 +2588,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
2596
2588
return self .concat_tuples (proper_left_type , proper_right_type )
2597
2589
2598
2590
if e .op in operators .op_methods :
2599
- method = self . get_operator_method ( e .op )
2591
+ method = operators . op_methods [ e .op ]
2600
2592
result , method_type = self .check_op (method , left_type , e .right , e , allow_reverse = True )
2601
2593
e .method_type = method_type
2602
2594
return result
@@ -2673,7 +2665,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
2673
2665
else :
2674
2666
self .msg .add_errors (local_errors .filtered_errors ())
2675
2667
elif operator in operators .op_methods :
2676
- method = self . get_operator_method ( operator )
2668
+ method = operators . op_methods [ operator ]
2677
2669
2678
2670
with ErrorWatcher (self .msg .errors ) as w :
2679
2671
sub_result , method_type = self .check_op (
@@ -2779,11 +2771,10 @@ def dangerous_comparison(
2779
2771
left = remove_optional (left )
2780
2772
right = remove_optional (right )
2781
2773
left , right = get_proper_types ((left , right ))
2782
- py2 = self .chk .options .python_version < (3 , 0 )
2783
2774
if (
2784
2775
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 )
2787
2778
):
2788
2779
# We need to special case bytes and bytearray, because 97 in b'abc', b'a' in b'abc',
2789
2780
# 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(
2805
2796
return False
2806
2797
return not is_overlapping_types (left , right , ignore_promotions = False )
2807
2798
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
-
2814
2799
def check_method_call_by_name (
2815
2800
self ,
2816
2801
method : str ,
@@ -2973,7 +2958,7 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
2973
2958
# STEP 1:
2974
2959
# We start by getting the __op__ and __rop__ methods, if they exist.
2975
2960
2976
- rev_op_name = self . get_reverse_op_method ( op_name )
2961
+ rev_op_name = operators . reverse_op_methods [ op_name ]
2977
2962
2978
2963
left_op = lookup_operator (op_name , left_type )
2979
2964
right_op = lookup_operator (rev_op_name , right_type )
@@ -2986,7 +2971,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
2986
2971
# We store the determined order inside the 'variants_raw' variable,
2987
2972
# which records tuples containing the method, base type, and the argument.
2988
2973
2989
- bias_right = is_proper_subtype (right_type , left_type )
2990
2974
if op_name in operators .op_methods_that_shortcut and is_same_type (left_type , right_type ):
2991
2975
# When we do "A() + A()", for example, Python will only call the __add__ method,
2992
2976
# never the __radd__ method.
@@ -3019,22 +3003,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
3019
3003
3020
3004
variants_raw = [(left_op , left_type , right_expr ), (right_op , right_type , left_expr )]
3021
3005
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
-
3038
3006
# STEP 3:
3039
3007
# We now filter out all non-existent operators. The 'variants' list contains
3040
3008
# all operator methods that are actually present, in the order that Python
@@ -3217,12 +3185,6 @@ def check_op(
3217
3185
context = context ,
3218
3186
)
3219
3187
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
-
3226
3188
def check_boolean_op (self , e : OpExpr , context : Context ) -> Type :
3227
3189
"""Type check a boolean operation ('and' or 'or')."""
3228
3190
@@ -3543,8 +3505,6 @@ def visit_enum_index_expr(
3543
3505
self , enum_type : TypeInfo , index : Expression , context : Context
3544
3506
) -> Type :
3545
3507
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" )])
3548
3508
self .chk .check_subtype (
3549
3509
self .accept (index ),
3550
3510
string_type ,
@@ -4176,10 +4136,7 @@ def _super_arg_types(self, e: SuperExpr) -> Union[Type, Tuple[Type, Type]]:
4176
4136
if not self .chk .in_checked_function ():
4177
4137
return AnyType (TypeOfAny .unannotated )
4178
4138
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 :
4183
4140
# This has already been reported by the semantic analyzer.
4184
4141
return AnyType (TypeOfAny .from_error )
4185
4142
elif self .chk .scope .active_class ():
@@ -4528,7 +4485,7 @@ def is_valid_var_arg(self, typ: Type) -> bool:
4528
4485
4529
4486
def is_valid_keyword_var_arg (self , typ : Type ) -> bool :
4530
4487
"""Is a type valid as a **kwargs argument?"""
4531
- ret = (
4488
+ return (
4532
4489
is_subtype (
4533
4490
typ ,
4534
4491
self .chk .named_generic_type (
@@ -4544,15 +4501,6 @@ def is_valid_keyword_var_arg(self, typ: Type) -> bool:
4544
4501
)
4545
4502
or isinstance (typ , ParamSpecType )
4546
4503
)
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
4556
4504
4557
4505
def has_member (self , typ : Type , member : str ) -> bool :
4558
4506
"""Does type have member with the given name?"""
@@ -5152,13 +5100,10 @@ def is_expr_literal_type(node: Expression) -> bool:
5152
5100
return False
5153
5101
5154
5102
5155
- def has_bytes_component (typ : Type , py2 : bool = False ) -> bool :
5103
+ def has_bytes_component (typ : Type ) -> bool :
5156
5104
"""Is this one of builtin byte types, or a union that contains it?"""
5157
5105
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" }
5162
5107
if isinstance (typ , UnionType ):
5163
5108
return any (has_bytes_component (t ) for t in typ .items )
5164
5109
if isinstance (typ , Instance ) and typ .type .fullname in byte_types :
0 commit comments