Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Apr 8, 2021
1 parent 5f17248 commit 39cfad4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def infer_typing_attr(
# node.parent.slots is evaluated and cached before the inference tip
# is first applied. Remove the last result to allow a recalculation of slots
cache = getattr(node.parent, "__cache")
if cache and cache.get(node.parent.slots) is not None:
if cache.get(node.parent.slots) is not None:
del cache[node.parent.slots]
return iter([value])

Expand Down
25 changes: 14 additions & 11 deletions astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,39 @@ def _c3_merge(sequences, cls, context):


def clean_typing_generic_mro(sequences: List[List["ClassDef"]]) -> None:
"""typing.Generic is allowed to appear multiple times in the initial mro.
The final one however, MUST only contain ONE.
"""A class can inherit from typing.Generic directly, as base,
and as base of bases. The merged MRO must however, only contain the last entry.
To prepare for _c3_merge, remove some typing.Generic entries from
sequences if multiple are present.
This method will check if Generic is in inferred_bases, but also
This method will check if Generic is in inferred_bases and also
part of bases_mro. If true, remove it from inferred_bases
as well as its entry the bases_mro.
Format sequences: [[self]] + bases_mro + [inferred_bases]
"""
pos_generic_in_main_bases = -1
# Check if part of inferred_bases
for i, base in enumerate(sequences[-1]):
bases_mro = sequences[1:-1]
inferred_bases = sequences[-1]
# Check if Generic is part of inferred_bases
for i, base in enumerate(inferred_bases):
if base.qname() == "typing.Generic":
pos_generic_in_main_bases = i
position_in_inferred_bases = i
break
else:
return
# Check if also part of bases_mro
# Ignore entry for typing.Generic
for i, seq in enumerate(sequences[1:-1]):
if i == pos_generic_in_main_bases:
for i, seq in enumerate(bases_mro):
if i == position_in_inferred_bases:
continue
if any(base.qname() == "typing.Generic" for base in seq):
break
else:
return
# Found multiple Generics in mro, remove entry from inferred_bases
# and the corresponding one from bases_mro
sequences[-1].pop(pos_generic_in_main_bases)
sequences.pop(pos_generic_in_main_bases + 1)
inferred_bases.pop(position_in_inferred_bases)
bases_mro.pop(position_in_inferred_bases)


def clean_duplicates_mro(sequences, cls, context):
Expand Down

0 comments on commit 39cfad4

Please sign in to comment.