python / python/cpython

ast.ImportFrom.level is incorrectly marked as optional

Open
#135,959 2 comments 0 reactions 1 assignee View on GitHub

@JelleZijlstra is already working on this.

Since Jun 26, 2025.

interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The level field on ast.ImportFrom is marked optional in the ASDL definition, but in fact the parser always sets it:

>>> ast.dump(ast.parse('from x import a'))
"Module(body=[ImportFrom(module='x', names=[alias(name='a')], level=0)])"
>>> ast.dump(ast.parse('from .x import a'))
"Module(body=[ImportFrom(module='x', names=[alias(name='a')], level=1)])"
>>> ast.dump(ast.parse('from ...............x import a'))
"Module(body=[ImportFrom(module='x', names=[alias(name='a')], level=15)])"

We should mark the field as required for consistency with the parser.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-135960

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.