Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-43224: Implement PEP 646 grammar changes#31018
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7ecb7bac0531ec479c175091ceebf1c9c77c8d25345df1e97677a3261543785e1ab6ea18e7fa772dd8b980bb5fafbfd5b1816098c8962d26973e9ccFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -309,6 +309,8 @@ star_etc[StarEtc*]: | ||
| | invalid_star_etc | ||
| | '*' a=param_no_default b=param_maybe_default* c=[kwds]{ | ||
| _PyPegen_star_etc(p, a, b, c) } | ||
| | '*' a=param_no_default_star_annotation b=param_maybe_default* c=[kwds]{ | ||
| _PyPegen_star_etc(p, a, b, c) } | ||
| | '*' ',' b=param_maybe_default+ c=[kwds]{ | ||
| _PyPegen_star_etc(p, NULL, b, c) } | ||
| | a=kwds{_PyPegen_star_etc(p, NULL, NULL, a) } | ||
| @@ -333,14 +335,19 @@ kwds[arg_ty]: | ||
| param_no_default[arg_ty]: | ||
| | a=param ',' tc=TYPE_COMMENT?{_PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| | a=param tc=TYPE_COMMENT? &')'{_PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| param_no_default_star_annotation[arg_ty]: | ||
| | a=param_star_annotation ',' tc=TYPE_COMMENT?{_PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| | a=param_star_annotation tc=TYPE_COMMENT? &')'{_PyPegen_add_type_comment_to_arg(p, a, tc) } | ||
| param_with_default[NameDefaultPair*]: | ||
| | a=param c=default ',' tc=TYPE_COMMENT?{_PyPegen_name_default_pair(p, a, c, tc) } | ||
| | a=param c=default tc=TYPE_COMMENT? &')'{_PyPegen_name_default_pair(p, a, c, tc) } | ||
| param_maybe_default[NameDefaultPair*]: | ||
| | a=param c=default? ',' tc=TYPE_COMMENT?{_PyPegen_name_default_pair(p, a, c, tc) } | ||
| | a=param c=default? tc=TYPE_COMMENT? &')'{_PyPegen_name_default_pair(p, a, c, tc) } | ||
| param[arg_ty]: a=NAME b=annotation?{_PyAST_arg(a->v.Name.id, b, NULL, EXTRA) } | ||
| param_star_annotation[arg_ty]: a=NAME b=star_annotation{_PyAST_arg(a->v.Name.id, b, NULL, EXTRA) } | ||
| annotation[expr_ty]: ':' a=expression{a } | ||
| star_annotation[expr_ty]: ':' a=star_expression{a } | ||
| default[expr_ty]: '=' a=expression{a } | invalid_default | ||
| # If statement | ||
| @@ -782,7 +789,7 @@ primary[expr_ty]: | ||
| slices[expr_ty]: | ||
| | a=slice !','{a } | ||
| | a[asdl_expr_seq*]=','.slice+ [',']{_PyAST_Tuple(a, Load, EXTRA) } | ||
| | a[asdl_expr_seq*]=','.(slice | starred_expression)+ [',']{_PyAST_Tuple(a, Load, EXTRA) } | ||
mrahtz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| slice[expr_ty]: | ||
| | a=[expression] ':' b=[expression] c=[':' d=[expression]{d }]{_PyAST_Slice(a, b, c, EXTRA) } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -175,7 +175,7 @@ def _exec_future(self, code): | ||
| scope ={} | ||
| exec( | ||
| "from __future__ import annotations\n" | ||
| + code, {}, scope | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this because if both | ||
| + code, scope | ||
| ) | ||
| return scope | ||
| @@ -287,10 +287,11 @@ def test_annotations(self): | ||
| eq("list[str]") | ||
| eq("dict[str, int]") | ||
| eq("set[str,]") | ||
| eq("tuple[()]") | ||
| eq("tuple[str, ...]") | ||
| eq("tuple[(str, *types)]") | ||
| eq("tuple[str, *types]") | ||
| eq("tuple[str, int, (str, int)]") | ||
| eq("tuple[(*int, str, str, (str, int))]") | ||
| eq("tuple[*int, str, str, (str, int)]") | ||
| eq("tuple[str, int, float, dict[str, int]]") | ||
| eq("slice[0]") | ||
| eq("slice[0:1]") | ||
| @@ -305,6 +306,21 @@ def test_annotations(self): | ||
| eq("slice[1:2, 1]") | ||
| eq("slice[1:2, 2, 3]") | ||
| eq("slice[()]") | ||
| # Note that `slice[*Ts]`, `slice[*Ts,]`, and `slice[(*Ts,)]` all have | ||
| # the same AST, but only `slice[*Ts,]` passes this test, because that's | ||
| # what the unparser produces. | ||
| eq("slice[*Ts,]") | ||
| eq("slice[1, *Ts]") | ||
| eq("slice[*Ts, 2]") | ||
| eq("slice[1, *Ts, 2]") | ||
| eq("slice[*Ts, *Ts]") | ||
| eq("slice[1, *Ts, *Ts]") | ||
| eq("slice[*Ts, 1, *Ts]") | ||
| eq("slice[*Ts, *Ts, 1]") | ||
| eq("slice[1, *Ts, *Ts, 2]") | ||
| eq("slice[1:2, *Ts]") | ||
| eq("slice[*Ts, 1:2]") | ||
| eq("slice[1:2, *Ts, 3:4]") | ||
| eq("slice[a, b:c, d:e:f]") | ||
| eq("slice[(x for x in a)]") | ||
| eq('str or None if sys.version_info[0] > (3,) else str or bytes or None') | ||
| @@ -403,6 +419,25 @@ def foo(): | ||
| def bar(arg: (yield)): pass | ||
| """)) | ||
| def test_get_type_hints_on_func_with_variadic_arg(self): | ||
| # `typing.get_type_hints` might break on a function with a variadic | ||
| # annotation (e.g. `f(*args: *Ts)`) if `from __future__ import | ||
| # annotations`, because it could try to evaluate `*Ts` as an expression, | ||
| # which on its own isn't value syntax. | ||
| namespace = self._exec_future(dedent("""\ | ||
| class StarredC: pass | ||
| class C: | ||
| def __iter__(self): | ||
| yield StarredC() | ||
| c = C() | ||
| def f(*args: *c): pass | ||
| import typing | ||
| hints = typing.get_type_hints(f) | ||
| """)) | ||
| hints = namespace.pop('hints') | ||
| self.assertIsInstance(hints['args'], namespace['StarredC']) | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.