Difference Between Defining typing.Dict
and dict
in Python?
I am practicing using type hints in Python 3.5, and I noticed that one of my colleagues uses typing.Dict
while defining function signatures. Here’s the code example:
import typing
def change_bandwidths(new_bandwidths: typing.Dict,
user_id: int,
user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return False
def my_change_bandwidths(new_bandwidths: dict,
user_id: int,
user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return True
def main():
my_id, my_name = 23, "Tiras"
simple_dict = {"Hello": "Moon"}
change_bandwidths(simple_dict, my_id, my_name)
new_dict = {"new": "energy source"}
my_change_bandwidths(new_dict, my_id, my_name)
if __name__ == "__main__":
main()
Both functions work fine, and I can’t see a difference in behavior. However, I have read the typing
module documentation and still wonder between typing.Dict
and dict
, which one should I use in my program?
How should I decide between Python typing dict and the built-in dict
for type hinting?
Hey @mehta_tvara ,
I’ve been using type hinting for years, and I can tell you, typing.Dict
is fantastic when you need precision with key-value types. For instance, typing.Dict
allows you to enforce stricter type checking, which is great for catching bugs early in larger projects. Here’s an example:
import typing
def change_bandwidths(new_bandwidths: typing.Dict[str, int], user_id: int, user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return False
In this case, you’re saying new_bandwidths
must be a dictionary with str
keys and int
values. If you’re managing a big codebase, this can be a lifesaver. Definitely worth using if you’re diving into serious Python typing dict scenarios!
Building on what @Ambikayache said, if you’re using Python 3.9 or later, there’s an even impler option for type hinting with dictionaries. Instead of importing typing.Dict
, you can just use dict
directly. The syntax is cleaner, and you don’t need an extra import. Here’s how it looks:
def my_change_bandwidths(new_bandwidths: dict[str, int], user_id: int, user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return True
See the difference? No typing
import, and it reads a bit more naturally. If you’re on Python 3.9+, this is the way to go. It’s still precise and leverages Python typing dict hints, but with less fuss.
Great points from both of you! Let me add another perspective—compatibility. If you’re working with Python 3.5 to 3.8, you won’t have access to the dict[str, int]
syntax. That’s where typing.Dict
becomes essential. It’s the only way to specify key-value types in those versions. Here’s the same example, adjusted for compatibility:
import typing
def change_bandwidths(new_bandwidths: typing.Dict[str, int], user_id: int, user_name: str) -> bool:
print(new_bandwidths, user_id, user_name)
return False
This ensures your code works seamlessly on older Python versions while still benefiting from Python typing dict hints. So, depending on your project or team’s Python version, choosing between typing.Dict
and dict
is all about compatibility and readability