
On 1 September 2016 at 18:21, Steven D'Aprano <steve@pearwood.info> wrote:
The simplest way would be to say "go on, one type hint won't hurt, there's no meaningful runtime cost, just do it".
from typing import Any
class X: NAME: Any
Since I'm not running a type checker, it doesn't matter what hint I use, but Any is probably the least inaccurate.
But I think there's a better way.
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
as equivalent to a declaration *without* a hint. The reader, and the type-checker, can see that there's an instance attribute called NAME, but in the absense of an actual hint, the type will have to be inferred, just as if it wasn't declared at all.
There is a convention for function annotations in PEP 484 that a missing annotation is equivalent to Any, so that I like your first option more. -- Ivan