Repository: pylint-dev/astroid

Issue:
__init__() got an unexpected keyword argument 'lineno' from brain_random.py
### Steps to reproduce & current behavior
```
~/tempenv$ pylint --version
pylint 2.7.2
astroid 2.5.1
Python 3.8.7 (default, Dec 22 2020, 10:37:26)
[GCC 10.2.0]
(tempenv) ~/tempenv$ cat example.py
class A:
    pass

b = sample(list({1: A()}.values()), 1)
(tempenv) ~/tempenv$ pylint example.py
************* Module example
example.py:1:0: C0114: Missing module docstring (missing-module-docstring)
example.py:1:0: C0103: Class name "A" doesn't conform to PascalCase naming style (invalid-name)
example.py:1:0: C0115: Missing class docstring (missing-class-docstring)
example.py:1:0: R0903: Too few public methods (0/2) (too-few-public-methods)
Traceback (most recent call last):
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/__init__.py", line 94, in _inference_tip_cached
    return iter(_cache[func, node])
KeyError: (<function infer_random_sample at 0x7f5d412c5940>, <Call l.4 at 0x7f5d40decf70>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/gps/tempenv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/__init__.py", line 22, in run_pylint
    PylintRun(sys.argv[1:])
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/run.py", line 358, in __init__
    linter.check(args)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 862, in check
    self._check_files(
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 896, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 922, in _check_file
    check_astroid_module(ast_node)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1054, in check_astroid_module
    retval = self._check_astroid_module(
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1099, in _check_astroid_module
    walker.walk(ast_node)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/checkers/base.py", line 1968, in visit_assignname
    if isinstance(utils.safe_infer(assign_type.value), astroid.ClassDef):
  File "/home/gps/tempenv/lib/python3.8/site-packages/pylint/checkers/utils.py", line 1205, in safe_infer
    value = next(infer_gen)
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/node_classes.py", line 360, in infer
    yield from self._explicit_inference(self, context, **kwargs)
  File "/home/gps/tempenv/lib/python3.8/site-packages/wrapt/wrappers.py", line 566, in __call__
    return self._self_wrapper(self.__wrapped__, self._self_instance,
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/__init__.py", line 96, in _inference_tip_cached
    result = func(*args, **kwargs)
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/brain/brain_random.py", line 56, in infer_random_sample
    new_elts = [
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/brain/brain_random.py", line 57, in <listcomp>
    _clone_node_with_lineno(elt, parent=new_node, lineno=new_node.lineno)
  File "/home/gps/tempenv/lib/python3.8/site-packages/astroid/brain/brain_random.py", line 21, in _clone_node_with_lineno
    new_node = cls(**init_params)
TypeError: __init__() got an unexpected keyword argument 'lineno'
```

### Expected behavior
A successful pylint run, probably highlighting that `simple` is undefined.

### ``python -c "from astroid import __pkginfo__; print(__pkginfo__.version)"`` output
2.5.1


If this winds up being a pylint issue, I guess refile it over there.  I filed here based on the bottom the traceback but that doesn't mean the problem originated in astroid.  I haven't spent time looking at the code.


Discussion / hints:
@gpshead thanks for the report.



Fix (patch):
diff --git a/ChangeLog b/ChangeLog
index 9d7b945fde..6097526bbd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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 ee5506cbae..6efd1ff134 100644
--- a/astroid/brain/brain_random.py
+++ b/astroid/brain/brain_random.py
@@ -9,6 +9,8 @@
 
 
 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


Test changes:
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py
index 0e8c591198..26fad2e3bf 100644
--- a/tests/unittest_brain.py
+++ b/tests/unittest_brain.py
@@ -1858,6 +1858,18 @@ def test_inferred_successfully(self):
         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"""