from typing import Dict, MutableMapping
def populate_ranks(votes: Dict[str, int], ranks: Dict[str, int]) -> None:
names = list(votes.keys())
names.sort(key=votes.get, reverse=True)
for i, name in enumerate(names, 1):
ranks[name] = i
def get_winner(ranks: Dict[str, int]) -> str:
return next(iter(ranks))
class SortedDict(MutableMapping[str, int]):
...
votes = {
"수달": 1281,
"북극곰": 587,
"여우": 863,
}
sorted_ranks = SortedDict()
populate_ranks(votes, sorted_ranks)
print(sorted_ranks.data)
winner = get_winner(sorted_ranks)
print(winner)
$ python3 -m mypy --strict example.py
.../example.py:48: error: Argument 2 to "populate_ranks" has
↳incompatible type "SortedDict"; expected "Dict[str, int]" [arg-type]
.../example.py:50: error: Argument 1 to "get_winner" has
↳ incompatible type "SortedDict"; expected "Dict[str, int]" [arg-type]