Skip to content
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

Fix duplicate bases error (MROs) with GenericAlias #910

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Release Date: TBA

Closes #895 #899

* Fixed duplicate bases error for "typing._GenericAlias" (false-positive)

Closes #905

What's New in astroid 2.5?
============================
Release Date: 2021-02-15
Expand Down
7 changes: 6 additions & 1 deletion astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ def clean_duplicates_mro(sequences, cls, context):
(node.lineno, node.qname()) if node.name else None for node in sequence
]
last_index = dict(map(reversed, enumerate(names)))
if names and names[0] is not None and last_index[names[0]] != 0:
if (
names
and names[0] is not None
and last_index[names[0]] != 0
and names[0][1] != "typing._GenericAlias"
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not found of this kind of solution. I think there is maybe something to handle through TypingBrain instead.
However i'm not sure, i'm still investigating this.
By the way on my machine the bug is also present with python 3.9.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. Thanks for your work!

By the way on my machine the bug is also present with python 3.9.

Hmm, interesting. MacOS with Py 3.9.2 works. Please let me know if I can help test things, once you have found a better solution.

raise exceptions.DuplicateBasesError(
message="Duplicates found in MROs {mros} for {cls!r}.",
mros=sequences,
Expand Down
56 changes: 56 additions & 0 deletions tests/unittest_scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,62 @@ class C(scope.A, scope.B):
)
self.assertEqualMro(cls, ["C", "A", "B", "object"])

@test_utils.require_version("3.7", "3.9")
def test_mro_with_duplicate_generic_alias(self):
"""Catch false positive. Assert no error is thrown."""
cls = builder.extract_node(
"""
import abc
from typing import Sized, Iterable
class AbstractRoute(abc.ABC):
pass
class AbstractResource(Sized, Iterable["AbstractRoute"]):
pass
class IndexView(AbstractResource):
def __init__(self):
self.var = 1
"""
)
self.assertEqualMro(
cls,
[
"IndexView",
"AbstractResource",
"_GenericAlias",
"_Final",
"_GenericAlias",
"object",
],
)

@test_utils.require_version("3.9")
def test_mro_with_duplicate_generic_alias_2(self):
cls = builder.extract_node(
"""
import abc
from typing import Sized, Iterable
class AbstractRoute(abc.ABC):
pass
class AbstractResource(Sized, Iterable["AbstractRoute"]):
pass
class IndexView(AbstractResource):
def __init__(self):
self.var = 1
"""
)
self.assertEqualMro(
cls,
[
"IndexView",
"AbstractResource",
"_SpecialGenericAlias",
"_BaseGenericAlias",
"_Final",
"_SpecialGenericAlias",
"object",
],
)

def test_generator_from_infer_call_result_parent(self):
func = builder.extract_node(
"""
Expand Down