Python: A Quick Guide To Type Annotations (ft. Mypy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video was brought to you by IND dentle IO learning python Made Simple how's it going everyone in today's video we're going to be covering the topic of type annotations and why I personally love to type absolutely everything in Python because a lot of you ask me why do I type out silly lines of code like this and to answer all of this I'm first going to teach you the concept of type annotations and if you are already familiar with type annotations and what do you can just skip ahead to the next timestamp so to get started let's create a few variables one that's called X of type 10 then one that's called weather which will be the string of Cloudy and right below that we're going to say that the weather is now equal to let's say 100 and then we're going to create some data which is going to be a dictionary of Bob with the value of one and James with the value of two so so far everything is perfect besides the fact that we redeclared weather without using it so just to make the code editor happy we can do something such as print weather and just like that we will have no warnings and immediately you will probably see that there's an issue here we have a variable called weather which initially started out as a string but then we reassigned a new value to it which converted it into an integer and this might or might not be what you want just by typing in weather as a variable name is not that descriptive we don't know if that's supposed to be an enum an integer some sort of dictionary we have no idea what weather is supposed to be so in Python in general it's a good idea to annotate your types because you are explicitly telling whoever is looking at your program what that variable is supposed to be so here we're saying that the weather is supposed to be of type string and that was quite straightforward and thanks to this type annotation we'll also know when we mess up later in our program for example here we try to change the value of weather to 100 but our code editor warns us that we passed in the wrong data type so now we can pass in something else such as sunny and we will get no complaints and the same thing goes for dictionary we can annotate that by saying that this dictionary has a type of string to integer so string is the key and integer is the value so these type annotations help us with being extra explicit about what we're doing with our data and most of the time when you use these type annotations your code editor is going to help you out and that just means that it's going to warn you if you do something silly or in other cases it will even give you extra context actions and I'll show you what I mean by that in just a moment but type annotations are far from perfect a lot of code editors don't know how to recognize them accurately for example here we have a list of elements which will be a list of type string and what we're going to do is insert a and b and I forgot the equals and that's why it looks so funky and that's going to work just fine we're not going to get any errors or Warnings because we passed in the correct data type but if we were to pass in one and two we would immediately get a warning and py charm would just tell us that we expected a list of string but we inserted a list of integer but now this is where it's kind of broken as soon as we pass in a string such as a py charm is not going to be strong enough to tell us that we do not have the correct data types in the list all it's going to do is check that at least one is correct and then tell us whether it's inside the list or not and in my opinion this is broken Behavior I don't know how hard it is for a Cod code Editor to implement this but this is something that we do not want so in these cases we need to rely on external tools such as mypi which is a static type checker for Python and to use mypi we can just pip install mypie into our project and in py charm I have a special plugin that is compatible with mypi which actually highlights the wrong data types for example 1 and two are not supposed to be in this list so it's it's going to highlight those two and in casee you're curious about which plugin I'm using just go to your settings go to your Marketplace wherever that may be or we're actually there and all you have to do is search for something that's called mypi and every time you pip install mypi you're going to get these warnings otherwise if you are in any code editor and you don't want to work with plugins you can scan your script using mypi so in the terminal I'm going to type in my pi main.py and it's going to give us back the exact same warning such as list item zero has incompatible type of integer but we expected a string and it's going to say the same thing for list item 1 and as soon as we fix that and rerun mypi it's going to tell us that we have no issues in our source file and that's really nice because now we can actually check whether we inserted the wrong data type into a list and type annotations do not stop your code from running they are a tool solely used for the developer for example if we type in list of type integer and we print these elements python really couldn't care less but if we were to insert these elements into a function that expected the correct data type of course it's going to give us some funky Behavior back anyway let's move on to another example but this time we're going to use type annotations with functions so in this case we're going to have a madeup function called get data and that's going to return to us a dictionary of string and integer and what we will return is a and 1 and then B and 2 and just like that we gave python all the context it needed to understand what kind of data type we should return and what's great about this is that if we by accident were to return something else such as 10 we would get some syntax highlighting or sometimes you might even just print something instead of returning something and you will also get syntax highlighting for that because this function expected a return but what we did instead is return nothing or none since any function that does doesn't return anything returns none by default so this is a beautiful warning to have when you're working with functions it's just a friendly reminder that you forgot to do something important but now let's move on to an example that I use in a lot of my videos and a lot of you ask me why do I do this so let's create this function called greet people and that's going to take people of typ list of type integer and it's going to return none so a lot of you in the ments ask me why do I explicitly return none and the reason I return none explicitly is because it self documents this function as soon as I read it I know that this function was only meant to be executed I'm not going to waste time trying to get a result back from it I'm just going to run it and you're not required to do this but I really enjoy doing it because it just makes reading functions a lot faster for me anyway inside here we're going to type in for person in people and we're going to print hello that person and it's not giving me the correct context actions and that's because I accidentally inserted integer so we're going to change that to string and we're going to type in capitalize and give it an exclamation mark at the end and this is one huge benefit of using type annotations with functions it just gives you some really good context actions that help you code more smoothly otherwise without this type annotation first of all we have no idea what the hell people are supposed to to be is that supposed to be an object is it supposed to be the number of people is it supposed to be their names we have absolutely no idea what that is second if we were to try to use any context actions on this person we would get nothing back from our code editor because if we're confused imagine what the code editor is seeing so by telling python explicitly what this is such as a list of type string and if your person is an object you could do something such as list of type person you're python enough information to give you back appropriate context actions so once again since this is a list of string it's going to infer that person is a string so we can use all the string context actions with person not to mention that we also get the warnings when we pass in the wrong data types to our function here we passed in a list of type integer but what we really wanted to do is pass in Bob and James okay so now that we've covered the basic of type annotations we can finally get to the point of why do I type everything for example take this class called fruit the implementation here doesn't matter but the question is why do I type my fruit like this I mean isn't it obvious that fruit is a fruit and I can't even argue with this this looks incredibly clean and it's very easy to understand and yet I still opt in for this version because yes yes it might be obvious but what's most important to me is writing code consistently rather than having a thousand different special exceptions to how I type my code so if you're watching my tutorials you're not required to take this approach it doesn't give you any extra benefits it is solely for consistency and I get loads of comments from you guys telling me you're super secret crabby patty recipe for how you type your code and that's great I'm happy you found a system that works for you but personally no matter how obvious it may be I will type it because it's consistent and you don't need to explain anything other than I type everything to your colleagues there are no special rules that will lead to inconsistencies in how you type your code with your team and again that's the only reason I type everything now with that being said you are free to type your code however suits your needs best some of you tell me that you don't even bother with trying to type your code because you only use Python for prototyping and that's perfectly fine these tools are here to suit our needs and if your needs don't require it then there's no point in convincing you to type more personally I can't even imagine programming in Python without type annotations my code rarely fails because I know exactly what everything is at every single moment and that's my style of coding I don't mind spending a few extra seconds typing my code and my best analogy for type annotations in Python is the traffic light a traffic light doesn't physically stop your car it doesn't stop you as a driver from Crossing that intersection either it's only there to control traffic and to keep you from driving into other cars at the wrong moment it's essentially just a warning telling you that you might put yourself in danger if you drive on red so type annotations are practically the traffic light they don't do anything per se but they do warn you that what you're doing could potentially cause an accident and again type annotations are completely ignored by python when you execute your program anyway that just about covers everything I wanted to discuss in today's video I would love to hear your take on type annotations in the comment section down below but otherwise as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 28,686
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: Y9fT4HVdCuQ
Channel Id: undefined
Length: 11min 24sec (684 seconds)
Published: Thu Mar 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.