Hi David, Thank you for writing up the PEP; that was an interesting read. On Thu, Feb 4, 2021, at 22:27, David Foster wrote:
PEP 484 [^type-c] defines the type `Type[C]` where `C` is a class, to refer to a class object that is a subtype of `C`. It explicitly does not allow `Type[C]` to refer to typing special forms such as the runtime object `Optional[str]` even if `C` is an unbounded `TypeVar`. This PEP proposes a new type `TypeForm` that allows referring to both any class object and *also* to any runtime object that is a typing special form, such as `Optional[str]`, `Union[int, str]`, or `MyTypedDict`.
The question raised on the GitHub issue [0], "Why can't we make `Type[T]` also work for other special forms?", will likely come up for others reading this too. Would it be worth adding a sentence to explain?
For example, here is a function that checks whether a value is assignable to a variable of a particular type, and if so returns the original value:
``` T = TypeVar('T')
def trycast(form: TypeForm[T], value: object) -> Optional[T]: ... ```
Would it make sense to have an example that explicitly ties the concern raised in the introductory paragraph ("explicitly does not allow `Type[C]` to refer to typing special forms such as the runtime object `Optional[str]`") to what this PEP aims to solve? Phrased differently: why can the `trycast` definition not be: ``` def trycast(form: Type[T], value: object) -> Optional[T]: ... ``` That seems to work OK (see attached), although understanding why it isn't adequate would help motivate the document. I don't know who the target audience of the PEP is, but presumably it would not hurt to make it a bit more widely accessible. Crystallizing the advantage over what is currently available with usage examples of, say, `Union[int, str]`, would help. I've never been active on a SIG before, so please accept my apologies if comments from those still learning the inner workings of types are considered unhelpful at this stage or a breach of social protocol. Best regards, Stéfan [0] https://github.com/python/mypy/issues/9773#issuecomment-736810721