Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b9aa4c

Browse files
Use property instead of method (#11068)
Closes #2112. This PR changes the FunctionLike.items from method to property. By doing so, the items field is consistent with other types such as TupleType and is distinguished from dict.items().
1 parent c603e13 commit 7b9aa4c

25 files changed

Lines changed: 73 additions & 70 deletions

mypy/checker.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ def check_overlapping_op_methods(self,
12781278
reverse_class, reverse_name,
12791279
forward_base, forward_name, context)
12801280
elif isinstance(forward_item, Overloaded):
1281-
for item in forward_item.items():
1281+
for item in forward_item.items:
12821282
if self.is_unsafe_overlapping_op(item, forward_base, reverse_type):
12831283
self.msg.operator_method_signatures_overlap(
12841284
reverse_class, reverse_name,
@@ -1584,7 +1584,7 @@ def get_op_other_domain(self, tp: FunctionLike) -> Optional[Type]:
15841584
return tp.arg_types[0]
15851585
return None
15861586
elif isinstance(tp, Overloaded):
1587-
raw_items = [self.get_op_other_domain(it) for it in tp.items()]
1587+
raw_items = [self.get_op_other_domain(it) for it in tp.items]
15881588
items = [it for it in raw_items if it]
15891589
if items:
15901590
return make_simplified_union(items)
@@ -1683,13 +1683,13 @@ def erase_override(t: Type) -> Type:
16831683
# (in that order), and if the child swaps the two and does f(str) -> str and
16841684
# f(int) -> int
16851685
order = []
1686-
for child_variant in override.items():
1687-
for i, parent_variant in enumerate(original.items()):
1686+
for child_variant in override.items:
1687+
for i, parent_variant in enumerate(original.items):
16881688
if is_subtype(child_variant, parent_variant):
16891689
order.append(i)
16901690
break
16911691

1692-
if len(order) == len(original.items()) and order != sorted(order):
1692+
if len(order) == len(original.items) and order != sorted(order):
16931693
self.msg.overload_signature_incompatible_with_supertype(
16941694
name, name_in_super, supertype, override, node)
16951695
emitted_msg = True
@@ -2431,7 +2431,7 @@ def lvalue_type_from_base(self, expr_node: Var,
24312431
OverloadedFuncDef):
24322432
# Same for properties with setter
24332433
if base_node.is_property:
2434-
base_type = base_type.items()[0].ret_type
2434+
base_type = base_type.items[0].ret_type
24352435

24362436
return base_type, base_node
24372437

@@ -3524,7 +3524,7 @@ def check_except_handler_test(self, n: Expression) -> Type:
35243524
continue
35253525

35263526
if isinstance(ttype, FunctionLike):
3527-
item = ttype.items()[0]
3527+
item = ttype.items[0]
35283528
if not item.is_type_obj():
35293529
self.fail(message_registry.INVALID_EXCEPTION_TYPE, n)
35303530
return AnyType(TypeOfAny.from_error)
@@ -5496,7 +5496,7 @@ def get_isinstance_type(expr: Expression,
54965496
if isinstance(typ, FunctionLike) and typ.is_type_obj():
54975497
# Type variables may be present -- erase them, which is the best
54985498
# we can do (outside disallowing them here).
5499-
erased_type = erase_typevars(typ.items()[0].ret_type)
5499+
erased_type = erase_typevars(typ.items[0].ret_type)
55005500
types.append(TypeRange(erased_type, is_upper_bound=False))
55015501
elif isinstance(typ, TypeType):
55025502
# Type[A] means "any type that is a subtype of A" rather than "precisely type A"
@@ -5669,9 +5669,9 @@ def is_more_general_arg_prefix(t: FunctionLike, s: FunctionLike) -> bool:
56695669
ignore_return=True)
56705670
elif isinstance(t, FunctionLike):
56715671
if isinstance(s, FunctionLike):
5672-
if len(t.items()) == len(s.items()):
5672+
if len(t.items) == len(s.items):
56735673
return all(is_same_arg_prefix(items, itemt)
5674-
for items, itemt in zip(t.items(), s.items()))
5674+
for items, itemt in zip(t.items, s.items))
56755675
return False
56765676

56775677

@@ -6034,13 +6034,13 @@ def is_untyped_decorator(typ: Optional[Type]) -> bool:
60346034
method = typ.type.get_method('__call__')
60356035
if method:
60366036
if isinstance(method.type, Overloaded):
6037-
return any(is_untyped_decorator(item) for item in method.type.items())
6037+
return any(is_untyped_decorator(item) for item in method.type.items)
60386038
else:
60396039
return not is_typed_callable(method.type)
60406040
else:
60416041
return False
60426042
elif isinstance(typ, Overloaded):
6043-
return any(is_untyped_decorator(item) for item in typ.items())
6043+
return any(is_untyped_decorator(item) for item in typ.items)
60446044
return True
60456045

60466046

mypy/checkexpr.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def apply_signature_hook(
778778
else:
779779
assert isinstance(callee, Overloaded)
780780
items = []
781-
for item in callee.items():
781+
for item in callee.items:
782782
adjusted = self.apply_signature_hook(
783783
item, args, arg_kinds, arg_names, hook)
784784
assert isinstance(adjusted, CallableType)
@@ -1079,7 +1079,7 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
10791079
callee = callee.copy_modified(ret_type=item)
10801080
elif isinstance(callee, Overloaded):
10811081
callee = Overloaded([c.copy_modified(ret_type=item)
1082-
for c in callee.items()])
1082+
for c in callee.items])
10831083
return callee
10841084
# We support Type of namedtuples but not of tuples in general
10851085
if (isinstance(item, TupleType)
@@ -1675,7 +1675,7 @@ def has_shape(typ: Type) -> bool:
16751675
if kind == ARG_STAR2 and not has_shape(typ):
16761676
args_have_kw_arg = True
16771677

1678-
for typ in overload.items():
1678+
for typ in overload.items:
16791679
formal_to_actual = map_actuals_to_formals(arg_kinds, arg_names,
16801680
typ.arg_kinds, typ.arg_names,
16811681
lambda i: arg_types[i])
@@ -3223,13 +3223,13 @@ def apply_type_arguments_to_callable(
32233223
return AnyType(TypeOfAny.from_error)
32243224
return self.apply_generic_arguments(tp, args, ctx)
32253225
if isinstance(tp, Overloaded):
3226-
for it in tp.items():
3226+
for it in tp.items:
32273227
if len(it.variables) != len(args):
32283228
self.msg.incompatible_type_application(len(it.variables),
32293229
len(args), ctx)
32303230
return AnyType(TypeOfAny.from_error)
32313231
return Overloaded([self.apply_generic_arguments(it, args, ctx)
3232-
for it in tp.items()])
3232+
for it in tp.items])
32333233
return AnyType(TypeOfAny.special_form)
32343234

32353235
def visit_list_expr(self, e: ListExpr) -> Type:
@@ -4366,7 +4366,7 @@ def is_typetype_like(typ: ProperType) -> bool:
43664366
if isinstance(actual, CallableType):
43674367
actual = actual.fallback
43684368
if isinstance(actual, Overloaded):
4369-
actual = actual.items()[0].fallback
4369+
actual = actual.items[0].fallback
43704370
if isinstance(actual, TupleType):
43714371
actual = tuple_fallback(actual)
43724372
if isinstance(actual, Instance) and formal.type in actual.type.mro:

mypy/checkmember.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def analyze_type_callable_member_access(name: str,
230230
mx: MemberContext) -> Type:
231231
# Class attribute.
232232
# TODO super?
233-
ret_type = typ.items()[0].ret_type
233+
ret_type = typ.items[0].ret_type
234234
assert isinstance(ret_type, ProperType)
235235
if isinstance(ret_type, TupleType):
236236
ret_type = tuple_fallback(ret_type)
@@ -251,7 +251,7 @@ def analyze_type_callable_member_access(name: str,
251251
# See https://github.com/python/mypy/pull/1787 for more info.
252252
# TODO: do not rely on same type variables being present in all constructor overloads.
253253
result = analyze_class_attribute_access(ret_type, name, mx,
254-
original_vars=typ.items()[0].variables)
254+
original_vars=typ.items[0].variables)
255255
if result:
256256
return result
257257
# Look up from the 'type' type.
@@ -481,7 +481,7 @@ def analyze_descriptor_access(instance_type: Type,
481481
dunder_get_type = expand_type_by_instance(bound_method, typ)
482482

483483
if isinstance(instance_type, FunctionLike) and instance_type.is_type_obj():
484-
owner_type = instance_type.items()[0].ret_type
484+
owner_type = instance_type.items[0].ret_type
485485
instance_type = NoneType()
486486
elif isinstance(instance_type, TypeType):
487487
owner_type = instance_type.item
@@ -630,7 +630,7 @@ def freeze_type_vars(member_type: Type) -> None:
630630
for v in member_type.variables:
631631
v.id.meta_level = 0
632632
if isinstance(member_type, Overloaded):
633-
for it in member_type.items():
633+
for it in member_type.items:
634634
for v in it.variables:
635635
v.id.meta_level = 0
636636

@@ -664,7 +664,7 @@ def f(self: S) -> T: ...
664664
original type of 'x' is a union. This is done because several special methods
665665
treat union types in ad-hoc manner, so we can't use MemberContext.self_type yet.
666666
"""
667-
items = functype.items()
667+
items = functype.items
668668
if not items:
669669
return functype
670670
new_items = []
@@ -907,7 +907,7 @@ class B(A[str]): pass
907907
return Overloaded([cast(CallableType, add_class_tvars(item, isuper,
908908
is_classmethod, original_type,
909909
original_vars=original_vars))
910-
for item in t.items()])
910+
for item in t.items])
911911
if isuper is not None:
912912
t = cast(ProperType, expand_type_by_instance(t, isuper))
913913
return t

mypy/constraints.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,15 @@ def infer_against_any(self, types: Iterable[Type], any_type: AnyType) -> List[Co
555555

556556
def visit_overloaded(self, template: Overloaded) -> List[Constraint]:
557557
res: List[Constraint] = []
558-
for t in template.items():
558+
for t in template.items:
559559
res.extend(infer_constraints(t, self.actual, self.direction))
560560
return res
561561

562562
def visit_type_type(self, template: TypeType) -> List[Constraint]:
563563
if isinstance(self.actual, CallableType):
564564
return infer_constraints(template.item, self.actual.ret_type, self.direction)
565565
elif isinstance(self.actual, Overloaded):
566-
return infer_constraints(template.item, self.actual.items()[0].ret_type,
566+
return infer_constraints(template.item, self.actual.items[0].ret_type,
567567
self.direction)
568568
elif isinstance(self.actual, TypeType):
569569
return infer_constraints(template.item, self.actual.item, self.direction)
@@ -586,7 +586,7 @@ def neg_op(op: int) -> int:
586586

587587
def find_matching_overload_item(overloaded: Overloaded, template: CallableType) -> CallableType:
588588
"""Disambiguate overload item against a template."""
589-
items = overloaded.items()
589+
items = overloaded.items
590590
for item in items:
591591
# Return type may be indeterminate in the template, so ignore it when performing a
592592
# subtype check.

mypy/expandtype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def freshen_function_type_vars(callee: F) -> F:
5353
else:
5454
assert isinstance(callee, Overloaded)
5555
fresh_overload = Overloaded([freshen_function_type_vars(item)
56-
for item in callee.items()])
56+
for item in callee.items])
5757
return cast(F, fresh_overload)
5858

5959

@@ -106,7 +106,7 @@ def visit_callable_type(self, t: CallableType) -> Type:
106106

107107
def visit_overloaded(self, t: Overloaded) -> Type:
108108
items: List[CallableType] = []
109-
for item in t.items():
109+
for item in t.items:
110110
new_item = item.accept(self)
111111
assert isinstance(new_item, ProperType)
112112
assert isinstance(new_item, CallableType)

mypy/fixup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def visit_callable_type(self, ct: CallableType) -> None:
196196
ct.type_guard.accept(self)
197197

198198
def visit_overloaded(self, t: Overloaded) -> None:
199-
for ct in t.items():
199+
for ct in t.items:
200200
ct.accept(self)
201201

202202
def visit_erased_type(self, o: Any) -> None:

mypy/indirection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def visit_callable_type(self, t: types.CallableType) -> Set[str]:
8383
return out
8484

8585
def visit_overloaded(self, t: types.Overloaded) -> Set[str]:
86-
return self._visit(t.items()) | self._visit(t.fallback)
86+
return self._visit(t.items) | self._visit(t.fallback)
8787

8888
def visit_tuple_type(self, t: types.TupleType) -> Set[str]:
8989
return self._visit(t.items) | self._visit(t.partial_fallback)

mypy/join.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ def visit_overloaded(self, t: Overloaded) -> ProperType:
339339
s = self.s
340340
if isinstance(s, FunctionLike):
341341
# The interesting case where both types are function types.
342-
for t_item in t.items():
343-
for s_item in s.items():
342+
for t_item in t.items:
343+
for s_item in s.items:
344344
if is_similar_callables(t_item, s_item):
345345
if is_equivalent(t_item, s_item):
346346
result.append(combine_similar_callables(t_item, s_item))

mypy/meet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def get_possible_variants(typ: Type) -> List[Type]:
131131
elif isinstance(typ, Overloaded):
132132
# Note: doing 'return typ.items()' makes mypy
133133
# infer a too-specific return type of List[CallableType]
134-
return list(typ.items())
134+
return list(typ.items)
135135
else:
136136
return [typ]
137137

@@ -575,8 +575,8 @@ def visit_overloaded(self, t: Overloaded) -> ProperType:
575575
# as TypeJoinVisitor.visit_overloaded().
576576
s = self.s
577577
if isinstance(s, FunctionLike):
578-
if s.items() == t.items():
579-
return Overloaded(t.items())
578+
if s.items == t.items:
579+
return Overloaded(t.items)
580580
elif is_subtype(s, t):
581581
return s
582582
elif is_subtype(t, s):

mypy/messages.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ def pretty_overload(self,
15121512
add_class_or_static_decorator: bool = False,
15131513
allow_dups: bool = False,
15141514
code: Optional[ErrorCode] = None) -> None:
1515-
for item in tp.items()[:max_items]:
1515+
for item in tp.items[:max_items]:
15161516
self.note('@overload', context, offset=offset, allow_dups=allow_dups, code=code)
15171517

15181518
if add_class_or_static_decorator:
@@ -1522,7 +1522,7 @@ def pretty_overload(self,
15221522

15231523
self.note(pretty_callable(item), context,
15241524
offset=offset, allow_dups=allow_dups, code=code)
1525-
left = len(tp.items()) - max_items
1525+
left = len(tp.items) - max_items
15261526
if left > 0:
15271527
msg = '<{} more overload{} not shown>'.format(left, plural_s(left))
15281528
self.note(msg, context, offset=offset, allow_dups=allow_dups, code=code)
@@ -1535,11 +1535,11 @@ def pretty_overload_matches(self,
15351535
max_items: int,
15361536
code: ErrorCode) -> None:
15371537
if not targets:
1538-
targets = func.items()
1538+
targets = func.items
15391539

15401540
shown = min(max_items, len(targets))
15411541
max_matching = len(targets)
1542-
max_available = len(func.items())
1542+
max_available = len(func.items)
15431543

15441544
# If there are 3 matches but max_items == 2, we might as well show
15451545
# all three items instead of having the 3rd item be an error message.
@@ -1771,7 +1771,7 @@ def format(typ: Type) -> str:
17711771
if func.is_type_obj():
17721772
# The type of a type object type can be derived from the
17731773
# return type (this always works).
1774-
return format(TypeType.make_normalized(erase_type(func.items()[0].ret_type)))
1774+
return format(TypeType.make_normalized(erase_type(func.items[0].ret_type)))
17751775
elif isinstance(func, CallableType):
17761776
return_type = format(func.ret_type)
17771777
if func.is_ellipsis_args:

0 commit comments

Comments
 (0)