classification
Title: Add position metadata to alias AST type
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, The Compiler, blueyed, gvanrossum, hauntsaninja, lys.nikolaou, matthew.suozzo, pablogsal
Priority: normal Keywords: patch

Created on 2021-04-09 20:28 by matthew.suozzo, last changed 2021-04-14 15:03 by The Compiler. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25324 merged matthew.suozzo, 2021-04-09 22:31
Messages (9)
msg390663 - (view) Author: Matthew Suozzo (matthew.suozzo) * Date: 2021-04-09 20:28
Given the increasing use of long `from typing import foo, bar, ...` import sequences, it's becoming more desirable to address individual components of the import node. Unfortunately, the ast.alias node doesn't contain source location metadata (e.g. lineno, col_offset).

This metadata would be comparatively easy to add, backwards compatible, and would enable more precise diagnostics e.g. lints.
msg390673 - (view) Author: Matthew Suozzo (matthew.suozzo) * Date: 2021-04-09 22:39
Ah and one other question: Is this normally the sort of thing that would get backported? It should be very straightforward to do so, at least for 3.9 given the support for the new parser.
msg390675 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2021-04-09 22:58
I'm okay with this, unless someone has any input on why the alias node should not have line/column info (and did not have it in the first place).
msg390677 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-09 23:26
> Ah and one other question: Is this normally the sort of thing that would get backported? It should be very straightforward to do so, at least for 3.9 given the support for the new parser.

Unfortunately no, is technically a new feature.
msg390740 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2021-04-10 20:56
New changeset 75a06f067bd0a2687312e5f8e78f9075be76ad3a by Matthew Suozzo in branch 'master':
bpo-43798: Add source location attributes to alias (GH-25324)
https://github.com/python/cpython/commit/75a06f067bd0a2687312e5f8e78f9075be76ad3a
msg390800 - (view) Author: Shantanu (hauntsaninja) * Date: 2021-04-11 22:15
FYI, probably unavoidable, but this appears to have broken pytest https://github.com/pytest-dev/pytest/issues/8539
msg390807 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-11 23:01
> FYI, probably unavoidable, but this appears to have broken pytest https://github.com/pytest-dev/pytest/issues/8539

What's the problem? alias objects *have* lineno argument:

>>> print(ast.dump(ast.parse("from x import y"), include_attributes=True, indent=4))
Module(
    body=[
        ImportFrom(
            module='x',
            names=[
                alias(
                    name='y',
                    lineno=1,
                    col_offset=14,
                    end_lineno=1,
                    end_col_offset=15)],
            level=0,
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=15)],
    type_ignores=[])
msg390901 - (view) Author: daniel hahler (blueyed) * Date: 2021-04-12 20:31
@Pablo: the problem (referenced in the pytest issue) is that `ast.alias` has new required arguments now (see also the adjusted test in
https://github.com/python/cpython/commit/75a06f067bd0a2687312e5f8e78f9075be76ad3a#diff-3f516b60719dd445d33225e4f316b36e85c9c51a843a0147349d11a005c55937R1060-R1061).  That's expected/wanted though (assuming that it should not have "defaults" for B/C), and there's a patch for the issue at https://github.com/pytest-dev/pytest/pull/8540.
msg390902 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-12 20:39
Ah, it was not clear from the issue that pytest was constructing nodes by hand. It makes sense now. Thanks!
History
Date User Action Args
2021-04-14 15:03:48The Compilersetnosy: + The Compiler
2021-04-12 20:39:42pablogsalsetmessages: + msg390902
2021-04-12 20:32:47blueyedsetversions: - Python 3.9
2021-04-12 20:31:54blueyedsetnosy: + blueyed
messages: + msg390901
2021-04-11 23:01:03pablogsalsetmessages: + msg390807
2021-04-11 22:15:11hauntsaninjasetnosy: + hauntsaninja
messages: + msg390800
2021-04-10 20:57:20lys.nikolaousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-04-10 20:56:34lys.nikolaousetmessages: + msg390740
2021-04-09 23:26:04pablogsalsetmessages: + msg390677
2021-04-09 22:58:28lys.nikolaousetnosy: + gvanrossum, lys.nikolaou, pablogsal, BTaskaya
messages: + msg390675
2021-04-09 22:39:10matthew.suozzosetmessages: + msg390673
versions: - Python 3.6, Python 3.7, Python 3.8
2021-04-09 22:31:24matthew.suozzosetkeywords: + patch
stage: patch review
pull_requests: + pull_request24059
2021-04-09 20:28:56matthew.suozzocreate