Skip to content

Python Type Hints#

Python is a dynamically typed language, meaning variable types don't require explicit declaration. However, as projects grow in complexity, type annotations become increasingly valuable for code maintainability and clarity.

Type hints have been a major focus of recent Python releases, and I was particularly intrigued when I heard about Guido van Rossum's work on MyPy at Dropbox, where the team needed robust tooling to migrate their codebase from Python 2 to Python 3.

Today, type hints are essential for modern Python development. They significantly enhance IDE capabilities and AI-powered development tools by providing better code completion, static analysis, and error detection. This mirrors the evolution we've seen with TypeScript's adoption over traditional JavaScript—explicit typing leads to more reliable and maintainable code.

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.

Typing tools#

MyPy#

Ref. MyPy in this post.

Pyright && Pylance#

Ref. Pyright in this post.

Pylance is the Microsoft backed Pyright extension for VSCode.

RightTyper#

During an internal tech demo at my working, I heard about RightTyper, a Python tool that generates type annotations for function arguments and return values. It’s important to note that RightTyper doesn’t statically parse your Python files to add types; instead, it needs to run your code to detect types on the fly. So, one of the best ways to use RightTyper is with python -m pytest, assuming you have good test coverage.

ty#

ty represents the next generation of Python type checking tools. Developed by the team behind the popular ruff linter, ty is implemented in Rust for exceptional performance. It functions both as a type checker and language server, offering seamless integration through its dedicated VSCode extension ty-vscode.

While Ruff excels at various aspects of Python linting, type checking remains outside its scope. ty aims to fill this gap, though it's currently in preview and still evolving toward production readiness. The combination of Ruff and ty promises to provide a comprehensive Python code quality toolkit.

pyrefly#

pyrefly emerges as another promising entrant in the Python type checking landscape. Developed by Meta and also written in Rust, pyrefly offers both type checking capabilities and language server functionality. While still in preview, it demonstrates the growing trend of high-performance Python tooling implemented in Rust.

The tool integrates smoothly with modern development environments through its VSCode extension refly-vscode, making it accessible to a wide range of developers. Its backing by Meta suggests potential for robust development and long-term support.

Just a quick test, pyrefly seems to generate more typing errors than ty.

Comments