Skip to content

Fix py2pyi on Python 3.14 (ast.Str and ast.Ellipsis were removed) - #953

Open
maoxor wants to merge 1 commit into
AnswerDotAI:mainfrom
maoxor:py2pyi-python-3.14
Open

maoxor wants to merge 1 commit into
AnswerDotAI:mainfrom
maoxor:py2pyi-python-3.14

Conversation

@maoxor

@maoxor maoxor commented Oct 29, 2026 •

Copy link
Copy Markdown

Python 3.14 removed the deprecated ast.Str and ast.Ellipsis aliases, and py2pyi still uses them in _is_empty_class and _body_ellip. As a result, py2pyi fails on 3.14 for any module with a function docstring:

  File ".../fastcore/py2pyi.py", line 147, in _body_ellip
    stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
AttributeError: module 'ast' has no attribute 'Str'

This checks for ast.Constant with a str (or ...) value instead. That's exactly what the aliases' isinstance matched on 3.8 to 3.13. The change is in the notebook, and fastcore/py2pyi.py is regenerated with nbdev-export; only those two lines change. I also removed the cell outputs that contained only the ast.Str is deprecated / ast.Ellipsis is deprecated warnings, since they no longer happen.

Tests: the existing cells in 10_py2pyi.ipynb already catch this, but only on 3.14, which CI doesn't run (3.10 to 3.13).

  • nbdev-test nbs/10_py2pyi.ipynb on 3.14: fails on main with AttributeError: module 'ast' has no attribute 'Ellipsis', and passes with this change. On 3.13 it passes both ways. 06_script.ipynb, which mentions py2pyi, passes on 3.14 too.
  • On 3.13, where the aliases still exist, I compared old and new checks on 14 nodes: str, ..., int, None, bytes and bool constants, a Name, an f-string, a call, and the same parsed from source. They agree on all of them.
  • End to end, I ran py2pyi on a small module with a docstring-only class, a class X: ..., a class with a documented method and a @patched method. The .pyi from this branch on 3.13 and 3.14 is byte-identical to main's on 3.13.

Python 3.14 removed the deprecated ast.Str and ast.Ellipsis aliases, so
py2pyi raised AttributeError for any function with a docstring. Check
for ast.Constant with a str or Ellipsis value instead, which is what
the aliases matched. Also drop the stale DeprecationWarning outputs
from the notebook.
@maoxor
maoxor force-pushed the py2pyi-python-3.14 branch from 8a9b6ef to 1789913 Compare October 29, 2026 16:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant