Skip to content

Commit d4263de

Browse files
spec: various minor changes (#2013)
- Replace an incorrect use of "subtype" with "assignable" - Remove discussion of str/bytes Literal types that seems geared towards Python 2. Instead, say straightforwardly that strings (str objects) and bytes objects are supported. - Rewrite an example that relies on the int/float/complex special case. The example was correct but I think it's confusing to readers if we have examples unnecessarily rely on the special case. Instead, use example types with more straightforward behavior.
1 parent ef75648 commit d4263de

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

docs/spec/generics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ Bound Rules
20002000
T1 = TypeVar("T1", bound=int)
20012001
TypeVar("Ok", default=T1, bound=float) # Valid
20022002
TypeVar("AlsoOk", default=T1, bound=int) # Valid
2003-
TypeVar("Invalid", default=T1, bound=str) # Invalid: int is not a subtype of str
2003+
TypeVar("Invalid", default=T1, bound=str) # Invalid: int is not assignable to str
20042004

20052005
Constraint Rules
20062006
^^^^^^^^^^^^^^^^

docs/spec/literal.rst

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,23 @@ expressions, and nothing else.
9898
Legal parameters for ``Literal`` at type check time
9999
"""""""""""""""""""""""""""""""""""""""""""""""""""
100100

101-
``Literal`` may be parameterized with literal ints, byte and unicode strings,
102-
bools, Enum values and ``None``. So for example, all of
101+
``Literal`` may be parameterized with literal ``int``, ``str``, ``bytes``,
102+
and ``bool`` objects, instances of ``enum.Enum`` subclasses, and ``None``. So for example, all of
103103
the following would be legal::
104104

105105
Literal[26]
106-
Literal[0x1A] # Exactly equivalent to Literal[26]
106+
Literal[0x1A] # Equivalent to Literal[26]
107107
Literal[-4]
108108
Literal["hello world"]
109+
Literal[u"hello world"] # Equivalent to Literal["hello world"]
109110
Literal[b"hello world"]
110-
Literal[u"hello world"]
111111
Literal[True]
112-
Literal[Color.RED] # Assuming Color is some enum
112+
113+
class Color(enum.Enum):
114+
RED = 1
115+
GREEN = 2
116+
117+
Literal[Color.RED]
113118
Literal[None]
114119

115120
**Note:** Since the type ``None`` is inhabited by just a single
@@ -143,17 +148,6 @@ This should be exactly equivalent to the following type::
143148

144149
Literal[1, 2, 3, "foo", 5] | None
145150

146-
**Note:** String literal types like ``Literal["foo"]`` should subtype either
147-
bytes or unicode in the same way regular string literals do at runtime.
148-
149-
For example, in Python 3, the type ``Literal["foo"]`` is equivalent to
150-
``Literal[u"foo"]``, since ``"foo"`` is equivalent to ``u"foo"`` in Python 3.
151-
152-
Similarly, in Python 2, the type ``Literal["foo"]`` is equivalent to
153-
``Literal[b"foo"]`` -- unless the file includes a
154-
``from __future__ import unicode_literals`` import, in which case it would be
155-
equivalent to ``Literal[u"foo"]``.
156-
157151
Illegal parameters for ``Literal`` at type check time
158152
"""""""""""""""""""""""""""""""""""""""""""""""""""""
159153

docs/spec/tuples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Type Compatibility Rules
9191
------------------------
9292

9393
Because tuple contents are immutable, the element types of a tuple are covariant.
94-
For example, ``tuple[int, int]`` is a subtype of ``tuple[float, complex]``.
94+
For example, ``tuple[bool, int]`` is a subtype of ``tuple[int, object]``.
9595

9696
As discussed above, a homogeneous tuple of arbitrary length is equivalent
9797
to a union of tuples of different lengths. That means ``tuple[()]``,

0 commit comments

Comments
 (0)