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.
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.
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]
How to type hint dictionaries without using Any?
How to set the python type hinting for a dictionary variable? - Stack Overflow
Untyped dict & walrus type hints - Ideas - Discussions on Python.org
How to Set Type Hinting for a Dictionary in Python - TestMu AI Community
Videos
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.
Hi folks I tried to keep the title as short and concise as possible, I often find myself struggling for hours in order to properly type annotate nested dictionaries in my code.
If the dict only has 1 level, it works well but with a tiny more complex ones like the following, I have a lot of trouble:
{"@context": "http://schema.org",
"@type": "VideoObject",
"url": "https://www.example.com/0000/adventures-music-videos.html",
"name": "sample movie",
"description": "example",
"thumbnailUrl": "thumbnail.com/1.jpg",
"uploadDate": "2023-11-29",
"duration": "PT2H20M20S",
"contentUrl": "sample_link",
"isAccessibleForFree": false,
"productionCompany": {"@type": "Organization",
"name": "Example",
"@id": "https://www.example.com/0000/studio/music-movies.html"},
"hasPart": [{"@type": "Clip",
"name": "scene 1",
"startOffset": 15,
"endOffset": 2126,
"url": "https://www.example.com/0000/adventures-music-videos.html#scene_0000",
"isAccessibleForFree": false},
{"@type": "Clip",
"name": "scene 2",
"startOffset": 2128,
"endOffset": 4114,
"url": "https://www.example.com/0000/adventures-music-videos.html#scene_0000",
"isAccessibleForFree": false},
{"@type": "Clip",
"name": "scene 3",
"startOffset": 4117,
"endOffset": 6055,
"url": "https://www.example.com/0000/adventures-music-videos.html#scene_0000",
"isAccessibleForFree": false},
{"@type": "Clip",
"name": "scene 4",
"startOffset": 6057,
"endOffset": 8414,
"url": "https://www.example.com/0000/adventures-music-videos.html#scene_0000",
"isAccessibleForFree": false}]}I tried dict[str, object] and I also tried a looong annotation with all the possible types separated by a pipe, but for now the only one that does not trigger mypy in strict mode is dict[str, Any].
Any clues as to what I might be doing wrong?
Dict takes two "arguments", the type of its keys and the type of its values. For a dict that maps strings to integers, use
def do_something(value: Dict[str, int]):
The documentation could probably be a little more explicit, though.
Python 3.9 on:
Use lowercase dict in the same method as the accepted answer. typing.Dict and similar upper case generic types which mirror built-ins are deprecated due to PEP 585:
def my_func(value: dict[str, int]):
pass