Python etc / getattr type annotaions (PEP-526)

getattr type annotaions (PEP-526)

PEP-526, introducing syntax for variable annotations (landed in Python 3.6), allows annotating any valid assignment target:

c.x: int = 0
c.y: int

d = {}
d['a']: int = 0
d['b']: int

The last line is the most interesting one. Adding annotations to an expression suppresses its execution:

d = {}

# fails
d[1]
# KeyError: 1

# nothing happens
d[1]: 1

Despite being a part of the PEP, it's not supported by mypy:

$ cat tmp.py
d = {}
d['a']: int
d['b']: str
reveal_type(d['a'])
reveal_type(d['b'])

$ mypy tmp.py
tmp.py:2: error: Unexpected type declaration
tmp.py:3: error: Unexpected type declaration
tmp.py:4: note: Revealed type is 'Any'
tmp.py:5: note: Revealed type is 'Any'