Jose: Hi guys and welcome back.
In this video, we're going to talk about how
to add type hinting to our application.
Type hinting is going to be really useful
because it allows to avoid some bugs.
And if you don't know what type hinting is,
we've got a full section dedicated to that
in our complete Python course.
If you're not in that course,
we're gonna leave a couple of links
in the resources section of this lecture
for you to check out instead.
But it's pretty straight forward,
so let's go to it.
Here we've got our application
and what we're gonna do
is we're gonna begin by type hinting our functions here.
So, we're going to go ahead
and add the...
imports.
So from the typing module
we're gonna import list and tuple.
And then we're going to go to down here
where we have get_Poll_details for example.
And every parameter is going to get a type hint.
What that's gonna do is later on when we call this function,
PyCharm is going to know what sort of value
this parameter should be.
So what we're gonna do is Poll_id should be an integer,
so we simply type : int before the bracket
and that annotates the Poll_id and tells the PyCharm linter
or type checker that it should be an integer.
And if we try to call this function with something
that's not an integer,
then PyCharm is gonna give us a warning.
Note that it will be a warning,
and it's never going to be an error.
Type hinting is just a hint,
so that's something to remember.
So the other things that are gonna be integers,
we're gonna annotate as well,
such as this Poll_id down here, Option_id down here.
And for these, there are, for example, strings:
the title and owner, we're going to do str down there.
Options we're going to leave for just now
and we're going to come back to it.
Username is going to be a string
and Option_id is going to be an integer as well.
The Options list here is going to be a list of strings.
So how we annotate that is we use the typing module
and we do list of strings inside square brackets.
That tells the Python type hinting system
that this is going to be a list
and it is going to be a list of strings.
Note that I'm using Python 3.8 at the moment
but, as of Python 3.9, you no longer need
to import the typing module.
You can simply use the built-in list for this.
But, because I'm using Python 3.8, I can't do that yet.
So you do have to import and use the L there
in order to use the list type hinting mechanism
with Python 3.8 and above.
So now that we have got our
parameter type hints for everything, we can also type hint
the return type of these functions, and that is where
it's going to get a bit more interesting.
What I'm going to do is I'm going to
define my own custom types.
And the reason I'm going to do that is because
these are going to reappear everywhere
and I don't want to be duplicating a lot of code.
So I'm going to do Poll is equal to a tuple
of int, string, and string.
And the reason I'm doing this is because
whenever one of my functions is returning a Poll,
that I'm going to signal with the Poll type
and that represents a tuple with three values inside it.
The first one is going to be an integer,
the second one is going to be a string,
and the third one is going to be a string as well.
The reason why I'm doing this is because
the Polls that are coming from my table
are integer, string, and string.
I'm going to do the same for votes.
And the reason I'm not doing it for Option is because
we actually never return Options on their own
right now so we don't need it.
However, we do return Poll with Option joined together.
So I'm going to create a PollWithOption type
that is a tuple that has the Poll: int, str, str;
and that it also has the Option: int, str, int.
So this tuple here represents the joined tables:
the Poll and the Option.
What we do return, though,
are Polls with Options joined together.
So I'm going to create the PollWithOption type here
that is a tuple of Polls: int, str, and str;
joined together with the Option which is int, str, and int.
Note that the Polls have id, title, and owner_username,
so that's these three,
and Options have the id, Option_text, and Poll_id,
so that's these three.
Then we're also going to get PollResults
which is another tuple that we're going to use
and which is an int, str, int,
and a float for the vote percentage.
That's down here.
Now, let's go ahead and use these types
to type hint the return values of our functions.
What we can do, for example here with get_Polls,
is we can do this arrow with a dash and a greater than sign
and then List of Poll, and that is going to
give us the list of tuples that we defined above.
And that means that whenever we get
the values from this function, the caller of the function
will actually be able to see that this is
a list of tuples that are being returned.
PyCharm can make use of that by offering you hints later on,
such as the sort of things that you can do
with a returned value.
For get_latest_Poll, we're going to do
List of PollWithOption.
For Poll details we're going to do the same, actually:
List of PollWithOptions.
For the get_Poll_and_vote_results, we're going to do
a List...
of PollResults.
Note that some of these are lists
even though they only pertain to one Poll,
because the database is returning multiple rows.
This one is simply going to be a Vote
because it gets one random Vote from the database.
And that's about it.
These don't return anything so we can just leave them.
Alternatively, you can always do None,
but it's not necessary.
That's implying that it's just going to add
a bit of craft to your code.
In app.py, there's actually not much typing to do,
or type hinting, because what we've got here
is just this function that actually
accepts anything or returns anything.
And so all we have to do here is go ahead and say
List of database.PollWithOption.
I'm using this here to refer to
the type defined in the database,
because it's just defined in one place.
So I'm not bothered enough to move my types
into a separate file.
You could do that if you are using them
from many different places, but right now
we don't have to do that.
We do have to import this, though.
So I'm going to just click over it and then
find this wee icon, import this name, type in .List,
and that's going to essentially just paste that
up there for me or you could type it yourself, of course.
That's everything for type hinting.
Now what that's going to do is, for example,
if we go down here, and this is going to know that
Poll_with_Options is a tuple.
So if you try to do something like .append(),
then it's going to allow it that.
But if you try to run something here
that doesn't exist in a list, then it's not
going to appear as a suggestion in PyCharm
and it will underline and say,
"List object doesn't have this method," for example.
So it's all pretty good with that.
It's really helpful, especially when
you're doing larger applications.
That's everything for this video.
Thank you guys for watching.
I'll see you in the next one.