2
def json_file_to_dict(_file: str) -> dict:

Does it mean that function returns a dictionary type? As far as I know, python is loosely typed language and it does not enforce you to specify data type of a variable.

Andy
  • 49,085
  • 60
  • 166
  • 233
shakti singh
  • 724
  • 1
  • 7
  • 7
  • http://stackoverflow.com/questions/32557920/what-are-type-hints-python-3-5-0 and http://stackoverflow.com/questions/29770412/how-why-does-python-type-hinting-syntax-work – hiro protagonist Sep 15 '15 at 15:17

2 Answers2

4

These are type hints. They were introduced in Python 3.5 as part of PEP 0484.

They are essentially a contract that this function returns a dict (and expects _file to be an instance of str), but they're not strictly enforced. They're also fully optional.

From the PEP:

While these annotations are available at runtime through the usual __annotations__ attribute, no type checking happens at runtime. Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily. Essentially, such a type checker acts as a very powerful linter.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • I'm under python 3.4.1 and using for a long, maybe I confuse between function annotation and type hints... What this the point ? – Ali SAID OMAR Sep 15 '15 at 15:43
  • @AliSAIDOMAR Type hints build upon function annotations, which were introduced in Python 3.0 ([PEP 3107](https://www.python.org/dev/peps/pep-3107/)). – Thomas Orozco Sep 15 '15 at 15:44
0

It's a syntax of type hints. It'll be included in Python 3.5.

For detailed description please refer to PEP0484.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93