@@ -200,12 +200,17 @@ that represent a type assignable to ``str | None``::
200200 err1: TypeForm[str | None] = str | int # Error
201201 err2: TypeForm[str | None] = list[str | None] # Error
202202
203- By this same definition, ``TypeForm[Any ] `` describes a type form object
204- that represents the type ``Any `` or any type that is assignable to ``Any ``.
205- Since all types in the Python type system are assignable to ``Any ``,
206- ``TypeForm[Any ] `` describes the set of all type form objects
203+ By this same definition, ``TypeForm[object ] `` describes a type form object
204+ that represents the type ``object `` or any type that is assignable to ``object ``.
205+ Since all types in the Python type system are assignable to ``object ``,
206+ ``TypeForm[object ] `` describes the set of all type form objects
207207evaluated from all valid type expressions.
208208
209+ ``TypeForm[Any] `` describes a ``TypeForm `` type whose type argument is not
210+ statically known but is a valid type form object. It is thus assignable both
211+ to and from any other ``TypeForm `` type (because ``Any `` is assignable both
212+ to and from any type).
213+
209214The type expression ``TypeForm ``, with no type argument provided, is
210215equivalent to ``TypeForm[Any] ``.
211216
@@ -289,9 +294,9 @@ This explicit syntax serves two purposes. First, it documents the developer's
289294intent to use the value as a type form object. Second, static type checkers
290295validate that all rules for type expressions are followed::
291296
292- x4 = type(int ) # No error, evaluates to "type[int]"
297+ x4 = type(1 ) # No error, evaluates to "type[int]"
293298
294- x5 = TypeForm(type(int )) # Error: call not allowed in type expression
299+ x5 = TypeForm(type(1 )) # Error: call not allowed in type expression
295300
296301
297302Assignability
@@ -306,6 +311,14 @@ Assignability
306311 t1: TypeForm[int | str] = get_type_form() # OK
307312 t2: TypeForm[str] = get_type_form() # Error
308313
314+ ``type[T] `` is a subtype of ``TypeForm[T] ``, which means that ``type[B] `` is
315+ assignable to ``TypeForm[A] `` if ``B `` is assignable to ``A ``::
316+
317+ def get_type() -> type[int]: ...
318+
319+ t3: TypeForm[int | str] = get_type() # OK
320+ t4: TypeForm[str] = get_type() # Error
321+
309322``TypeForm `` is a subtype of ``object `` and is assumed to have all of the
310323attributes and methods of ``object ``.
311324
@@ -530,34 +543,6 @@ of ``type``.
530543.. _
designed :
https://mail.python.org/archives/list/[email protected] /message/D5FHORQVPHX3BHUDGF3A3TBZURBXLPHD/ 531544
532545
533- Treat ``type[T] `` as a subtype of ``TypeForm[T] ``
534- -------------------------------------------------
535-
536- It was suggested that type ``type[T] `` should be considered a subtype
537- of ``TypeForm[T] ``. This was ultimately rejected because there are ways to
538- create an object of type ``type[T] `` that does not encode a valid type expression.
539- For example, the expression ``type[1] `` is not a valid type expression. Likewise,
540- the expression ``type[S] `` is not a valid type expression if ``S `` is an
541- out-of-scope type variable. This same argument applies to other special forms
542- like objects of type ``UnionType ``, which may or may not encode a valid type
543- expression. Rather than carving out a special case for ``type[T] `` that would
544- allow for potential unsoundness, it was decided to treat all type forms
545- consistently. Therefore, ``type[T] `` is not considered a subtype of ``TypeForm[T] ``.
546-
547- It was also pointed out that the expression ``C | C `` (where ``C `` is a class
548- object) is a valid type expression - and therefore a valid ``TypeForm ``,
549- but its runtime type form encoding is an instance of ``UnionType `` and
550- therefore is not compatible with ``type[C] ``.
551-
552- If a function wishes to indicate that it accepts values of type ``TypeForm[T] ``
553- _and_ ``type[T] ``, the parameter can simply be annotated with a union of these
554- two types.
555-
556- ::
557-
558- def func[T](t: TypeForm[T] | type[T]) -> None: ...
559-
560-
561546
562547Accept arbitrary annotation expressions
563548---------------------------------------
0 commit comments