PEP 484 update: add Type[T]

There's a more fundamental PEP 484 update that I'd like to add. The discussion is in https://github.com/python/typing/issues/107. Currently we don't have a way to talk about arguments and variables whose type is itself a type or class. The only annotation you can use for this is 'type' which says "this argument/variable is a type object" (or a class). But it's often useful to be able to say "this is a class and it must be a subclass of X". In fact this was proposed in the original rounds of discussion about PEP 484, but at the time it felt too far removed from practice to know quite how it should be used, so I just put it off. But it's been one of the features that's been requested most by the early adopters of PEP 484 at Dropbox. So I'd like to add it now. At runtime this shouldn't do much; Type would be just a generic class of one parameter that records its one type parameter. The real magic would happen in the type checker, which will be able to use types involving Type. It should also be possible to use this with type variables, so we could write e.g. T = TypeVar('T', bound=int) def factory(c: Type[T]) -> T: <implementation> This would define factory() as a function whose argument must be a subclass of int and returning an instance of that subclass. (The bound= option to TypeVar() is already described in PEP 484, although mypy hasn't implemented it yet.) (If I screwed up this example, hopefully Jukka will correct me. :-) Again, I'd like this to go out with 3.5.2, because it requires adding something to typing.py (and again, that's allowed because PEP 484 is provisional -- see PEP 411 for an explanation). -- --Guido van Rossum (python.org/~guido)
participants (1)
-
Guido van Rossum