Skip to content

Commit c243ce0

Browse files
committed
TR updates
1 parent 886bdfb commit c243ce0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

python-313/typing/deprecations.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""Demonstration of PEP 702: Marking deprecations using the type system
22
3+
The deprecations should be marked in PyCharm and VS Code.
4+
35
Use PyLance in VS Code by setting Python › Analysis: Type Checking Mode or run
46
the Pyright CLI:
57
6-
$ python -m pip install pyright $ pyright --pythonversion 3.13 .
8+
$ python -m pip install pyright
9+
$ pyright --pythonversion 3.13 .
710
8-
Note that showing warnings requires setting the reportDeprecated option in
9-
Pyright. This is done in pyproject.toml.
11+
Note that showing warnings with Pyright requires setting the reportDeprecated
12+
option. This is done in the accompanying pyproject.toml.
1013
"""
1114

1215
from typing import overload

python-313/typing/readonly.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from typing import NotRequired, ReadOnly, TypedDict
1313

14+
# %% Without ReadOnly
15+
1416
# class Version(TypedDict):
1517
# version: str
1618
# release_year: NotRequired[int | None]
@@ -21,28 +23,34 @@
2123
# release_year: int
2224

2325

26+
# %% Using ReadOnly
27+
#
28+
# Can only use PythonVersion as a Version if the differing fields are ReadOnly
2429
class Version(TypedDict):
25-
version: ReadOnly[str]
30+
version: str
2631
release_year: ReadOnly[NotRequired[int | None]]
2732

33+
# Note that ReadOnly can be nested with other special forms in any order
34+
# release_year: NotRequired[ReadOnly[int | None]]
35+
2836

2937
class PythonVersion(TypedDict):
30-
version: ReadOnly[str]
38+
version: str
3139
release_year: ReadOnly[int]
3240

3341

34-
py313 = PythonVersion(version="3.13", release_year=2024)
35-
36-
# Alternative syntax, using TypedDict as an annotation
37-
# py313: PythonVersion = {"version": "3.13", "release_year": 2024}
38-
39-
42+
# %% Work with Version and PythonVersion
43+
#
4044
def get_version_info(ver: Version) -> str:
4145
if "release_year" in ver:
4246
return f"Version {ver['version']} released in {ver['release_year']}"
4347
else:
4448
return f"Version {ver['version']}"
4549

4650

47-
# Only allowed to use PythonVersion instead of Version if the fields are ReadOnly
51+
py313 = PythonVersion(version="3.13", release_year=2024)
52+
53+
# Alternative syntax, using TypedDict as an annotation
54+
# py313: PythonVersion = {"version": "3.13", "release_year": 2024}
55+
4856
print(get_version_info(py313))

0 commit comments

Comments
 (0)