Skip to content

Avoid duplicate inference results for List[int] #2185

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
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ def infer_typing_alias(
# This is an issue in cases where the aliased class implements it,
# but the typing alias isn't subscriptable. E.g., `typing.ByteString` for PY39+
_forbid_class_getitem_access(class_def)

# Avoid re-instantiating this class every time it's seen
node._explicit_inference = lambda node, context: iter([class_def])
Copy link
Collaborator

Choose a reason for hiding this comment

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

This feels like something we might need to do for a lot of brains.

We us a similar pattern for the enum brain. πŸ˜“

Copy link
Member Author

Choose a reason for hiding this comment

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

You're quite right. I traced the bulk of the primer noise to needing to do this in other places in the typing brain.

return iter([class_def])


Expand Down
10 changes: 10 additions & 0 deletions tests/brain/test_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,16 @@ def __init__(self, value):
assert isinstance(slots[0], nodes.Const)
assert slots[0].value == "value"

@test_utils.require_version(minver="3.9")
def test_typing_no_duplicates(self):
node = builder.extract_node(
"""
from typing import List
List[int]
"""
)
assert len(node.inferred()) == 1

def test_collections_generic_alias_slots(self):
"""Test slots for a class which is a subclass of a generic alias type."""
node = builder.extract_node(
Expand Down