Skip to content

Track column numbers (colno) in AST nodes#2134

Closed
daniel7an wants to merge 1 commit intopallets:mainfrom
daniel7an:add-colno-tracking
Closed

Track column numbers (colno) in AST nodes#2134
daniel7an wants to merge 1 commit intopallets:mainfrom
daniel7an:add-colno-tracking

Conversation

@daniel7an
Copy link
Copy Markdown

Add column number (colno) tracking to AST nodes, complementing the existing lineno attribute. This enables more precise source location reporting for static analysis tools.

Closes #2133

Changes:

  • Add colno field to Token namedtuple (default 0, backward compatible)
  • Track column position in tokeniter relative to line start
  • Add colno to Node attributes alongside lineno and environment
  • Add Node.set_colno() method mirroring set_lineno()
  • Propagate colno from tokens to nodes throughout the parser
  • Update ext.py token unpacking for compatibility

Example:

from jinja2 import Environment, nodes
env = Environment()
ast = env.parse('hello {{ foo }} world {{ bar + baz }}')
for name in ast.find_all(nodes.Name):
    print(f'{name.name}: line={name.lineno}, col={name.colno}')
# foo: line=1, col=10
# bar: line=1, col=26  
# baz: line=1, col=32

Backward compatible: Token.colno defaults to 0, Node.colno defaults to None. All 911 existing tests pass. Added 12 new tests.

Add column number tracking to AST nodes, complementing the existing
lineno attribute. This enables more precise source location reporting
for static analysis tools working with Jinja templates.

Changes:
- Add colno field to Token namedtuple (default 0, backward compatible)
- Track column position in tokeniter based on source position
- Add colno attribute to Node base class
- Add Node.set_colno() method mirroring set_lineno()
- Propagate colno from tokens to nodes throughout the parser
- Update ext.py token unpacking for compatibility
- Add comprehensive tests for column number tracking

Closes #2133
@davidism davidism closed this Feb 9, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking colno in AST nodes

2 participants