-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path3.11.py
More file actions
executable file
·61 lines (45 loc) · 1.42 KB
/
3.11.py
File metadata and controls
executable file
·61 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3.11
# https://docs.python.org/3.11/whatsnew/3.11.html
from passed import passed
import asyncio
import enum
import tomllib
# ExceptionGroup and except* (PEP 654)
def _raise_exception_group() -> None:
raise ExceptionGroup(
"demo",
[ValueError("bad value"), TypeError("bad type")],
)
handled_value_error = False
handled_type_error = False
try:
_raise_exception_group()
except* ValueError as eg:
handled_value_error = True
# Each subgroup only contains the matched exception type
assert all(isinstance(e, ValueError) for e in eg.exceptions)
except* TypeError as eg:
handled_type_error = True
assert all(isinstance(e, TypeError) for e in eg.exceptions)
assert handled_value_error and handled_type_error
# tomllib in the standard library (PEP 680)
toml_text = """
name = "alice"
age = 42
"""
data = tomllib.loads(toml_text)
assert data == {"name": "alice", "age": 42}
# enum.StrEnum (bakes in str behavior)
class Color(enum.StrEnum):
RED = "red"
BLUE = "blue"
assert Color.RED == "red"
assert isinstance(Color.BLUE, str)
# asyncio.TaskGroup (structured concurrency)
async def _tg_demo() -> int:
async with asyncio.TaskGroup() as tg:
task_one = tg.create_task(asyncio.sleep(0.01, result=1))
task_two = tg.create_task(asyncio.sleep(0.01, result=2))
return task_one.result() + task_two.result()
assert asyncio.run(_tg_demo()) == 3
passed()