Skip to content

CallContext assertion failed in the wild #9214

@martin-belanger

Description

@martin-belanger

I wrote a simple mock module (below) to show the problem. The code executes fine with Python 3.10. However, pylint throws an exception (log below) when parsing it. I updated pylint today from 2.17.5 to 3.0.2. That's when it started to fail.

Note: This was run on Ubuntu 22.04 with Python 3.10.

Pylint version that was working OK:

$ pylint --version
pylint 2.17.5
astroid 2.15.6

Pylint version that is not working:

$ pylint --version
pylint 3.0.2
astroid 3.0.1

Mock module:

'''Test module'''
import functools

class MockClass():
    '''Mock class'''
    def __init__(self, enabled=True):
        self._options = {
            'Global': {
                'Enabled': enabled,
            }
        }

    def _get_option(self, section, option):
        '''Mock getter'''
        return self._options[section][option]

    enabled = property(functools.partial(_get_option, section='Global', option='Enabled'))

    @property
    def supported_types(self):
        '''Get supported types'''
        return ['tcp', 'udp'] if self.enabled else list()


if __name__ == '__main__':
    obj = MockClass()

    print(f'enabled={obj.enabled}')
    print(f'supported_types={obj.supported_types}')

This is pylint's output:

pylint --rcfile=/home/mbelanger/work/linux-nvme/nvme-stas/test/pylint.rc ./property.py 
Exception on node <FunctionDef.supported_types l.20 at 0x7f67dae01ae0> in file '/tmp/property.py'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/astroid/bases.py", line 245, in getattr
    values = self._proxied.instance_attr(name, context)
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2353, in instance_attr
    raise AttributeInferenceError(target=self, attribute=name, context=context)
astroid.exceptions.AttributeInferenceError: 'enabled' not found on <ClassDef.MockClass l.4 at 0x7f67dae028f0>.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/astroid/bases.py", line 278, in igetattr
    get_attr = self.getattr(name, context, lookupclass=False)
  File "/usr/local/lib/python3.10/dist-packages/astroid/bases.py", line 255, in getattr
    raise AttributeInferenceError(
astroid.exceptions.AttributeInferenceError: 'enabled' not found on <Instance of property.MockClass at 0x140084026905104>.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/usr/local/lib/python3.10/dist-packages/pylint/checkers/classes/special_methods_checker.py", line 183, in visit_functiondef
    inferred = _safe_infer_call_result(node, node)
  File "/usr/local/lib/python3.10/dist-packages/pylint/checkers/classes/special_methods_checker.py", line 42, in _safe_infer_call_result
    value = next(inferit)
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 1650, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/usr/local/lib/python3.10/dist-packages/astroid/decorators.py", line 90, in inner
    yield next(generator)
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/node_classes.py", line 3108, in _infer
    test = next(self.test.infer(context=context.clone()))
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/usr/local/lib/python3.10/dist-packages/astroid/decorators.py", line 90, in inner
    yield next(generator)
  File "/usr/local/lib/python3.10/dist-packages/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/node_classes.py", line 1107, in _infer_attribute
    yield from owner.igetattr(node.attrname, context)
  File "/usr/local/lib/python3.10/dist-packages/astroid/bases.py", line 290, in igetattr
    yield from self._wrap_attr(attrs, context)
  File "/usr/local/lib/python3.10/dist-packages/astroid/bases.py", line 298, in _wrap_attr
    for attr in attrs:
  File "/usr/local/lib/python3.10/dist-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2520, in igetattr
    yield from function.infer_call_result(
  File "/usr/local/lib/python3.10/dist-packages/astroid/objects.py", line 317, in infer_call_result
    assert (
AssertionError: CallContext should be set before inferring call result
************* Module property
property.py:1:0: F0002: property.py: Fatal error while checking 'property.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/home/mbelanger/.cache/pylint/pylint-crash-2023-11-02-13-05-18.txt'. (astroid-error)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Metadata

Metadata

Labels

AstroidRelated to astroidCrash 💥A bug that makes pylint crash

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions