Skip to content

Commit 25a4525

Browse files
committed
Address grievejia review comments on Never/NoReturn spec
- Use int instead of object as the non-Never witness (object is already expected to not be a subtype of most things; int is more illuminating) - Rephrase to avoid the too-strong 'No type other than Never' claim (uninhabitable types like tuple[Never, int] are also subtypes of Never) - Qualify all 'every type T' as 'every fully static type T' throughout (the subtype relation is undefined for gradual types like Any) - Replace list[Never] with Sequence[Never] in the covariant example (list is invariant, so list[Never] is not assignable to list[int]; Sequence is covariant and correctly demonstrates the bottom-type behaviour)
1 parent 498e503 commit 25a4525

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

docs/spec/special-types.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ to a variable of any type ``T``::
117117
v1: int = x # OK — Never is a subtype of int
118118
v2: str = x # OK — Never is a subtype of str
119119

120-
No type other than ``Never`` itself is a subtype of ``Never``. In
121-
particular, ``object`` is *not* a subtype of ``Never``::
120+
For ordinary inhabited types such as ``int`` or ``str``, no value is
121+
assignable to ``Never``::
122122

123-
def g(x: object) -> Never:
124-
return x # Error — object is not assignable to Never
123+
def g(x: int) -> Never:
124+
return x # Error — int is not assignable to Never
125125

126-
Because ``Never`` is a subtype of every type ``T``, the union ``Never | T`` is
127-
equivalent to ``T``::
126+
Because ``Never`` is a subtype of every fully static type ``T``, the union
127+
``Never | T`` is equivalent to ``T``::
128128

129129
from typing import Never, Union
130130

@@ -138,12 +138,14 @@ Using ``Never`` as a type argument
138138

139139
``Never`` may appear as a type argument. When used with a covariant
140140
type parameter, ``Container[Never]`` is a subtype of ``Container[T]`` for
141-
every type ``T``, because ``Never`` is a subtype of every type. This is
142-
useful to type an empty container whose element type is not yet known::
141+
every fully static type ``T``, because ``Never`` is a subtype of every fully
142+
static type. This is useful to type an empty container whose element type is
143+
not yet known::
143144

145+
from collections.abc import Sequence
144146
from typing import Never
145147

146-
def empty_list() -> list[Never]:
148+
def empty_sequence() -> Sequence[Never]:
147149
return []
148150

149151
When used with an invariant type parameter, the normal invariance rules
@@ -162,10 +164,10 @@ apply: ``Container[Never]`` is only assignable to ``Container[Never]``::
162164
``type[Never]``
163165
^^^^^^^^^^^^^^^^^
164166

165-
``type[Never]`` is a subtype of ``type[T]`` for every type ``T``, just as
166-
``Never`` is a subtype of every type ``T``. In practice a variable receives
167-
this type only in provably unreachable code — for example, when narrowing a
168-
``type[int] | type[str]`` through all possible branches.
167+
``type[Never]`` is a subtype of ``type[T]`` for every fully static type ``T``,
168+
just as ``Never`` is a subtype of every fully static type ``T``. In practice
169+
a variable receives this type only in provably unreachable code — for example,
170+
when narrowing a ``type[int] | type[str]`` through all possible branches.
169171

170172
.. _`numeric-promotions`:
171173

0 commit comments

Comments
 (0)