Python typehints and linters

Python 3.6 implements PEP 526: Syntax for Variable Annotations, which as the name suggests introduces new syntax for variable annotations, removing the need for type comments.

In the new syntax, your code would be rewritten as:

from typing import List, Optional
from something import MyOtherClass

class MyClass:

    def __init__(self) -> None:
        self.some_var: Optional[List[MyOtherClass]] = None

... or alternatively:

from typing import List, Optional
from something import MyOtherClass

class MyClass:

    some_var: Optional[List[MyOtherClass]]

    def __init__(self) -> None:
        self.some_var = None

Since List and MyOtherClass now appear as actual tokens in the code, rather than comments, linters should have no trouble acknowledging that they are indeed being used.


@Zero Piraeus answer offers the most recent solution to this (i.e use variable annotations, also see: What are variable annotations in Python 3.6?).

Apart from that, you don't even need to import List when you're using # type: comments. mypy doesn't require them to be imported and neither to pyflakes or pylint as far as I am aware.

There's no need to import names from typing unless you require to use their name somewhere that Python actually performs a name look-up (and in comments, this isn't required.)