diff --git a/python-313/typing/deprecations.py b/python-313/typing/deprecations.py index 9f8c3c2e4c..7fe8a8fa99 100644 --- a/python-313/typing/deprecations.py +++ b/python-313/typing/deprecations.py @@ -1,12 +1,15 @@ """Demonstration of PEP 702: Marking deprecations using the type system +The deprecations should be marked in PyCharm and VS Code. + Use PyLance in VS Code by setting Python › Analysis: Type Checking Mode or run the Pyright CLI: - $ python -m pip install pyright $ pyright --pythonversion 3.13 . + $ python -m pip install pyright + $ pyright --pythonversion 3.13 . -Note that showing warnings requires setting the reportDeprecated option in -Pyright. This is done in pyproject.toml. +Note that showing warnings with Pyright requires setting the reportDeprecated +option. This is done in the accompanying pyproject.toml. """ from typing import overload diff --git a/python-313/typing/readonly.py b/python-313/typing/readonly.py index 5e4e886f1f..05ed0f92f2 100644 --- a/python-313/typing/readonly.py +++ b/python-313/typing/readonly.py @@ -11,6 +11,8 @@ from typing import NotRequired, ReadOnly, TypedDict +# %% Without ReadOnly + # class Version(TypedDict): # version: str # release_year: NotRequired[int | None] @@ -21,22 +23,24 @@ # release_year: int +# %% Using ReadOnly +# +# Can only use PythonVersion as a Version if the differing fields are ReadOnly class Version(TypedDict): - version: ReadOnly[str] + version: str release_year: ReadOnly[NotRequired[int | None]] + # Note that ReadOnly can be nested with other special forms in any order + # release_year: NotRequired[ReadOnly[int | None]] + class PythonVersion(TypedDict): - version: ReadOnly[str] + version: str release_year: ReadOnly[int] -py313 = PythonVersion(version="3.13", release_year=2024) - -# Alternative syntax, using TypedDict as an annotation -# py313: PythonVersion = {"version": "3.13", "release_year": 2024} - - +# %% Work with Version and PythonVersion +# def get_version_info(ver: Version) -> str: if "release_year" in ver: return f"Version {ver['version']} released in {ver['release_year']}" @@ -44,5 +48,9 @@ def get_version_info(ver: Version) -> str: return f"Version {ver['version']}" -# Only allowed to use PythonVersion instead of Version if the fields are ReadOnly +py313 = PythonVersion(version="3.13", release_year=2024) + +# Alternative syntax, using TypedDict as an annotation +# py313: PythonVersion = {"version": "3.13", "release_year": 2024} + print(get_version_info(py313))