|
93 | 93 | infer_reachability_of_if_statement, infer_condition_value, ALWAYS_FALSE, ALWAYS_TRUE,
|
94 | 94 | MYPY_TRUE, MYPY_FALSE
|
95 | 95 | )
|
96 |
| -from mypy.typestate import TypeState |
| 96 | +from mypy.mro import calculate_mro, MroError |
97 | 97 |
|
98 | 98 | MYPY = False
|
99 | 99 | if MYPY:
|
@@ -3732,62 +3732,6 @@ def refers_to_class_or_function(node: Expression) -> bool:
|
3732 | 3732 | isinstance(node.node, (TypeInfo, FuncDef, OverloadedFuncDef)))
|
3733 | 3733 |
|
3734 | 3734 |
|
3735 |
| -def calculate_mro(info: TypeInfo, obj_type: Optional[Callable[[], Instance]] = None) -> None: |
3736 |
| - """Calculate and set mro (method resolution order). |
3737 |
| -
|
3738 |
| - Raise MroError if cannot determine mro. |
3739 |
| - """ |
3740 |
| - mro = linearize_hierarchy(info, obj_type) |
3741 |
| - assert mro, "Could not produce a MRO at all for %s" % (info,) |
3742 |
| - info.mro = mro |
3743 |
| - # The property of falling back to Any is inherited. |
3744 |
| - info.fallback_to_any = any(baseinfo.fallback_to_any for baseinfo in info.mro) |
3745 |
| - TypeState.reset_all_subtype_caches_for(info) |
3746 |
| - |
3747 |
| - |
3748 |
| -class MroError(Exception): |
3749 |
| - """Raised if a consistent mro cannot be determined for a class.""" |
3750 |
| - |
3751 |
| - |
3752 |
| -def linearize_hierarchy(info: TypeInfo, |
3753 |
| - obj_type: Optional[Callable[[], Instance]] = None) -> List[TypeInfo]: |
3754 |
| - # TODO describe |
3755 |
| - if info.mro: |
3756 |
| - return info.mro |
3757 |
| - bases = info.direct_base_classes() |
3758 |
| - if (not bases and info.fullname() != 'builtins.object' and |
3759 |
| - obj_type is not None): |
3760 |
| - # Second pass in import cycle, add a dummy `object` base class, |
3761 |
| - # otherwise MRO calculation may spuriously fail. |
3762 |
| - # MRO will be re-calculated for real in the third pass. |
3763 |
| - bases = [obj_type().type] |
3764 |
| - lin_bases = [] |
3765 |
| - for base in bases: |
3766 |
| - assert base is not None, "Cannot linearize bases for %s %s" % (info.fullname(), bases) |
3767 |
| - lin_bases.append(linearize_hierarchy(base, obj_type)) |
3768 |
| - lin_bases.append(bases) |
3769 |
| - return [info] + merge(lin_bases) |
3770 |
| - |
3771 |
| - |
3772 |
| -def merge(seqs: List[List[TypeInfo]]) -> List[TypeInfo]: |
3773 |
| - seqs = [s[:] for s in seqs] |
3774 |
| - result = [] # type: List[TypeInfo] |
3775 |
| - while True: |
3776 |
| - seqs = [s for s in seqs if s] |
3777 |
| - if not seqs: |
3778 |
| - return result |
3779 |
| - for seq in seqs: |
3780 |
| - head = seq[0] |
3781 |
| - if not [s for s in seqs if head in s[1:]]: |
3782 |
| - break |
3783 |
| - else: |
3784 |
| - raise MroError() |
3785 |
| - result.append(head) |
3786 |
| - for s in seqs: |
3787 |
| - if s[0] is head: |
3788 |
| - del s[0] |
3789 |
| - |
3790 |
| - |
3791 | 3735 | def find_duplicate(list: List[T]) -> Optional[T]:
|
3792 | 3736 | """If the list has duplicates, return one of the duplicates.
|
3793 | 3737 |
|
|
0 commit comments