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 expensive list/tuple multiplication operations #2228

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ Release date: TBA

Closes pylint-dev/pylint#8749

* Avoid expensive list/tuple multiplication operations that would result in ``MemoryError``.

Closes pylint-dev/pylint#8748


What's New in astroid 2.15.5?
=============================
Expand Down
3 changes: 3 additions & 0 deletions astroid/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def _multiply_seq_by_int(
context: InferenceContext,
) -> _TupleListNodeT:
node = self.__class__(parent=opnode)
if other.value > 1e8:
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
node.elts = [nodes.Const(NotImplemented)]
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be Uninferable instead of NotImplemented ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose that's better than implying there's one element in the list.

return node
filtered_elts = (
helpers.safe_infer(elt, context) or util.Uninferable
for elt in self.elts
Expand Down
7 changes: 7 additions & 0 deletions tests/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def test_uninferable_exponents() -> None:
parsed = extract_node("None ** 2")
assert parsed.inferred() == [Uninferable]

@staticmethod
def test_uninferable_list_multiplication() -> None:
"""Attempting to calculate the result is prohibitively expensive."""
parsed = extract_node("[0] * 123456789")
element = parsed.inferred()[0].elts[0]
assert element.value is NotImplemented


def test_named_expr_inference() -> None:
code = """
Expand Down