Skip to content

Python Type Hints#

typing module vs collections module#

Since Python 3.9, most of types in typing module is deprecated, and collections module is recommended.

Some types like: typing.Any, typing.Generic, typing.TypeVar, etc. are still not deprecated.

Aliases to Built-in Types#

Deprecated AliasReplacement
typing.Dictdict
typing.Listlist
typing.Setset
typing.FrozenSetfrozenset
typing.Tupletuple
typing.Typetype

Aliases to Types in collections#

Deprecated AliasReplacement
typing.DefaultDictcollections.defaultdict
typing.OrderedDictcollections.OrderedDict
typing.ChainMapcollections.ChainMap
typing.Countercollections.Counter
typing.Dequecollections.deque

Aliases to Other Concrete Types#

Deprecated AliasReplacement
typing.Patternre.Pattern
typing.Matchre.Match
typing.Textstr
typing.ByteStringcollections.abc.Buffer or union like bytes | bytearray | memoryview

Aliases to Container ABCs in collections.abc#

Deprecated AliasReplacement
typing.AbstractSetcollections.abc.Set
typing.Collectioncollections.abc.Collection
typing.Containercollections.abc.Container
typing.ItemsViewcollections.abc.ItemsView
typing.KeysViewcollections.abc.KeysView
typing.Mappingcollections.abc.Mapping
typing.MappingViewcollections.abc.MappingView
typing.MutableMappingcollections.abc.MutableMapping
typing.MutableSequencecollections.abc.MutableSequence
typing.MutableSetcollections.abc.MutableSet
typing.Sequencecollections.abc.Sequence
typing.ValuesViewcollections.abc.ValuesView

Aliases to Asynchronous ABCs in collections.abc#

Deprecated AliasReplacement
typing.Coroutinecollections.abc.Coroutine
typing.AsyncGeneratorcollections.abc.AsyncGenerator
typing.AsyncIterablecollections.abc.AsyncIterable
typing.AsyncIteratorcollections.abc.AsyncIterator
typing.Awaitablecollections.abc.Awaitable

Aliases to Other ABCs in collections.abc#

Deprecated AliasReplacement
typing.Iterablecollections.abc.Iterable
typing.Iteratorcollections.abc.Iterator
typing.Callablecollections.abc.Callable
typing.Generatorcollections.abc.Generator
typing.Hashablecollections.abc.Hashable
typing.Reversiblecollections.abc.Reversible
typing.Sizedcollections.abc.Sized

Aliases to contextlib ABCs#

Deprecated AliasReplacement
typing.ContextManagercontextlib.AbstractContextManager
typing.AsyncContextManagercontextlib.AbstractAsyncContextManager

Notes#

  • Deprecated aliases are guaranteed to remain until at least Python 3.14.
  • Type checkers may flag deprecated aliases for projects targeting Python 3.9+.

Sequence & Collection#

  • collections.abc.Sequence is a type of ordered collection. Sequence does not include append and extend methods.
  • collections.abc.Collection is a type of unordered collection.
TypeSequenceCollection
strYesNo
tupleYesYes
listYesYes
setNoYes
dictNoYes
orderYesNo
indexing (e.g., seq[0])YesNo
Membership Checks (x in data)YesYes

(Python 3.9+) Both typing.Sequence and typing.Collection are deprecated aliases.

To be continued#

Comments