Skip to content

Commit f450733

Browse files
committed
Fixup language around NewType in the spec
1 parent 9d613b1 commit f450733

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

conformance/results/mypy/aliases_newtype.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
conformant = "Partial"
22
notes = """
3-
`NewType`s are considered classes, not functions.
3+
`NewType`s are incorrectly considered to be classes.
44
"""
55
output = """
66
aliases_newtype.py:11: error: Argument 1 to "UserId" has incompatible type "str"; expected "int" [arg-type]

conformance/results/results.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ <h3>Python Type System Conformance Test Results</h3>
468468
<th class="column col2 conformant">Pass</th>
469469
</tr>
470470
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aliases_newtype</th>
471-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>`NewType`s are considered classes, not functions.</p></span></div></th>
471+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>`NewType`s are incorrectly considered to be classes.</p></span></div></th>
472472
<th class="column col2 conformant">Pass</th>
473473
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not reject use of NewType in `isinstance` call.</p><p>Does not reject use of NewType in class definition statement.</p><p>Does not report inconsistency between name of NewType and assigned identifier name.</p><p>Does not reject use of NewType with generic class with TypeVar.</p><p>Does not reject use of NewType with protocol class.</p><p>Does not reject use of NewType with TypedDict class.</p><p>Does not reject use of NewType with Any.</p></span></div></th>
474474
<th class="column col2 conformant">Pass</th>

conformance/tests/aliases_newtype.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Tests the `typing.NewType` function.
2+
Tests the `typing.NewType` type constructor.
33
"""
44

55
# Specification: https://typing.readthedocs.io/en/latest/spec/aliases.html#newtype
@@ -14,12 +14,12 @@
1414

1515
assert_type(UserId(5) + 1, int)
1616

17-
# > NewType('Derived', Base) returns a dummy function
18-
_: type = UserId # E: functions are not instances of `type`
17+
# > NewType('Derived', Base) returns a dummy object
18+
_: type = UserId # E: `NewType()` does not return an instance of `type`
1919

20-
# > Both isinstance and issubclass, as well as subclassing will fail for
21-
# > NewType('Derived', Base) since function objects don’t support these
22-
# > operations.
20+
# > Both ``isinstance`` and ``issubclass``, as well as subclassing will fail
21+
# > for ``NewType('Derived', Base)``, since the object returned by a call to
22+
# > ``NewType`` is not a class.
2323
isinstance(u2, UserId) # E: not allowed in isinstance call
2424

2525

docs/spec/aliases.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ to a definition::
156156
def __init__(self, _x: Base) -> None:
157157
...
158158

159-
While at runtime, ``NewType('Derived', Base)`` returns a dummy function
160-
that simply returns its argument. Type checkers require explicit casts
161-
from ``int`` where ``UserId`` is expected, while implicitly casting
159+
While at runtime, ``NewType('Derived', Base)`` returns a dummy object
160+
that simply returns its argument when called. Type checkers require explicit
161+
casts from ``int`` where ``UserId`` is expected, while implicitly casting
162162
from ``UserId`` where ``int`` is expected. Examples::
163163

164164
UserId = NewType('UserId', int)
@@ -176,7 +176,7 @@ from ``UserId`` where ``int`` is expected. Examples::
176176
``NewType`` accepts exactly two arguments: a name for the new unique type,
177177
and a base class. The latter should be a proper class (i.e.,
178178
not a type construct like ``Union``, etc.), or another unique type created
179-
by calling ``NewType``. The function returned by ``NewType``
179+
by calling ``NewType``. The callable returned by ``NewType``
180180
accepts only one argument; this is equivalent to supporting only one
181181
constructor accepting an instance of the base class (see above). Example::
182182

@@ -193,5 +193,8 @@ constructor accepting an instance of the base class (see above). Example::
193193
tcp_packet = TcpPacketId(127, 0) # Fails in type checker and at runtime
194194

195195
Both ``isinstance`` and ``issubclass``, as well as subclassing will fail
196-
for ``NewType('Derived', Base)`` since function objects don't support
197-
these operations.
196+
for ``NewType('Derived', Base)``, since the object returned by a call to
197+
``NewType`` is not a class.
198+
199+
See also :ref:`protocol-newtype-aliases` for a discussion of how
200+
``NewType`` interacts with protocol definitions.

docs/spec/protocol.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ For example::
514514
a: ProtoA = C # Type check error, signatures don't match!
515515
b: ProtoB = C # OK
516516

517+
.. _`protocol-newtype-aliases`:
517518

518519
``NewType()`` and type aliases
519520
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)