Skip to content

Commit 92fe829

Browse files
jacobtylerwallsDanielNoord
authored andcommitted
Avoid duplicate inference results for List[int]
This became necessary once we fixed the cache key in 0740a0d.
1 parent 12c3d15 commit 92fe829

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

astroid/brain/brain_typing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ def infer_typing_alias(
316316
# This is an issue in cases where the aliased class implements it,
317317
# but the typing alias isn't subscriptable. E.g., `typing.ByteString` for PY39+
318318
_forbid_class_getitem_access(class_def)
319+
320+
# Avoid re-instantiating this class every time it's seen
321+
node._explicit_inference = lambda node, context: iter([class_def])
319322
return iter([class_def])
320323

321324

tests/brain/test_brain.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,16 @@ def __init__(self, value):
650650
assert isinstance(slots[0], nodes.Const)
651651
assert slots[0].value == "value"
652652

653+
@test_utils.require_version(minver="3.9")
654+
def test_typing_no_duplicates(self):
655+
node = builder.extract_node(
656+
"""
657+
from typing import List
658+
List[int]
659+
"""
660+
)
661+
assert len(node.inferred()) == 1
662+
653663
def test_collections_generic_alias_slots(self):
654664
"""Test slots for a class which is a subclass of a generic alias type."""
655665
node = builder.extract_node(

0 commit comments

Comments
 (0)