diff options
| author | Andrew Haigh <hello@nelf.in> | 2021-05-23 02:58:48 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-22 18:58:48 +0200 |
| commit | 54cfd7253973fa47915da4bdd1a55ed197d16bab (patch) | |
| tree | 943122f9579565d1496711d152f7225b0f728e41 | |
| parent | 01f9e2439513a83899b88b1053ca90f81ab49903 (diff) | |
| download | astroid-git-54cfd7253973fa47915da4bdd1a55ed197d16bab.tar.gz | |
Fix crash on random.sample brain (#993)
* Add test of crash on random.sample inference
Ref #922
* Fix crash in brain for random.sample
Fixes #922. A combination of changes in 79d5a3a7 and 77e205dc meant that
the node cloning helper could be given an EvaluatedObject, which doesn't
present the same __init__ signature as NodeNG.
* Update changelog
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/brain/brain_random.py | 2 | ||||
| -rw-r--r-- | tests/unittest_brain.py | 12 |
3 files changed, 18 insertions, 0 deletions
@@ -37,6 +37,10 @@ Release Date: TBA Closes PyCQA/pylint#3535 Closes PyCQA/pylint#4358 +* Update random brain to fix a crash with inference of some sequence elements + + Closes #922 + What's New in astroid 2.5.6? ============================ diff --git a/astroid/brain/brain_random.py b/astroid/brain/brain_random.py index ee5506cb..6efd1ff1 100644 --- a/astroid/brain/brain_random.py +++ b/astroid/brain/brain_random.py @@ -9,6 +9,8 @@ ACCEPTED_ITERABLES_FOR_SAMPLE = (astroid.List, astroid.Set, astroid.Tuple) def _clone_node_with_lineno(node, parent, lineno): + if isinstance(node, astroid.EvaluatedObject): + node = node.original cls = node.__class__ other_fields = node._other_fields _astroid_fields = node._astroid_fields diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 0e8c5911..26fad2e3 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -1858,6 +1858,18 @@ class RandomSampleTest(unittest.TestCase): elems = sorted(elem.value for elem in inferred.elts) self.assertEqual(elems, [1, 2]) + def test_no_crash_on_evaluatedobject(self): + node = astroid.extract_node( + """ + from random import sample + class A: pass + sample(list({1: A()}.values()), 1)""" + ) + inferred = next(node.infer()) + assert isinstance(inferred, astroid.List) + assert len(inferred.elts) == 1 + assert isinstance(inferred.elts[0], nodes.Call) + class SubprocessTest(unittest.TestCase): """Test subprocess brain""" |
