But let’s look at what happens in the terminal when you run mypy. If you were to try to run this Python script, reveal.py, you’re going to end up with errors because those are not valid Python functions. So, please note that this is a troubleshooting step. And here you’re going to use the Mypy expression of reveal_type() of math.pi.Ġ3:45 And then you can use another statement that’s part of Mypy of reveal_locals(). Let me give you an example.Ġ3:13 This file is going to be named reveal.py.Ġ3:18 Let me adjust everything here. One is reveal_type() and the other is reveal_locals(). If you’re to use circumference() and give it a value of 1.23, you can see there a float going in and a float coming out.Ġ2:52 So, how is Mypy interpreting your hints? If you have questions about that, there are a couple special Mypy expressions that can provide a little more clarity. And here you can see, there’s the two annotations that you put in for the types.Ġ2:35 The radius being of a float and the return value being of a float. _annotations_ as the very first choice here. _annotations_, so you could type circumference. When running the code, you can actually inspect these annotations that you’ve put in already.Ġ2:16 They’re stored in a special dunder method of. Working here in the REPL, I’ll have you import math and then define a function named circumference().Ġ1:49 circumference() takes an argument of radius, which is a float, and this function returns a float.Ġ1:59 Then the return is 2 * math.pi * radius. ) with two underscores ( _) and then the word annotations and then two underscores ( _).Ġ1:36 Let me have you work with annotations a little bit more with an example. _annotations_ attribute on the function.Ġ1:29 So again, that would be a dot (. To inspect the annotations while running your code, you can use the special. As a note, annotations must be a valid Python expression.Ġ1:17 One kind of interesting note is that you can do inspections of your annotations.
And then the syntax for return types is that space, greater than symbol arrow ( ->), and then the annotation. To do function annotations, you annotate the arguments and the return value.Ġ0:56 The syntax for arguments is the argument’s name, colon ( :), space, and then whatever the annotation is. And although there may have been other uses for annotations in the past, as type checking is becoming more and more common, annotations should mainly be reserved for type hints.Ġ0:42 Now this may seem as a bit of a review, annotation style is the same as you did for the type hints before.
#ANNOTATIONS DEFINITION HOW TO#
A few years passed and then PEP 484 defined how to add type hints to your Python code.Ġ0:24 The main way to add the type hints is by using the annotations, like you saw before in the type hint videos. First introduced in Python 3.0, they were used as a way to associate arbitrary expressions to function arguments and return values. 00:00 This video is about using annotations in your Python code.