Python type hints: typing.Mapping vs. typing.Dict

PythonPython 3.xType Hinting

Python Problem Overview


I'm working on a python3 project where we use the typing module type hints throughout.

It seems that we use typing.Dict and typing.Mapping pretty much interchangeably.

Is there a reason to prefer one over the other?

Python Solutions


Solution 1 - Python

Managed to answer this myself.

typing.Dict should be used to indicate a literal dict type with support for element type hinting i.e. Dict[bytes, str]

typing.Mapping is an object which defines the __getitem__,__len__,__iter__ magic methods

typing.MutableMapping is an object which defines same as Mapping but with __setitem__,__delitem__ magic methods as well.

typing.Mapping et al. are based on the abc types in this table

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstacksonstacksView Question on Stackoverflow
Solution 1 - PythonstacksonstacksView Answer on Stackoverflow