I am trying to annotate my code with types but I am a little confused when it comes to sets. I read some points in PEP 484:
Note: Dict , List , Set and FrozenSet are mainly useful for annotating return values. For arguments, prefer the abstract collection types defined below, e.g. Mapping , Sequence or AbstractSet .
and
Set, renamed to AbstractSet . This name change was required because Set in the typing module means set() with generics.
but this does not help.
My first question is: what are the commonalities and differences between Set, FrozenSet, MutableSet and AbstractSet?
My second question is: why if I try
from collections import FrozenSet
I get
ImportError: cannot import name 'FrozenSet'
?
I am using Python 3.4 and I have installed mypy-lang via pip.
Be careful with annotations and typing. The ideas discussed in 484 are brand new, and implemented in the typing
module. That module is only available in Python3.5 (the latest typing
is also available from pip
for both Py2 and Py3).
https://docs.python.org/3/library/typing.html
That note that you quoted is from a section in 484 that starts:
To open the usage of static type checking to Python 3.5 as well as older versions, a uniform namespace is required. For this purpose, a new module in the standard library is introduced called typing .
The things that the note lists are annotation types, not actual object classes (builtin or from collections
). Don't confuse the two.
Note that Dict
, List
, Set
and FrozenSet
are all capitalized, where as the functions (and type names) are dict
, list
, set
, frozenset
. In other words to make a dictionary you use dict()
or {}
, not Dict
.
Annotations are new to 3.0 (not in 2.n at all). In a regular interpreter all they do is populate the function's __annotations__
dictionary. There's nothing in the interpreter that uses or requires annotations.
http://mypy-lang.org/ describes itself as an experiemental typing checker. You need to look at it's documentation to see how compatible it is with 484 etc.
https://docs.python.org/3/library/collections.abc.html#module-collections.abc has some abstract definitions, which I believe typing
uses. I've never used these. They are mainly for people developing new classes of objects, not 'regular' users.
The typing
tag for this question is probably not a good idea. It doesn't have many followers, and is too generic. It does not refer to this Python module.
Search for [python] 484
for other SO questions dealing with this style of annotations.
https://github.com/python/typing - the typing
development repository.
In this repository, there is a FrozenSet
definition in the python2/typing.py
file (the python2 backport), but not in src/typing.py
. I'm not sure of the significance of that.