Skip to content

Commit bbe48c7

Browse files
authored
Support Module and other objects in ast.parse (#14837)
1 parent e9d5cc7 commit bbe48c7

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ast
2+
from typing_extensions import assert_type
3+
4+
# Test with source code strings
5+
assert_type(ast.parse("x = 1"), ast.Module)
6+
assert_type(ast.parse("x = 1", mode="exec"), ast.Module)
7+
assert_type(ast.parse("1 + 1", mode="eval"), ast.Expression)
8+
assert_type(ast.parse("x = 1", mode="single"), ast.Interactive)
9+
assert_type(ast.parse("(int, str) -> None", mode="func_type"), ast.FunctionType)
10+
11+
# Test with mod objects - Module
12+
mod1: ast.Module = ast.Module([], [])
13+
assert_type(ast.parse(mod1), ast.Module)
14+
assert_type(ast.parse(mod1, mode="exec"), ast.Module)
15+
mod2: ast.Module = ast.Module(body=[ast.Expr(value=ast.Constant(value=42))], type_ignores=[])
16+
assert_type(ast.parse(mod2), ast.Module)
17+
18+
# Test with mod objects - Expression
19+
expr1: ast.Expression = ast.Expression(body=ast.Constant(value=42))
20+
assert_type(ast.parse(expr1, mode="eval"), ast.Expression)
21+
22+
# Test with mod objects - Interactive
23+
inter1: ast.Interactive = ast.Interactive(body=[])
24+
assert_type(ast.parse(inter1, mode="single"), ast.Interactive)
25+
26+
# Test with mod objects - FunctionType
27+
func1: ast.FunctionType = ast.FunctionType(argtypes=[], returns=ast.Constant(value=None))
28+
assert_type(ast.parse(func1, mode="func_type"), ast.FunctionType)
29+
30+
# Test that any AST node can be passed and returns the same type
31+
binop: ast.BinOp = ast.BinOp(left=ast.Constant(1), op=ast.Add(), right=ast.Constant(2))
32+
assert_type(ast.parse(binop), ast.BinOp)
33+
34+
constant: ast.Constant = ast.Constant(value=42)
35+
assert_type(ast.parse(constant), ast.Constant)
36+
37+
expr_stmt: ast.Expr = ast.Expr(value=ast.Constant(value=42))
38+
assert_type(ast.parse(expr_stmt), ast.Expr)
39+
40+
# Test with additional parameters
41+
assert_type(ast.parse(mod1, filename="test.py"), ast.Module)
42+
assert_type(ast.parse(mod1, type_comments=True), ast.Module)
43+
assert_type(ast.parse(mod1, feature_version=(3, 10)), ast.Module)
44+
assert_type(ast.parse(binop, filename="test.py"), ast.BinOp)

stdlib/ast.pyi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,16 @@ if sys.version_info < (3, 14):
17441744
_T = _TypeVar("_T", bound=AST)
17451745

17461746
if sys.version_info >= (3, 13):
1747+
@overload
1748+
def parse(
1749+
source: _T,
1750+
filename: str | ReadableBuffer | os.PathLike[Any] = "<unknown>",
1751+
mode: Literal["exec", "eval", "func_type", "single"] = "exec",
1752+
*,
1753+
type_comments: bool = False,
1754+
feature_version: None | int | tuple[int, int] = None,
1755+
optimize: Literal[-1, 0, 1, 2] = -1,
1756+
) -> _T: ...
17471757
@overload
17481758
def parse(
17491759
source: str | ReadableBuffer,
@@ -1823,6 +1833,15 @@ if sys.version_info >= (3, 13):
18231833
) -> mod: ...
18241834

18251835
else:
1836+
@overload
1837+
def parse(
1838+
source: _T,
1839+
filename: str | ReadableBuffer | os.PathLike[Any] = "<unknown>",
1840+
mode: Literal["exec", "eval", "func_type", "single"] = "exec",
1841+
*,
1842+
type_comments: bool = False,
1843+
feature_version: None | int | tuple[int, int] = None,
1844+
) -> _T: ...
18261845
@overload
18271846
def parse(
18281847
source: str | ReadableBuffer,

0 commit comments

Comments
 (0)