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.
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.
Untyped dict & walrus type hints - Ideas - Discussions on Python.org
Python typing, what's the difference between dict[any,str] and dict(any,str)?
Json Type in Mypy.
Type hinting the return value of `callable` in dictionary
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]