Skip to content

Mapping and MutableMapping use the same example argument in docs #7617

@MarcoGorelli

Description

@MarcoGorelli

The cheat sheet from the docs shows the following:

# Mapping describes a dict-like object (with "__getitem__") that we won't
# mutate, and MutableMapping one (with "__setitem__") that we might
def f(my_dict: Mapping[int, str]) -> List[int]:
    return list(my_dict.keys())

f({3: 'yes', 4: 'no'})

def f(my_mapping: MutableMapping[int, str]) -> Set[str]:
    my_mapping[5] = 'maybe'
    return set(my_mapping.values())

f({3: 'yes', 4: 'no'})

In both cases, the argument passed to f is a dict, though in one case the argument is annotated as Mapping and in the other MutableMapping.

From this example, it might not be clear to a beginner when to use Mapping over MutableMapping.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions