PEP 747: TypeExpr: Type Hint for a Type Expression

The section “Explicit TypeForm Evaluation“ in the PEP contains this fragment:

This explicit syntax serves two purposes. First, it documents the developer’s intent to use the value as a type form object. Second, static type checkers validate that all rules for type expressions are followed:

I think it would make sense to also explicitly mention that the static type of a TypeForm(...) expression will always be TypeForm[...], even if a TypeForm wouldn’t otherwise be expected based on type context. Currently the word ‘documents’ could be understood to say that this is only intended for programmers reading the code. This can be helpful in cases where the type context empty or ambiguous. For example, "list[str]" would be inferred as a normal string literal instead of a type form without a suitable type context, and TypeForm(...) can be used to force this type context.

3 Likes