Skip to content

Commit dc13d5b

Browse files
Avoid duplicate inference results for Tuple[Optional[int], ...] (#2340)
1 parent 15d2544 commit dc13d5b

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ What's New in astroid 3.0.2?
1717
============================
1818
Release date: TBA
1919

20+
* Avoid duplicate inference results for some uses of ``typing.X`` constructs like
21+
``Tuple[Optional[int], ...]``. This was causing pylint to occasionally omit
22+
messages like ``deprecated-typing-alias``.
23+
24+
Closes pylint-dev/pylint#9220
2025

2126

2227
What's New in astroid 3.0.1?

astroid/brain/brain_typing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def infer_typing_attr(
188188
cache = node.parent.__cache # type: ignore[attr-defined] # Unrecognized getattr
189189
if cache.get(node.parent.slots) is not None:
190190
del cache[node.parent.slots]
191+
# Avoid re-instantiating this class every time it's seen
192+
node._explicit_inference = lambda node, context: iter([value])
191193
return iter([value])
192194

193195
node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]))
@@ -393,6 +395,8 @@ def infer_special_alias(
393395
class_def.postinit(bases=[res], body=[], decorators=None)
394396
func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
395397
class_def.locals["__class_getitem__"] = [func_to_add]
398+
# Avoid re-instantiating this class every time it's seen
399+
node._explicit_inference = lambda node, context: iter([class_def])
396400
return iter([class_def])
397401

398402

tests/brain/test_brain.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ def test_typing_no_duplicates(self):
668668
)
669669
assert len(node.inferred()) == 1
670670

671+
@test_utils.require_version(minver="3.9")
672+
def test_typing_no_duplicates_2(self):
673+
node = builder.extract_node(
674+
"""
675+
from typing import Optional, Tuple
676+
Tuple[Optional[int], ...]
677+
"""
678+
)
679+
assert len(node.inferred()) == 1
680+
671681
def test_collections_generic_alias_slots(self):
672682
"""Test slots for a class which is a subclass of a generic alias type."""
673683
node = builder.extract_node(

0 commit comments

Comments
 (0)