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

Infer the length argument of the random.sample function. #1862

Merged
merged 3 commits into from Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -16,6 +16,10 @@ Release date: TBA

Refs PyCQA/pylint#2567

* Infer the `length` argument of the ``random.sample`` function.

Refs PyCQA/pylint#7706

* Prevent returning an empty list for ``ClassDef.slots()`` when the mro list contains one class & it is not ``object``.

Refs PyCQA/pylint#5099
Expand Down
10 changes: 5 additions & 5 deletions astroid/brain/brain_random.py
Expand Up @@ -42,10 +42,10 @@ def infer_random_sample(node, context=None):
if len(node.args) != 2:
raise UseInferenceDefault

length = node.args[1]
if not isinstance(length, Const):
inferred_length = helpers.safe_infer(node.args[1], context=context)
if not isinstance(inferred_length, Const):
raise UseInferenceDefault
if not isinstance(length.value, int):
if not isinstance(inferred_length.value, int):
raise UseInferenceDefault

inferred_sequence = helpers.safe_infer(node.args[0], context=context)
Expand All @@ -55,12 +55,12 @@ def infer_random_sample(node, context=None):
if not isinstance(inferred_sequence, ACCEPTED_ITERABLES_FOR_SAMPLE):
raise UseInferenceDefault

if length.value > len(inferred_sequence.elts):
if inferred_length.value > len(inferred_sequence.elts):
# In this case, this will raise a ValueError
raise UseInferenceDefault

try:
elts = random.sample(inferred_sequence.elts, length.value)
elts = random.sample(inferred_sequence.elts, inferred_length.value)
except ValueError as exc:
raise UseInferenceDefault from exc

Expand Down
23 changes: 23 additions & 0 deletions tests/unittest_brain.py
Expand Up @@ -2415,6 +2415,29 @@ def test_inferred_successfully(self) -> None:
elems = sorted(elem.value for elem in inferred.elts)
self.assertEqual(elems, [1, 2])

def test_arguments_inferred_successfully(self) -> None:
"""Test inference of `random.sample` when both arguments are of type `nodes.Call`."""
node = astroid.extract_node(
"""
import random

def sequence():
return [1, 2]

random.sample(sequence(), len([1,2])) #@
"""
)
# Check that arguments are of type `nodes.Call`.
sequence, length = node.args
self.assertIsInstance(sequence, astroid.Call)
self.assertIsInstance(length, astroid.Call)

# Check the inference of `random.sample` call.
inferred = next(node.infer())
self.assertIsInstance(inferred, astroid.List)
elems = sorted(elem.value for elem in inferred.elts)
self.assertEqual(elems, [1, 2])

def test_no_crash_on_evaluatedobject(self) -> None:
node = astroid.extract_node(
"""
Expand Down