Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
The following code is completely valid on Python 3.8:
fromcontextlibimportnullcontextwith (nullcontext()ifbool()elsenullcontext()):pass
However, following0daba82 (which was backported to Python 3.11 and Python 3.10),ast.parse() incorrectly throws an error if you try to parse this code withfeature_version=(3, 8):
% python Python 3.10.6 (main, Feb 24 2024, 10:35:05) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>>import ast>>> source="""...with (... nullcontext()ifbool()else nullcontext()... ):...pass...""">>> ast.parse(source,feature_version=(3,8))Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/alexw/.pyenv/versions/3.10.6/lib/python3.10/ast.py", line 50, in parse return compile(source, filename, mode, flags, File "<unknown>", line 5 pass ^SyntaxError: Parenthesized context managers are only supported in Python 3.9 and greater
CPython versions tested on:
3.8, 3.9, 3.10, 3.11, 3.12
Operating systems tested on:
macOS
Linked PRs
- gh-115881: Ensure
ast.parse()parses conditional context managers even with lowfeature_versionpassed #115920 - [3.12] gh-115881: Ensure
ast.parse()parses conditional context managers even with lowfeature_versionpassed (#115920) #115959 - [3.11] gh-115881: Ensure
ast.parse()parses conditional context managers even with lowfeature_versionpassed (#115920) #115960 - gh-115881: Document feature_version limitations #115980
- [3.12] gh-115881: Document feature_version limitations (#115980) #116173
- [3.11] gh-115881: Document feature_version limitations (#115980) #116174