Skip to content

Commit

Permalink
TR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gahjelle committed Sep 27, 2024
1 parent 886bdfb commit c243ce0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
9 changes: 6 additions & 3 deletions python-313/typing/deprecations.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 17 additions & 9 deletions python-313/typing/readonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from typing import NotRequired, ReadOnly, TypedDict

# %% Without ReadOnly

# class Version(TypedDict):
# version: str
# release_year: NotRequired[int | None]
Expand All @@ -21,28 +23,34 @@
# 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']}"
else:
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))

0 comments on commit c243ce0

Please sign in to comment.