Skip to content
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

Update ast for 3.12 #10201

Merged
merged 5 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions stdlib/_ast.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys
import typing_extensions
from typing import Any, ClassVar
from typing_extensions import Literal, TypeAlias
from typing_extensions import Literal

PyCF_ONLY_AST: Literal[1024]
if sys.version_info >= (3, 8):
PyCF_TYPE_COMMENTS: Literal[4096]
PyCF_ALLOW_TOP_LEVEL_AWAIT: Literal[8192]

_Identifier: TypeAlias = str
_Identifier: typing_extensions.TypeAlias = str

class AST:
if sys.version_info >= (3, 10):
Expand Down Expand Up @@ -59,31 +60,43 @@ class Expression(mod):
class stmt(AST): ...

class FunctionDef(stmt):
if sys.version_info >= (3, 10):
if sys.version_info >= (3, 12):
__match_args__ = ("name", "args", "body", "decorator_list", "returns", "type_comment", "type_params")
elif sys.version_info >= (3, 10):
__match_args__ = ("name", "args", "body", "decorator_list", "returns", "type_comment")
name: _Identifier
args: arguments
body: list[stmt]
decorator_list: list[expr]
returns: expr | None
if sys.version_info >= (3, 12):
type_params: list[type_param]

class AsyncFunctionDef(stmt):
if sys.version_info >= (3, 10):
if sys.version_info >= (3, 12):
__match_args__ = ("name", "args", "body", "decorator_list", "returns", "type_comment", "type_params")
elif sys.version_info >= (3, 10):
__match_args__ = ("name", "args", "body", "decorator_list", "returns", "type_comment")
name: _Identifier
args: arguments
body: list[stmt]
decorator_list: list[expr]
returns: expr | None
if sys.version_info >= (3, 12):
type_params: list[type_param]

class ClassDef(stmt):
if sys.version_info >= (3, 10):
if sys.version_info >= (3, 12):
__match_args__ = ("name", "bases", "keywords", "body", "decorator_list", "type_params")
elif sys.version_info >= (3, 10):
__match_args__ = ("name", "bases", "keywords", "body", "decorator_list")
name: _Identifier
bases: list[expr]
keywords: list[keyword]
body: list[stmt]
decorator_list: list[expr]
if sys.version_info >= (3, 12):
type_params: list[type_param]

class Return(stmt):
if sys.version_info >= (3, 10):
Expand Down Expand Up @@ -366,10 +379,10 @@ class Attribute(expr):
ctx: expr_context

if sys.version_info >= (3, 9):
_Slice: TypeAlias = expr
_Slice: typing_extensions.TypeAlias = expr
else:
class slice(AST): ...
_Slice: TypeAlias = slice
_Slice: typing_extensions.TypeAlias = slice

class Slice(_Slice):
if sys.version_info >= (3, 10):
Expand Down Expand Up @@ -526,7 +539,7 @@ if sys.version_info >= (3, 10):

class pattern(AST): ...
# Without the alias, Pyright complains variables named pattern are recursively defined
_Pattern: TypeAlias = pattern
_Pattern: typing_extensions.TypeAlias = pattern

class match_case(AST):
__match_args__ = ("pattern", "guard", "body")
Expand Down Expand Up @@ -571,3 +584,25 @@ if sys.version_info >= (3, 10):
class MatchOr(pattern):
__match_args__ = ("patterns",)
patterns: list[pattern]

if sys.version_info >= (3, 12):
class type_param(AST): ...

class TypeVar(type_param):
__match_args__ = ("name", "bound")
name: _Identifier
bound: expr | None

class ParamSpec(type_param):
__match_args__ = ("name",)
name: _Identifier

class TypeVarTuple(type_param):
__match_args__ = ("name",)
name: _Identifier

class TypeAlias(stmt):
__match_args__ = ("name", "typeparams", "value")
name: Name
type_params: list[type_param]
value: expr
4 changes: 2 additions & 2 deletions stdlib/ast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sys
from _ast import *
from _typeshed import ReadableBuffer, Unused
from collections.abc import Iterator
from typing import Any, TypeVar, overload
from typing import Any, TypeVar as _TypeVar, overload
from typing_extensions import Literal

if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -168,7 +168,7 @@ class NodeTransformer(NodeVisitor):
# The usual return type is AST | None, but Iterable[AST]
# is also allowed in some cases -- this needs to be mapped.

_T = TypeVar("_T", bound=AST)
_T = _TypeVar("_T", bound=AST)
Copy link
Member

Choose a reason for hiding this comment

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

Does pytype like it any more if we do _T = typing.TypeVar("_T")? (FWIW that would also be friendlier to flake8-pyi, which will recognise typing.TypeVar("_T") as a TypeVar declaration, but won't recognise _TypeVar("_T") as a TypeVar declaration.)


if sys.version_info >= (3, 8):
@overload
Expand Down