-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Refactor: move MRO calculation to a new module #6133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Optional, Callable, List | ||
|
||
from mypy.nodes import TypeInfo | ||
from mypy.types import Instance | ||
from mypy.typestate import TypeState | ||
|
||
|
||
def calculate_mro(info: TypeInfo, obj_type: Optional[Callable[[], Instance]] = None) -> None: | ||
"""Calculate and set mro (method resolution order). | ||
|
||
Raise MroError if cannot determine mro. | ||
""" | ||
mro = linearize_hierarchy(info, obj_type) | ||
assert mro, "Could not produce a MRO at all for %s" % (info,) | ||
info.mro = mro | ||
# The property of falling back to Any is inherited. | ||
info.fallback_to_any = any(baseinfo.fallback_to_any for baseinfo in info.mro) | ||
TypeState.reset_all_subtype_caches_for(info) | ||
|
||
|
||
class MroError(Exception): | ||
"""Raised if a consistent mro cannot be determined for a class.""" | ||
|
||
|
||
def linearize_hierarchy(info: TypeInfo, | ||
obj_type: Optional[Callable[[], Instance]] = None) -> List[TypeInfo]: | ||
# TODO describe | ||
if info.mro: | ||
return info.mro | ||
bases = info.direct_base_classes() | ||
if (not bases and info.fullname() != 'builtins.object' and | ||
obj_type is not None): | ||
# Second pass in import cycle, add a dummy `object` base class, | ||
# otherwise MRO calculation may spuriously fail. | ||
# MRO will be re-calculated for real in the third pass. | ||
bases = [obj_type().type] | ||
lin_bases = [] | ||
for base in bases: | ||
assert base is not None, "Cannot linearize bases for %s %s" % (info.fullname(), bases) | ||
lin_bases.append(linearize_hierarchy(base, obj_type)) | ||
lin_bases.append(bases) | ||
return [info] + merge(lin_bases) | ||
|
||
|
||
def merge(seqs: List[List[TypeInfo]]) -> List[TypeInfo]: | ||
seqs = [s[:] for s in seqs] | ||
result = [] # type: List[TypeInfo] | ||
while True: | ||
seqs = [s for s in seqs if s] | ||
if not seqs: | ||
return result | ||
for seq in seqs: | ||
head = seq[0] | ||
if not [s for s in seqs if head in s[1:]]: | ||
break | ||
else: | ||
raise MroError() | ||
result.append(head) | ||
for s in seqs: | ||
if s[0] is head: | ||
del s[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, third pass does
self.sem.calculate_class_mro(tdef)
, maybe we can also importcalculate_mro
there to reduce coupling between second and third passes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good idea. I'll do this in a separate PR since this requires moving some code around.