[Python-ideas] PEP 484 (Type Hints) -- first draft round

Andrew Barnert abarnert at yahoo.com
Sat Jan 17 01:27:32 CET 2015

On Jan 16, 2015, at 13:34, Guido van Rossum <guido at python.org> wrote:

> On Fri, Jan 16, 2015 at 12:14 PM, Andrew Barnert <abarnert at yahoo.com.dmarc.invalid> wrote:
>> One quick clarification question:
>> Can strings-as-forward-declarations be used inside non-string type annotations--e.g.. Set['Employee'] instead of 'Set[Employee]'? If so, do they have identical meaning?
> Yes. There's a FIXME in the PEP source to define forward references rigorously. See also https://github.com/ambv/typehinting/issues/34 

OK, thanks.

I see that MyPy already supports this (List['A'] is valid per Jukka's comment, and from a quick test it seems to be identical to 'List[A]').

>> I can imagine, say, a refactoring tool that didn't understand the full Python+MyPy syntax being able to do things with the former that it couldn't with the latter. And I think it might look more natural for simple recursive types. But those are just vague feelings, so don't take this as a suggestion.
> Well, the most common use case is where you are referencing the current class (which is in the process of being defined but isn't actually created until the entire class body is finished). E.g.
> class Node(Generic[T]):
>     def add_left(self, node: 'Node[T]'): ...

Sure, but in that case you're using a generic class being defined, with a declared typevar as a parameter. It's the other way around that's interesting--when the class being defined is a parameter to a declared generic type, like:

    class Node:
        def descendants(self) -> Set['Node']:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150116/c025d065/attachment.html>

More information about the Python-ideas mailing list