python / python/cpython

Conflict between pragmatic locations and pure spans

Open
#94,758 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

The PEG parser produces locations for an AST that span the entire AST node. This is well defined, and has a number of desirable properties such as the span of a node including all its children's spans.

Unfortunately, this does not produce good locations for tracing and debugging, so we transform some locations in the compiler.
This is problematic as it means that tools producing as AST (e.g. pytest) need to second guess what transformations that the compiler will make.

I proposed moving these transformations in the parser in https://github.com/python/cpython/issues/94694, but as @pablogsal points out, this means that tools which consume the AST (e.g. type checkers) need to undo the transformations to get the original spans back.

The core problem is that there are tools that consume the AST produced by the parser, and tools that produce ASTs for consumption by the compiler, and they want subtly different ASTs.

One possible fix for this is to add a keyword argument to compile() to specify whether it should modify the locations. This would allow pytest, etc. to pass location information through compile() unmodified when needed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading issue 94694 and the parser/compiler handling of AST locations. Compare the needs of AST consumers with tools that produce ASTs for compile(), then determine whether an option to preserve locations is sufficient. Done means the location behavior is defined without requiring tools to guess or undo compiler transformations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.