The Python Oracle

Type hinting a collection of a specified type

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping

--

Chapters
00:00 Question
00:46 Accepted answer (Score 283)
02:23 Answer 2 (Score 178)
02:54 Answer 3 (Score 136)
04:06 Answer 4 (Score 54)
04:39 Thank you

--

Full question
https://stackoverflow.com/questions/2485...

Accepted answer links:
[new ]: https://docs.python.org/3/library/typing...
[image]: https://i.stack.imgur.com/KHn4f.jpg
[PEP0484 (Type Hints)]: https://www.python.org/dev/peps/pep-0484/
[github under ambv/typehinting]: https://github.com/ambv/typehinting
http://mail.python.org/pipermail/python-...

Answer 2 links:
[typing]: https://docs.python.org/3/library/typing...
[List]: https://docs.python.org/3/library/typing...

Answer 3 links:
[PEP 585]: https://www.python.org/dev/peps/pep-0585/
[__future__.annotations]: https://docs.python.org/3.7/library/__fu...

Answer 4 links:
[PEP 484]: https://www.python.org/dev/peps/pep-0484...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #typehinting #pythontyping

#avk47



ACCEPTED ANSWER

Score 333


As of May 2015, PEP 484 (Type Hints) has been formally accepted. The draft implementation is also available at github under ambv/typehinting.

In September 2015, Python 3.5 was released with support for Type Hints and includes a new typing module. This allows for the specification of types contained within collections. As of November 2015, JetBrains PyCharm 5.0 fully supports Python 3.5 to include Type Hints as illustrated below.

PyCharm 5.0 Code Completion using Type Hints

from typing import List

def do_something(l: List[str]):
    for s in l:
        s  # str

Original Answer

As of Aug 2014, I have confirmed that it is not possible to use Python 3 type annotations to specify types within collections (ex: a list of strings).

The use of formatted docstrings such as reStructuredText or Sphinx are viable alternatives and supported by various IDEs.

It also appears that Guido is mulling over the idea of extending type annotations in the spirit of mypy: http://mail.python.org/pipermail/python-ideas/2014-August/028618.html




ANSWER 2

Score 205


Since Python 3.5 has been officially out, there is the Type Hints supporting module - typing and the relevant List "type" for the generic containers.

In other words, now you could do:

from typing import List

def my_func(l: List[int]):
    pass

This is deprecated since Python 3.9, now you can directly use the built-in list instead of typing.List.




ANSWER 3

Score 63


Type comments have been added since PEP 484

from . import Monitor
from typing import List, Set, Tuple, Dict


active_monitors = [] # type: List[Monitor]
# or
active_monitors: List[Monitor] = []

# bonus
active_monitors: Set[Monitor] = set()
monitor_pair: Tuple[Monitor, Monitor] = (Monitor(), Monitor())
monitor_dict: Dict[str, Monitor] = {'codename': Monitor()}

# nested
monitor_pair_list: List[Dict[str, Monitor]] = [{'codename': Monitor()}]

This is currently working for me on PyCharm with Python 3.6.4

Example Picture in Pycharm




ANSWER 4

Score 4


With support from the BDFL, it's almost certain now that python (probably 3.5) will provide a standardized syntax for type hints via function annotations.

https://www.python.org/dev/peps/pep-0484/

As referenced in the PEP, there is an experimental type-checker (kind of like pylint, but for types) called mypy that already uses this standard, and doesn't require any new syntax.

http://mypy-lang.org/