ALL Python 3.12's major typing improvements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
python 3.12 released exactly a week ago and you know what that means this entire month is going to have content dedicated to that apart from one video there's one video in the middle that's going to be different but the rest is all going to be python 3.12 related uh I put out a poll on my YouTube community and most of you wanted to see type far stuff or the type syntax stuff um most so I'm doing that first even though some type Checkers aren't readily available with this functionality yet my P hasn't caught up but pyite already knows what's up so I'm actually going to be using pyite in the CLI to be kind of showing and verifying everything so we're kind of going to go from least to most impactful SL important um because it also happens to go from least to most complicated so it's just the way it is um but yeah we're going to start with uh typ dicks quag unpacking we're then going to move on to the override decorator then we're going to move on to the type parameter syntax so let's uh begin if we create a new file we're just going to call this quags i guess. p and we're going to do from typing import not not yet I'm getting I'm already getting ahead of myself type dick and unpack we'll come back to the not required in a bit um and then we're going to define a profile we're going all those Imports for now and we're going to define a profile which takes various quags either string or integer so this is the kind of the old way of doing it for now so for KV in quags do items print just a simple little um utility to print quags um to the terminal in a way that is cool uh so if we do that and then we do profile uh yeah that's why I want profile so I just looking at my no what am I doing then and then my age is I'm 25 in my notes I say I'm 24 I forgot my birthday's kind gone since then bloody you know you're getting old you don't remember how old you are uh so if I do PI quags dop we'll see that it just prints you know it's a very simple utility function what I want to keep in mind is this string or int so what this is saying the moment and this is the way you do it in Python 3.11 and below is that your quags can be um of type string or int and that applies to any keyword argument There's no distinction as to which ones are strings and which ones are integers and this is where our type dict comes into play so we can do if I just kind of move this a bit over so we have a bit more real estate if we do class let's say attributes Let's do quags let's call it quags that seems potentially a bit more suitable we inherit from type dict and we just create a normal type dick I've already done a video on type dicks uh if you want to go see that that would be in the cards in the top right of your screen as you see it and now instead of this we can do uh unpack quag this is why we need to the unpack so what this is doing is it's taking the quags which is a type dick it's then unpacking it in of itself and then applying or I suppose mapping the unpacking to the actual quags themselves so now it would expect um a name and an age to be passed through and it would expect the name to be a string and the age to be an integer so if we run a pyate on this and at the moment I do have to specify that the python version is 3.12 I don't really know why that is maybe it's just the way that P set everything up but as you can see we get no errors if I were to set this to a string we suddenly get an error saying that literal 25 cannot be assigned to paret of age of type in so it's actually able to see that this is a type int it's able to you know do all the unpacking and everything and that's really cool if I were to get rid of age entirely it would then also say that you're missing the parameter age because currently by default it's set to be required now if you saw the typ dict video you will know that we can import not required and there is another way of doing this but I'm not going to bother showing that you can look at that type tip video if you want to see that this is the preferable way and you can set non-required in here so now we are requiring uh quag with name String name name uh key name value string there we go key age and in the second one key age that's not required of type int so if we do that after that abysmal explanation of what I was doing we can see that we now get no errors because it's not expecting one for age so if you were to put it back in it would and you can actually see the function signat is updated now which is cool then we also get no errors so that's all sorted that is how the quag system works uh it's quite nice I can I could sort of see a use case if you have a lot of keyword arguments um and you want to specify like a more detailed schema for these arguments um perhaps without polluting the code because code quality analyzers tend not to like huge suedes of arguments or parameters in there so it could be useful for that but I would say this is probably the least useful of the three which brings us to the second most useful or second least useful depending on your psychological Outlook there we go took a while to get to the word the override decorator so if I do override pi and then say for example you have a class called fruit and this this fruit can grow and when it does it talks to you cuz it's a bit weird like that uh this this lives in the world of Super Mario wonder and it says I have grown like that and then it has a similar method called eat uh which prints enjoy your meal and then you have a second class called ban which inherits from fruit which also inherits the grow method as it comes but then redefines or overrides the eat method to say Enjoy your pum instead now as it stands there's nothing wrong with this but what if you decided to change a method name in The Base Class to consume and then you forgot got to change it in the in the subass uh this eat would still be valid as a method in of itself but it would be uh now disconnected from the parent class so the banana and the fruit have different um behaviors and we don't want that it's going to cause you know crashes and all sorts so what we can do is we can do from typing import override and you know it's good because override isn't actually in theorder auto complete at the moment for PS code which is wonderful but it does exist and it does work which is good to know and then we can set um we say that this is an override of the eat and then we load pyate up again and that's the wrong file let's actually get it to use the correct file so override. Pi you can see we get an error saying method eat is marked as override uh but no base method of same name is present so it's now able to see oh we've reformatted our Base Class and we've forgotten to do it for all the children and now we can go and change this as well and voila we have no errors there was actually if you want this on earlier python versions there is actually a library on the python package index that's been able to do this for some time it marks its compatible versions I believe as 3.5 through 3.9 so it status for 3. 3.1 is unknown I don't know if it works or not haven't tried it myself um but you could you know quite easily in a requirements.txt file set an environment marker saying if python version is less than 3.12 then you know install this override decorator so you can use it across all versions of python it's very similar to like the toml lib that came out in 3.11 where there was a python package index package that could do it for some time uh but it just wasn't in the standard Library until then and this wasn't until now it's a good one to have it certainly you know I didn't know about the python package index one before all this um and it's just a really good like safety net to have in case you do ever do any braking changes or you need to do any breaking changes for whatever reason um just to absolutely make sure that everything is correct and in the right place the third and final thing that I want to talk about in this video is the type parameter syntax this is arguably you know the biggest typing change in this version but it's also the most complicated to explain which is why I've left it for last so if we were to from typing import type VAR generic iterable and iterator um and I'm going to be explaining this in the context of the older system so you can kind of understand how to change into the newer system from the older system it might make a bit more sense that way and if you were to have a class of immutable list and that inherits from generic TCO and then we Define a covariant type by doing this I set that as the name as TCO and covariant equals true uh and then we were to have a and I'll talk about covariant in a in a second that name like that nope item like that and that would be an iterable of TCO have self. items equals items and if we were to also create an iter for it self and then that returns none uh we then return it self do items this is our mutable list class it is kind of a a very simple implementation of a list that you can't change basically but you can iterate through which is cool so once you set the list you can't change it or anything um we then have say your class employee and it's at this point that people some people might realize that this is a modified example that I'm kind of taking from Pep 484 but I've modified it to be more useful in this context uh so we just set a name like that and if we set manager employee and then we set that as just the same thing so what you mean by co-variance here is that we have a class called employee and then we have a subclass manager at any given point in time class manager may or may not use any interfaces provided by it super type which in this case is employee because you know the manager could potentially override something something and it could you know do something different maybe it breaks the list C substitution principle hopefully if you're using the typing Library it probably wouldn't um but it could we don't know which is why the two are considered invariant unless you explicitly specify otherwise and covariance is one of the ways of doing that if two types are covariant the subtype should be able to use everything that the super type can and will you know be perfectly compatible with the super type so what that means here is that the manager class um will be able to use everything from the employee class and it hasn't like changed or broken anything Contra variance is the other way around but we don't need to talk about that in this video I'm probably going to have a more detailed explanation of these typing contexts in a future video this is mainly for people that kind of already know what they're talking about but I'm providing these explanations um for people that might not know just kind of at a base level hopefully that makes sense uh so if name equals Main and we have a rung guard down here and then we set people as a list of a mutable list and then we provide a list called employee uh John and then his manager Jane um so here we're passing employee and manager the immutable list so all the items in the immutable list and therefore how you iterate through everything you do with each item in this list should be able to do all of the same things um which is where you know this covariance comes from if you to do that and then if we ran pyite we would run into an error how have I done that it's not supposed to happen oh here we go this iter is supposed to do an iterator of TCO there you go this an iterator and it returns an iterator of our type of the items and now if you want by right we should get no errors at all there you go that is the beauty I did that on purpose that is the beauty of type Checkers they can find errors like that you wouldn't otherwise have known so this is a very basic example of a generic class how we can do that in the new python system is by not importing type VAR at all not importing generic at all getting rid of that completely and instead of using oh bloody Nora instead of using um normal brackets we can use square brackets and provide t.co in here um or you can provide this as T if you want it doesn't matter you can provide this as anything um but this functions exactly the same as it did before you don't even have to explicitly specify that the type V is covariant it seemingly according to P 695 just knows which is quite convenient um it doesn't have any bounding parameters automatically but I'll come to that in a second and you can see um so anything that's defined in here will then be used here and here and this TCO is scoped to this class so if you to use it somewhere else it would be a different type VAR so you don't have to you know Define a billion type vars to be able to use it which is quite nice and if we were to load um pirate again we can see that we have no errors so it's all well and good if we were to provide uh the number four here we'd still get no errors but if we were to say do four um you know person in people and then print person uh do name uh then we would get an error because uh um the type Checker is able to realize that uh the our integer doesn't have a DOT name which is fine it's great but what if there was another way what if you only wanted your immutable list to be able to accept objects of type employee or more specific in the first place well you could do that just by doing this so This colon and then um our class type here here replaces the bound keyword argument of type bars and now if I were to run it again we'd get a different error saying argument of type list employee there there cannot be assigned to parameter items of type thingy because type literal 4 cannot be assigned to type employee and Pi almost a pie charm then um P right is able to see that TCO is bound to employee and so must be employee or more specific which the number four is not so that's how you can do bounds it's a much nicer syntax than having to do coant equals true and bound equals string and all that jazz um so this can be applied to functions as well so if you were to create a function down here and we would to say just get the first out of our employee list and we would have provide a type T and then we would do sequence of iterable T and then return T return list sequence and then the zeroth one so this function first defines a new type bar of name t and then is then able to accept an iterable of you know type T and then Returns the first in the iterable which will be type T um and you can do that just by using this uh square bracket syntax here so if I want to do print first people uh. name like this we'll see that we get no errors and we also are able to get the first one in the list which is John in this instance we can also bound or bind this here so we can say that we need it to be a list of employee only um and if we did say first of one or two oops ignore that runtime error uh then we get uh another typing error with exactly the same stuff as before saying that lital one can you know isn't employee in lital 2 is an employee because the type is bound to this if we were to do this then we get no errors at all or we would because we still have this name we to do this we get no errors and P parameter would take one so this is now a more generic function than it was before because we haven't bound it to anything if we wanted it to only be usable with employees then we can bind it to employees and then the type Checker would be able to pick something up um in a more specific fashion than if you would have just kind of let it do its thing or if you would have just let the program run so that is a general overview of the big typing changes in Python 3.12 if you like the video then make sure to leave a like to let me know and maybe subscribe if you want to see more videos like this including further 3.12 videos and python 3.13 plans video coming out very soon if you have any questions or if you have any ideas for future videos make sure to leave them in the comment section below I read every single one so your feedback is greatly appreciated if you want to support this channel monetarily you can do so by becoming a member or a patron using the links in the buttons below1 a month on either and you can be on this screen like these people and I will see you in the next next video where we will probably be talking in more depth about the Gill and what python 3.12 does with it so if you're interested with that make sure to stop by either next week or some point over the month and uh I'll see you for that
Info
Channel: Carberra
Views: 8,248
Rating: undefined out of 5
Keywords: pyfhon, pytho, pytbon, pytjon, ptyhon, pytyon, ptthon, pyyhon, pythn, pythoh, pythpn, ython, pytgon, pyhon, pytohn, phthon, oython, pthon, pyghon, pythoj, pythno, pythkn, ypthon, pytuon, lython, pyrhon, pythom, pythob, puthon, pgthon, python, pyhton, pythln, pythin, pytnon, pyton
Id: jA5L0GDrVqs
Channel Id: undefined
Length: 20min 16sec (1216 seconds)
Published: Mon Oct 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.