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

Dennis Brakhane brakhane at googlemail.com
Fri Jan 16 22:39:51 CET 2015


>
>     Forward references <https://www.python.org/dev/peps/pep-0484/#id9>
>
> When a type hint contains names that have not been defined yet, that
> definition may be expressed as a string, to be resolved later.

Is it intentionally undefined when exactly "later" is?

Silly example:

from typing import Set

def foo(bar: 'Set[Employee]') -> None:
    ...

class Employee:
    ...

def foo2(bar: Set[Employee]) -> None:
    ...

class Set(Generic[T]): # oops, I didn't pay attention and redefined Set
    ...



A conforming type checker could interpret the type of foo in two ways so
that foo either takes a normal set (resolved right after Employee was
defined) or an instance of the new class Set (what Set refers to at the
end of the file).

In the first case, the signatures of foo and foo2 are the same, in the
second one, foo2(bar: Set[Employee]) and foo2(bar: 'Set[Employee]') mean
different things.

Of course, a good style checker should have slapped the user for
redefining Set, so it's more of a academic problem and leaving "later"
vague doesn't matter in practise, but I'd still document the fact that
conforming implementations are free to choose what later means.




More information about the Python-ideas mailing list