[issue38166] ast identifies incorrect column for compound attribute lookups

Justin McCann report at bugs.python.org
Fri Sep 13 17:48:54 EDT 2019


New submission from Justin McCann <jneilm at gmail.com>:

This issue is related to https://github.com/microsoft/vscode-python/issues/7327 and https://github.com/PyCQA/pylint/issues/3103

For compound attribute access in variable references like a.b.c.d, the AST reports the column of the first variable/attribute in the sequence instead of the specific attribute location. For example, the location of  c is reported as column 0 (a) and not column 4 (c).

Here's the AST test case provided by a pylint developer in a comment on pylint issue 3103; I confirmed the behavior is the same in python 3.8.0b4.
```
$ python3.8
Python 3.8.0b4 (v3.8.0b4:d93605de72, Aug 29 2019, 21:47:47)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> body = ast.parse('''
... print(x.item.akey > 2)
... ''')
>>> # x.item
... x_item = body.body[0].value.args[0].left.value
>>> print(x_item, x_item.attr, x_item.col_offset)
<_ast.Attribute object at 0x10a7751f0> item 6
>>> # x.item.akey
... print(x_item.value, x_item.value.col_offset) .  ### probably should be 8
<_ast.Name object at 0x10a775280> 6
```

Related issues:
* https://bugs.python.org/issue1440601 Add col information to parse & ast nodes
* https://bugs.python.org/issue10769 ast: provide more useful range information


Here is the resulting confusion when you use this output in pylint (and then VSCode highlights only "x" since it's the variable that starts in column 0):

Original pylint/vscode testcase:
```
class TestMe:
    def __init__(self):
        self.item = {'akey': 42}
        self.again = self

x = TestMe()
### pylint error message here is
###    testme.py:11:6: E1101: Instance of 'dict' has no 'akey' member (no-member)
### The problem is with `x.item`, but pylint shows the column for `x`
print(x.item.akey > 2)

print(x.again.item.doesnotexist)
```

Current behavior
$ pylint testme.py  -rn -sn
************* Module testme
testme.py:10:6: E1101: Instance of 'dict' has no 'akey' member (no-member)
testme.py:12:6: E1101: Instance of 'dict' has no 'doesnotexist' member (no-member)

Expected behavior
$ pylint testme.py  -rn -sn
************* Module testme
testme.py:10:8: E1101: Instance of 'dict' has no 'akey' member (no-member)
testme.py:12:14: E1101: Instance of 'dict' has no 'doesnotexist' member (no-member)

$ pylint --version output
pylint 2.3.1
astroid 2.2.5
Python 3.7.4 (default, Jul 9 2019, 18:13:23)
[Clang 10.0.1 (clang-1001.0.46.4)]

----------
components: Library (Lib)
messages: 352393
nosy: Justin McCann
priority: normal
severity: normal
status: open
title: ast identifies incorrect column for compound attribute lookups
type: behavior
versions: Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38166>
_______________________________________


More information about the Python-bugs-list mailing list