0

I am learning a bit about annotations. I am using Python 3.8.6 and before I dig into the TypedDict alternative, I would like to understand why my_dict() is accepting a dictionary (movie) that has an int as value:

from typing import Dict

movie = {"name": "Torrente", "year": "1996"}

def my_dict(bla: Dict[str, int]):

    return bla

print(my_dict(movie))

Could someone help me to understand this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Golpe
  • 1
  • 1
  • 2
    What are you expecting to do the type checking, here? MyPy, for example, _does_ tell you that's not valid: https://mypy-play.net/?mypy=latest&python=3.10&gist=8ffeded3d95cd70350fadd302021fbb7. – jonrsharpe Jan 25 '22 at 17:39
  • 2
    Wait, did you expect your type annotations to be enforced at runtime? They aren't. – juanpa.arrivillaga Jan 25 '22 at 17:40
  • ah, ok! I was expecting getting an error at runtime, i must use a tool like `mypy`. Thank you guys! – Golpe Jan 25 '22 at 18:01
  • @juanpa.arrivillaga , could you put your comment as answer, so that i can mark the post as solved? – Golpe Jan 25 '22 at 18:12
  • or yours @jonrsharpe, it was also helpful enough – Golpe Jan 25 '22 at 18:13
  • In that case it's covered by e.g. https://stackoverflow.com/q/41692473/3001761, https://stackoverflow.com/q/38892779/3001761 – jonrsharpe Jan 25 '22 at 18:14

0 Answers0