[Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations

Nick Coghlan ncoghlan at gmail.com
Thu Sep 1 22:38:27 EDT 2016


On 2 September 2016 at 02:21, Steven D'Aprano <steve at pearwood.info> wrote:
> Unless I've missed something, there's no way to pre-declare an instance
> attribute without specifying a type. (Even if that type is Any.) So how
> about we allow None as a type-hint on its own:
>
>     NAME: None

None already has a meaning as an annotation - it's a shorthand for "type(None)".

While for variables and parameters, that's usually only seen in
combination with Union, and even though Union[T, None] has a preferred
spelling as Optional[T], there's also the "-> None" case to specify
that a function doesn't return a value. Having "-> None" mean "no
return value" and "NAME: None" mean "infer type from later assignment"
would be quite confusing.

However, a standalone Ellipsis doesn't currently have a meaning as a
type annotation (it's only meaningful when subscripting Tuple and
Callable), so a spelling like this might work:

    NAME: ...

That spelling could then also be used in function definitions to say
"infer the return type from the return statements rather than assuming
Any":

    def inferred_return_type(): -> ...
        return some_other_function()

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list