Skip to content

builtins: do the TODO on compile() #9567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1220,27 +1220,90 @@ if sys.version_info >= (3, 10):
@overload
async def anext(__i: SupportsAnext[_T], default: _VT) -> _T | _VT: ...

# TODO: `compile` has a more precise return type in reality; work on a way of expressing that?
# compile() returns a CodeType, unless the flags argument includes PyCF_ONLY_AST (=1024),
# in which case it returns ast.AST. We have overloads for flag 0 (the default) and for
# explicitly passing PyCF_ONLY_AST. We fall back to Any for other values of flags.
if sys.version_info >= (3, 8):
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: int = 0,
flags: Literal[0],
dont_inherit: int = False,
optimize: int = -1,
*,
_feature_version: int = -1,
) -> CodeType: ...
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
*,
dont_inherit: int = False,
optimize: int = -1,
_feature_version: int = -1,
) -> CodeType: ...
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: Literal[1024],
dont_inherit: int = False,
optimize: int = -1,
*,
_feature_version: int = -1,
) -> _ast.AST: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can potentially be more precise here: if mode is "eval", the node must be an _ast.expr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we have to duplicate all 8 overloads from ast.parse, or does compile only take "exec", "eval" or "single" for the mode parameter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we would. ast.parse just calls compile(). I think it's fine to stick with AST for simplicity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I think we'd only need four overloads because the second argument to compile() is required, unlike ast.parse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Four overloads isn't that bad? But I don't have a strong opinion either way.

@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: int,
dont_inherit: int = False,
optimize: int = -1,
*,
_feature_version: int = -1,
) -> Any: ...

else:
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: Literal[0],
dont_inherit: int = False,
optimize: int = -1,
) -> CodeType: ...
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
*,
dont_inherit: int = False,
optimize: int = -1,
) -> CodeType: ...
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: int = ...,
dont_inherit: int = ...,
optimize: int = ...,
flags: Literal[1024],
dont_inherit: int = False,
optimize: int = -1,
) -> _ast.AST: ...
@overload
def compile(
source: str | ReadableBuffer | _ast.Module | _ast.Expression | _ast.Interactive,
filename: str | ReadableBuffer | _PathLike[Any],
mode: str,
flags: int,
dont_inherit: int = False,
optimize: int = -1,
) -> Any: ...

def copyright() -> None: ...
Expand Down