Skip to content

Commit

Permalink
Avoid duplicate inference results for Tuple[Optional[int], ...] (#2340
Browse files Browse the repository at this point in the history
) (#2342)

(cherry picked from commit dc13d5b)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
  • Loading branch information
github-actions[bot] and jacobtylerwalls authored Dec 12, 2023
1 parent d57c912 commit 7cad63a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ What's New in astroid 3.0.2?
============================
Release date: TBA

* Avoid duplicate inference results for some uses of ``typing.X`` constructs like
``Tuple[Optional[int], ...]``. This was causing pylint to occasionally omit
messages like ``deprecated-typing-alias``.

Closes pylint-dev/pylint#9220


What's New in astroid 3.0.1?
Expand Down
4 changes: 4 additions & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def infer_typing_attr(
cache = node.parent.__cache # type: ignore[attr-defined] # Unrecognized getattr
if cache.get(node.parent.slots) is not None:
del cache[node.parent.slots]
# Avoid re-instantiating this class every time it's seen
node._explicit_inference = lambda node, context: iter([value])
return iter([value])

node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]))
Expand Down Expand Up @@ -393,6 +395,8 @@ def infer_special_alias(
class_def.postinit(bases=[res], body=[], decorators=None)
func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
class_def.locals["__class_getitem__"] = [func_to_add]
# Avoid re-instantiating this class every time it's seen
node._explicit_inference = lambda node, context: iter([class_def])
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 @@ -668,6 +668,16 @@ def test_typing_no_duplicates(self):
)
assert len(node.inferred()) == 1

@test_utils.require_version(minver="3.9")
def test_typing_no_duplicates_2(self):
node = builder.extract_node(
"""
from typing import Optional, Tuple
Tuple[Optional[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

0 comments on commit 7cad63a

Please sign in to comment.