-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Changes from all commits
7ecb7ba
c0531ec
479c175
091ceeb
f1c9c77
c8d2534
5df1e97
677a326
1543785
e1ab6ea
18e7fa7
72dd8b9
80bb5fa
fbfd5b1
816098c
8962d26
973e9cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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.