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

Avoid duplicate inference results for Tuple[Optional[int], ...] #2340

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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
Loading