Note: typing.Dict has been deprecated as of Python 3.9, because the dict type itself can be used as a generic type directly (together with other standard containers). You can do the same in Python 3.7 or 3.8 if you use a from __future__ import annotations directive. My answer was originally written for much older Python 3 releases.


There is no real difference between using a plain typing.Dict and dict, no.

However, typing.Dict is a Generic type that lets you specify the type of the keys and values too, making it more flexible:

def change_bandwidths(new_bandwidths: typing.Dict[str, str],
                      user_id: int,
                      user_name: str) -> bool:

As such, it could well be that at some point in your project lifetime you want to define the dictionary argument a little more precisely, at which point expanding typing.Dict to typing.Dict[key_type, value_type] is a 'smaller' change than replacing dict.

You can make this even more generic by using Mapping or MutableMapping types here; since your function doesn't need to alter the mapping, I'd stick with Mapping. A dict is one mapping, but you could create other objects that also satisfy the mapping interface, and your function might well still work with those:

from collections.abc import Mapping

def change_bandwidths(new_bandwidths: Mapping[str, str],
                      user_id: int,
                      user_name: str) -> bool:

Now you are clearly telling other users of this function that your code won't actually alter the new_bandwidths mapping passed in.

Your actual implementation is merely expecting an object that is printable. That may be a test implementation, but as it stands your code would continue to work if you used new_bandwidths: object, because any object in Python is printable.

Answer from Martijn Pieters on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
Special type construct to mark class variables. As introduced in PEP 526, a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class.
🌐
Reddit
reddit.com › r/learnpython › how to type hint a dict of str, variable pairs
r/learnpython on Reddit: How to type hint a dict of str, variable pairs
July 2, 2023 -

Basically as the title says. I have a dict representing the str of a variable name and the variable itself. However, I can’t find anything saying how to type hint this. Any help would be greatly appreciated.

Discussions

Untyped dict & walrus type hints - Ideas - Discussions on Python.org
Hello, using Python 3.10.12, I have a couple of ideas about type hints for you to ponder. Adding type hints to key/value iteration over items of a dictionary whose type is unknown: for key:'str', value:'int' in unty… More on discuss.python.org
🌐 discuss.python.org
0
October 1, 2023
Python typing, what's the difference between dict[any,str] and dict(any,str)?
No, they're not the same thing. The latter shouldn't even work because dict expects named arguments. If you're using a Python version older than 3.9, you need to use typing.Dict for type hints instead of dict. So in this case, from typing import Any, Dict def example_function(variable: Dict[Any, str]): ... Are you sure that's the right way around, though? Usually I see dictionaries with strings as keys and the values being all kinds of types. More on reddit.com
🌐 r/learnpython
8
1
November 26, 2021
Json Type in Mypy.
json() returns JSON parsed as Python dictionaries and lists, so list | dict would work. That's pretty broad though, and may lead to issues down the road when reading this data, since lists and dicts have quite different interfaces. If you know exactly what shape of data is returned (which is usually the case), you would specify that instead. If it's a list of strings returned, you'd do list[str], for example. If it's a dictionary with specific keys though, look into a TypedDict. It allows you to specify the expected keys and the types of the associated values. More on reddit.com
🌐 r/learnpython
13
1
January 16, 2025
Type hinting the return value of `callable` in dictionary
You can specify the parameters (if any) and the return type. Since you didn't tell us anything about parameters, I'm just going to assume they take whatever. from collections.abc import Callable foo: dict[str, Callable[..., str]] = {} For more granular control, there's typing.TypedDict. More on reddit.com
🌐 r/learnpython
1
7
September 22, 2024
Top answer
1 of 5
388

Note: typing.Dict has been deprecated as of Python 3.9, because the dict type itself can be used as a generic type directly (together with other standard containers). You can do the same in Python 3.7 or 3.8 if you use a from __future__ import annotations directive. My answer was originally written for much older Python 3 releases.


There is no real difference between using a plain typing.Dict and dict, no.

However, typing.Dict is a Generic type that lets you specify the type of the keys and values too, making it more flexible:

def change_bandwidths(new_bandwidths: typing.Dict[str, str],
                      user_id: int,
                      user_name: str) -> bool:

As such, it could well be that at some point in your project lifetime you want to define the dictionary argument a little more precisely, at which point expanding typing.Dict to typing.Dict[key_type, value_type] is a 'smaller' change than replacing dict.

You can make this even more generic by using Mapping or MutableMapping types here; since your function doesn't need to alter the mapping, I'd stick with Mapping. A dict is one mapping, but you could create other objects that also satisfy the mapping interface, and your function might well still work with those:

from collections.abc import Mapping

def change_bandwidths(new_bandwidths: Mapping[str, str],
                      user_id: int,
                      user_name: str) -> bool:

Now you are clearly telling other users of this function that your code won't actually alter the new_bandwidths mapping passed in.

Your actual implementation is merely expecting an object that is printable. That may be a test implementation, but as it stands your code would continue to work if you used new_bandwidths: object, because any object in Python is printable.

2 of 5
43

typing.Dict is a generic version of dict:

class typing.Dict(dict, MutableMapping[KT, VT])

A generic version of dict. The usage of this type is as follows:

def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
     return word_list[word]

Here you can specify the type of key and values in the dict: Dict[str, int]

🌐
Medium
pavolkutaj.medium.com › explaining-type-hints-for-dictionaries-in-python-187d712df631
Explaining Type Hints for Dictionaries in Python | by Pavol Z. Kutaj | Medium
June 8, 2023 - Dict[str, str] is a type hint in Python that indicates a dictionary where the keys and values are both of type str.
🌐
Adam Johnson
adamj.eu › tech › 2021 › 05 › 10 › python-type-hints-how-to-use-typeddict
Python type hints: How to Use TypedDict - Adam Johnson
May 10, 2021 - We can use the typing union operator, |, to declare the possible types of values: from __future__ import annotations def get_sales_summary() -> dict[str, int | str | list[str]]: ...
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 2.3.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes", 7.5) # For tuples of varia
Find elsewhere
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
These "type hints" or annotations are a special syntax that allows declaring the type of a variable. By declaring types for your variables, editors and tools can give you better support. This is just a quick tutorial / refresher about Python type hints. It covers only the minimum necessary ...
🌐
Python
typing.python.org › en › latest › spec › typeddict.html
Typed dictionaries — typing documentation
In these cases, extra keys should not be allowed unless the TypedDict is defined to allow extra items. In this example, the director key is not defined in Movie and is expected to generate an error from a type checker: m: Movie = dict( name='Alien', year=1979, director='Ridley Scott') # error: Unexpected key 'director'
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › python 201 – all about the typeddict
Python 201 - All About the TypedDict - Mouse Vs Python
June 17, 2025 - When you want to type hint a dictionary, you would use the following: dict[key_type, value_type] ... Fortunately, modern Python no longer requires that extra import.
🌐
Dagster
dagster.io › blog › python-type-hinting
Using Type Hinting in Python Projects
from typing import Optional def ... the student is not found, return None · The Union type hint is used to indicate that a variable can be one of several types....
🌐
Python
peps.python.org › pep-0589
PEP 589 – TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys | peps.python.org
PEP 484 defines the type Dict[K, V] for uniform dictionaries, where each value has the same type, and arbitrary key values are supported. It doesn’t properly support the common pattern where the type of a dictionary value depends on the string value of the key.
🌐
Python.org
discuss.python.org › ideas
Untyped dict & walrus type hints - Ideas - Discussions on Python.org
October 1, 2023 - Hello, using Python 3.10.12, I have a couple of ideas about type hints for you to ponder. Adding type hints to key/value iteration over items of a dictionary whose type is unknown: for key:'str', value:'int' in untyped_dict.items(): ... Adding type hints to the walrus operator would also come in handy with untyped dicts: if status_code:'str' := untyped_dict.get('status_code', ''): ... Please note that this is not a request to find a workaround for the lack of these features.
🌐
GitConnected
levelup.gitconnected.com › python-type-hints-taming-the-duck-d9538224774f
Python type hints: taming the duck | by Bryan Palmer | Level Up Coding
January 23, 2024 - Since version 3.5, Python has been developing a syntax to annotate the expected type of variables, function parameters and return values. Such annotations or hints are intended to produce code that is easier to read, easier to maintain, and which can be examined by external type-checkers (such as mypy) to validate the type-safety of your program.
🌐
Real Python
realpython.com › ref › glossary › type-hint
type hint | Python Glossary – Real Python
In Python, a type hint is a syntactic construct that allows you to indicate the expected data types of variables, function arguments, and return values. They provide a way to improve your code’s maintainability by explicitly declaring the ...
🌐
Python
docs.python.org › 3 › glossary.html
Glossary — Python 3.14.7 documentation
A type that can be parameterized; typically a container class such as list or dict. Used for type hints and annotations.
🌐
GeeksforGeeks
geeksforgeeks.org › python › specify-argument-type-for-list-of-dictionary-for-a-python
Specify Argument Type For List of Dictionary For a Python - GeeksforGeeks
July 23, 2025 - The type of output is a list that contains a Dictionary with values - a string and integer. ... # typing is a standard library from typing import List, Dict def GetData(name: str, age: int) -> List[Dict[str, int]]: return [{'name': name, 'age': age}] print(GetData('Sandeep Jain', 35)) ... In this example, we are using list comprehension to get a List of Dictionary. ... # import library from typing import List, Dict # define function def get_list_of_dicts(name: str, category: str) -> Dict[str, str]: return [{'name': name, 'category': category}] Fruit = ["Mango", "tomato", "potato", "papaya"] Fruit_type = ["fruit", "vegetable", "vegetable", "fruit"] res = [get_list_of_dicts(Fruit[i], Fruit_type[i]) for i in range(3)] print(res)
🌐
Medium
medium.com › @DataCraft-Innovations › python-type-hints-writing-clearer-and-more-reliable-code-58d27527a409
Python Type Hints: Writing Clearer and More Reliable Code | by Ronak Rathore | Medium
October 25, 2024 - ... Even though nothing prevents you from changing scores to a list of strings, type-checking tools will throw a warning if you do so. For lists, dictionaries, tuples, and sets, type hints make it clear what kinds of items they contain.
🌐
W3Schools
w3schools.com › python › python_arguments.asp
Python Function Arguments
You can send any data type as an argument to a function (string, number, list, dictionary, etc.).
🌐
Like Geeks
likegeeks.com › home › python › python dictionary type hinting: from dict to typeddict
Python Dictionary Type Hinting: From Dict to TypedDict
November 18, 2024 - To hint at a simple dictionary, use the dict keyword along with types for keys and values.
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-type-hinting-and-annotations-work-319d952247a6
How Python’s Type Hinting and Annotations Work | Medium
July 14, 2024 - Type hints reduce the need for extensive comments to explain what types are expected. They act as inline documentation, providing information about the types directly in the function signature.