How can I type hint an attribute in Python 3.5?

In Python 3.5, you have to write

self.some_attribute = None  # type: AnotherClass

Since Python 3.6, new type hinting syntax was added for variables (PEP 526):

self.some_attribute: AnotherClass = None

This will probably make every type-checking system complain, because None is in fact not an instance of AnotherClass. Instead, you can use typing.Union[None, AnotherClass], or the shorthand:

from typing import Optional
...
self.some_attribute: Optional[AnotherClass] = None

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)