Meaning
A dictionary that maps string keys to integer values.
Primary Function
Annotates that a variable, parameter, return value, or attribute is a dictionary whose keys are strings and values are integers, enabling static type checking and documentation.
Communicative Purpose
Communicates to readers and type checkers that the annotated object should behave as a mapping from strings to integers.
Pattern
Dict[<key_type>, <value_type>] where key_type is str and value_type is int
Core Structure
Dict[<key_type>, <value_type>] with key_type = str and value_type = int
Função primária
Annotates that a variable, parameter, return value, or attribute is a dictionary whose keys are strings and values are integers, enabling static type checking and documentation.
Propósito comunicativo
Communicates to readers and type checkers that the annotated object should behave as a mapping from strings to integers.
Situações de gatilho
When annotating function parameters, return values, class attributes, or variable assignments that hold a dict of string‑to‑int mappings.
Contextos
Function signatures, variable annotations, class attributes, return type annotations, and type aliases.
Padrão
Dict[<key_type>, <value_type>] where key_type is str and value_type is int
Estrutura central
Dict[<key_type>, <value_type>] with key_type = str and value_type = int
Slots de substituição
key_type: type (must be hashable), value_type: type
Colocados típicos
- typing.Dict
- collections.defaultdict
- typing.Mapping
- function arguments
- return annotations
- variable assignments
- .items()
- .get()
- len()
Substituições comuns
- typing.Mapping[str
- int] (more general)
- typing.MutableMapping[str
- int]
- dict[str
- int] (Python 3.9+)
- collections.defaultdict(str
- int)
- collections.Counter (when values are counts)
Erros comuns
Using a mutable default argument like def f(d: Dict[str, int] = {}): leads to shared state across calls; using a non‑hashable key type such as a list as a dict key raises TypeError; forgetting to import typing.Dict in Python <3.9 results in NameError; treating the annotation as a runtime type check and expecting isinstance to work; confusing Dict[str, int] with List[int] or Set[int] leading to incorrect attribute access.
Similar / contraste
Dict[int, str] – maps integers to strings instead of strings to integers; List[str] – ordered list of strings rather than a key‑value mapping; Set[str] – unordered collection of unique strings with no associated values; Tuple[str, int] – fixed‑size pair rather than a variable‑size mapping; Mapping[str, int] – more general read‑only mapping interface.
Interferências
Coming from Java: may assume Map<String, Integer> allows null values; in Python None is a valid integer value only if None is allowed, otherwise it violates the int hint → use Optional[int] if None is permitted. Coming from JavaScript: may assume object keys are always strings and values can be any type; in Python dict keys must be hashable and values must match the declared type, so using a list as a key or a string as a value will be caught by type checkers.
Família do chunk
- Dict[str
- int]
- Dict[int
- str]
- List[str]
- Set[str]
- Tuple[str
- int]
Nuance
(1) Do not use Dict[str, int] when you need mutable default values or keys that are not strings (e.g., tuples or custom objects); (2) Performance: dict look‑ups are O(1) average case with modest memory overhead; very large or deeply nested dicts can impact memory usage; (3) Boundary conditions: an empty dict {} satisfies the type, keys must be hashable strings (all strings are hashable), values must be integers – subclasses of bool are also ints and will pass the hint, which may be surprising.
Efeito pragmático
Using this type hint lets static analysers catch mismatched key or value types (e.g., passing an integer key or a string value) before runtime, improves code readability, and reduces bugs where incorrect data types are inserted into the dictionary.
Dica de memória
Think of a phone book where each name (a string) maps to a phone number (an integer).
Nota
Starting with Python 3.9, the built‑in dict can be subscripted directly as dict[str, int] without importing from typing.
Upgrade path
Consider using TypedDict for fixed‑key dictionaries or NewDomain for more domain‑specific mappings.
Log in to save chunks.