Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
alicederyn committed Oct 21, 2023
1 parent 0a7bdc4 commit d304a08
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions peps/pep-0705.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ The ``movie_string`` function in the first motivating example can then be typed
else:
return f'{movie["name"]} ({movie["year"]})'

A mixture of read-only and non-read-only items is permitted, allowing the second motivating example to be correctly type-hinted::
A mixture of read-only and non-read-only items is permitted, allowing the second motivating example to be correctly annotated::

class HasTimestamp(TypedDict):
timestamp: float
Expand Down Expand Up @@ -183,7 +183,7 @@ The ``typing.ReadOnly`` type qualifier is used to indicate that an item declared
name: str
members: ReadOnly[list[str]]

blur: Band = {"name": "blur", members: []}
blur: Band = {"name": "blur", "members": []}
blur["name"] = "Blur" # OK: "name" is not read-only
blur["members"] = ["Damon Albarn"] # Type check error: "members" is read-only
blur["members"].append("Damon Albarn") # OK: list is mutable
Expand Down Expand Up @@ -226,7 +226,7 @@ Subclasses can redeclare read-only items as non-read-only, allowing them to be m
name: str
year: int

album: Album = { name: "Flood", year: 1990 }
album: Album = { "name": "Flood", "year": 1990 }
album["year"] = 1973
album["name"] = "Dark Side Of The Moon" # OK: "name" is not read-only in Album

Expand All @@ -235,7 +235,7 @@ If a read-only item is not redeclared, it remains read-only::
class Album(NamedDict):
year: int

album: Album = { name: "Flood", year: 1990 }
album: Album = { "name": "Flood", "year": 1990 }
album["name"] = "Dark Side Of The Moon" # Type check error: "name" is read-only in Album

Subclasses can narrow value types of read-only items::
Expand Down Expand Up @@ -282,7 +282,7 @@ A TypedDict type ``A`` is consistent with TypedDict ``B`` if ``A`` is structural

Discussion:

* All non-specified items in a TypedDict implicitly have value type ``ReadOnly[NotRequired[Any]]`` (or ``ReadOnly[NotRequired[Unknown]]`` in pyright).
* All non-specified items in a TypedDict implicitly have value type ``ReadOnly[NotRequired[object]]``.

* Read-only items behave covariantly, as they cannot be mutated. This is similar to container types such as ``Sequence``, and different from non-read-only items, which behave invariantly. Example::

Expand All @@ -298,7 +298,7 @@ Discussion:
b: B = {"x": 1}
f(b) # Accepted by type checker

* A TypedDict type ``A`` with no explicit key ``'x'`` is not consistent with a TypedDict type ``B`` with a non-required key ``'x'``, since at runtime the key ``'x'`` could be present and have an incompatible type (which may not be visible through ``A`` due to structural subtyping). The only exception to this rule is if the item in ``B`` is read-only, and the value type is a top type (``object``, ``Any`` or pylance's ``Unknown``). For example::
* A TypedDict type ``A`` with no explicit key ``'x'`` is not consistent with a TypedDict type ``B`` with a non-required key ``'x'``, since at runtime the key ``'x'`` could be present and have an incompatible type (which may not be visible through ``A`` due to structural subtyping). The only exception to this rule is if the item in ``B`` is read-only, and the value type is of top type (``object``). For example::

class A(TypedDict):
x: int
Expand Down Expand Up @@ -330,7 +330,7 @@ The merge operation (``d1 | d2``) creates a new dictionary with the merged keys

Type checkers should conform to the following rules, even if they are not expressed correctly in the typeshed due to language constraints.

If the merger of two TypedDict objects of type ``A`` and ``B`` are assigned to a TypedDict of type ``C``, the type checker should verify that:
If the merger of two TypedDict objects of type ``A`` and ``B`` is assigned to a TypedDict of type ``C``, the type checker should verify that:

* for each key in ``C``, one of the following holds:

Expand Down Expand Up @@ -405,7 +405,7 @@ If a shallow copy of TypedDict type ``A`` is assigned to TypedDict type ``B``, t

* for each key in ``B``, one of the following holds:

* the associated value type in of top type
* the associated value type is of top type
* the key is in ``A``, and the associated value type in ``A`` is consistent with the value type in ``B``

* if a key is required in ``B``, it is required in ``A``
Expand All @@ -414,7 +414,7 @@ If a deep copy of TypedDict type ``A`` is assigned to TypedDict type ``B``, the

* for each key in ``B``, one of the following holds:

* the associated value type in of top type
* the associated value type is of top type
* the key is in ``A``, and a deep copy of the associated value type in ``A`` could be assigned to the value type in ``B``

* if a key is required in ``B``, it is required in ``A``
Expand Down

0 comments on commit d304a08

Please sign in to comment.