Smarter FastAPI Through Tooling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to today's webinar organized by jetbrains i'm pie charm i'm paul everett pie charm developer advocate i'll be your host for today's webinar on smarter fast api through tooling i've been looking forward to this window for a while we're joined today by sebastian ramirez someone with a lot of years of fast api experience i make that joke because he got a tweet from a recruiter asking for four years of fast api experience it was two years old sebastian is the creator of fast api creator of typer creator sql model senior software engineer at forethought i think the keynote speaker at every python conference for the last six months a genuinely wonderful and kind person welcome sebastian thank you very much that was such an introduction thank you paul thank you everyone thank you bye charm and jed prince for having me here it's a pleasure to be here it's going to be a pleasure for us as we crank through this but wait paul you may say there's another face on the screen we're also joined by alexey nyazov pycharm core developer creator of our recent in my recent i mean recent fast api support along with the endpoints integration that we're going to be showing today smart nice funny welcome lexi hi everyone sebastian nice to have us nice to have you here by the way everyone alexei has a job today to keep me out of the ditch both on correcting my mistakes but also telling me faster slower whatever it is let's do some framing sebastian this webinar is focused on the word smarter this morning i was rereading a real python interview with you i recommend that everyone go and read that it's really good where you discussed your experience kind of as a front-end developer the developer experience over in that side of the world with typescript and all these kinds of things and how you wanted things to be in python back-end development full stack development whatever you want to call it can you explain that idea a little bit more yeah absolutely so the uh i don't know through the years i got the chance to work in a bunch of different projects very strange and diverse with python and with some front-end javascript typescript uh even electron apps and stuff like that and a bunch of data science and data processing and machine learning whatever the thing is that seeing all these languages and all the features that they provide i got the chance to realize that the type annotations in whatever the language it is are the thing that provides auto completion and i love about the completion i live for outer completion like it does that's one of the main things i use to select the technologies that i use or the ones that i built is just like together to competition as much as i can and also inline errors in the editor funny thing is that the funny thing is that these type annotations and this this supports provided by editors is normally called static analysis and things like that that sounds super sophisticated and like this is only for like i don't know very very very important computer scientist somewhere else and it actually refers to the auto completion and the inline errors that i really really like so i wanted to bring all that developer experience to python i wanted to be able to have that but there was not an easy way and then python 3.6 came with type annotations for python allowing us to have these optional type annotations and it doesn't even we don't even need to compile python like we have to compile typescript to javascript or something like that it's just like the same python but it has support for this type annotations and then suddenly we can get this great editor support everywhere and that was the idea to get these type annotations and bring them up a notch to have a lot of extra features on top of just of the support that we could have from the editors also have data validation data documentation serialization and everything based on standards and all the stuff so that's what i wanted to have in python and import those features and that developer experience from other areas as well i'm totally with you on that my other day job is web storm developer advocate and so i make these courses and things about fail faster because the typescript experience and the frameworks that embrace it will red squiggly you immediately as soon as you make a mistake alexei for um developer experience dx i view it as two sides of a handshake on one side the language and the frameworks need to provide some structure to the tooling and then on the other side the tooling needs to take that structure and then make the development experience better does that sound right and is it static analysis i mean you know type hints etc is that the way it happens yeah that's exactly right actually uh sebastian said it best uh type hints are what gives you under completion that's that's just perfect so i mean uh as the creators of tools like python we will try to provide the developer experience but if um and we don't do anything magical by the way we're basically reading the source code and uh giving you uh all the help we can we can give but to do that we have to understand the code and by virtue of python being very dynamic language it's possible to do a lot of things in a way that's really hard to understand statically without running this code but when you have a lot of type annotations and structure in the framework itself it makes my job so much easy so i'm on board i mean specifically on that point for the previous generations of frameworks that we kind of provide deep support for django flask pyramid etc uh in many cases you have to go look for like magic variable names you have to teach our tool all the weird run timey things which limits the number of tools you can support and so with fast api i don't think it was that big of a leap for you right uh i wouldn't even call it a leap so even before we added the any support for fast api you could uh you could use it in pycharm and the support already was pretty good and that all thanks thanks to sebastian and the extensive type hinting in the library itself and sebastian i think you've written and said repeatedly that was your goal do things using the standards so that you know if it's pycharm or vs code or mypi or any of these other things they're just gonna be able to work yeah in fact when i was building fast api before i wrote any of the internal code like it was a long process and a bunch of understanding standards and adding pull requests to the supporting libraries and things like that but when i was about to write the actual internal code fast api before writing anything i wrote how i wanted it to interact like how i wanted it to feel working with it in python and visual studio code like and in the main editors i wanted to have the best support possible before even writing any of the core actual code so i i first fixed the design of how to use it and how to how i wanted the developer experience to be and making sure that it will work great in in in the main editors in python visual studio and the other ones based on jedi uh and then after i knew that it would work great in all of them i then wrote the code to read and to use all the information that i was i had decided to to put in that like sort of api for the developers as someone uh my side project for the last two years has been trying to replace templating with kind of component style component driven development to get that same experience totally agree with you it really makes a big difference so with all this setup done let's now get on with the slides so switch back over to my slides view all right as we go through this there's going to be questions that get asked and as you have these questions go ahead and ask them don't wait until the end over here in the chat window for youtube we've got some of the pycharm folks we've got alexei we got sebastian sometimes you'll see me turning my head looking over and what we'll do is we'll take some of the questions and gather them up and go over them during little breaks in the demonstration but some of them we're just going to answer immediately i make a joke always the first question is is this being recorded i've given a trillion webinars and a trillion plus one times someone asked this question usually five minutes after i say these words so yes this is being recorded it'll be available essentially immediately because this is youtube and we will also announce it over on our blog all right we also have some material that this is based on um we this demo is based on the sql model tutorial i've got a link actually let me just get out of this and bring over my window another one i meant to put this in the slides and i didn't get them in in time uh at this url which alexa will paste into chat are all of the demo notes that i have for today's demo uh that you can follow along and it's got working code at each step this is based on a tutorial that sebastian wrote for sql model fantastic tutorial uh so if you go to the sql model website for the docs and go to tutorial you can walk through all this in particular the part at the end about fast api and pedantic now i've also adopted this a little bit from michael kennedy's talk python course and he's got a course on modern apis with fast api wonderful course a lot of material in it and i took some of the ideas from it and put it into how i present this material here and then one other thing look for maybe the end of november the beginning of december on the pie charm guide site mukul montage who's been doing uh incredible video demo courses for us he's working on one to do aws and fast api and kubernetes oh my goodness the amount of work he's doing to you your food for you when this thing comes out it's going to be absolutely amazing i've been working with him each week on all of his checkpoints along the way so that's the material that we've got in place for this let's go ahead and start i will move this back off the screen and then you can go back into presentation mode and i will slide you over to here good okay uh with this in mind yes we are no i didn't want to i clicked one click too many the difference between a swipe and a click all right we're back and let me get my instructions set up over here all right i'm going to begin uh in that uh link that alexei pasted in there i'm going to begin on make a project and what we're going to start we're going to start at the very beginning we're going to show stuff that was just added in the latest eap for fast api and as we go along i'm going to stop occasionally and we're going to talk about the word smarter this handshake between a framework that provides structure usually via tie panning and static analysis and then on the other side a tool in this case pycharm but we might also look at my pie that takes advantage of that and can tell you and help you and give you coding assistance so i'm going to start on the uh welcome to pax arm screen for the record i'm sure i'm using the purple theme for the very first time i've always done light theme and i have to look over and say why aren't you using a dark theme so i'm using a dark theme so you'll have to accept that i also have my id set up in a minimalist way without tabs and stuff like that so i'm going to create a new project and i'm going to say that this is a fast api project alexa added this i think in the previous eap some of the stuff you're going to see is from the most recent eap one of the things that you're going to see from the most recent eap is this we now have bundled uh the poetry plugin we had collaborated with anno and now moved it into the open source repository for pycharm so this is going to be a sample project for fast api based on poetry and i'll go ahead and kick it off the choice of poetry is kind of interesting it's a little bit bleeding edge but it's um getting so much response from the pycharm community so i thought i'd go ahead and do it for this demo uh sebastian how much poetry are you saying i would recommend poetry for for new projects i will use poetry in fact sql model uses the alpha version of the newest thing with poetry and i will use poetry for any any any new projects i'm not using poetry for fast api and the typer because i started with something else but i would just go ahead with poetry all right i dragged this over because it i'm in a multi-monitor setup all right and so the what the project is doing is it's creating a poetry uh it's creating a new pie chart project with a poetry interpreter and some sample code in it that lets you uh goes ahead and makes a pi project.tamil file that has the dependencies it's going and getting the dependencies and uh has some sample code in it a sample project it's going to have a run configuration various kinds of things so i don't know why it's not just using the maybe there was a new unicorn sense this morning i don't know but it's not using the cash version so this is taking a little bit longer uh sebastian as things like fast api get adopted we talked about smarter and talks about better tooling one of the things that happened in the front end world was jsx and components just got so big that everyone had to do really good support for it and so fast api is becoming really big maybe it's having a similar effect that the tooling has to give a good experience for it do you buy that i yeah like one of the thing is that fast api was optimized for the best developer experience but on the other side fast api is used uses all the main standards and the typing standards and tries to take advantage of all these standards and not use anything that is very dynamic or strange and the good thing is that as as i keep using more the standard type annotations and for example it's important and add support for new type annotation features and the other editors as well then these editors like pycharm and other editors end up supporting more just general python and the standard python and that's amazing because it's something that is not only a for fast api that benefits any other python project that is just using similar types so i think that's amazing that that i don't know as you were saying that partnership between tools and a code basis through the standards it's awesome i'm going to follow up with you about that and get the two of you to talk about pi project dot tomml as a convenient place to park the port number but we'll get to that okay what do we have we have a fast api project with the main.pi and it made a poetry environment for me based on 3.9.7 which was what i had as my default and it created a run configuration what does this mean part of our support for a particular project is to go ahead and create out of the box run configurations for things like django things like flask and now for uh for fast api and one of the things is it it seems to be pretty much settled that the developer experience is going to be unicorn so alexei went ahead and wired up uvicorn support and you don't even have to uh point at the uvicorn executable you just say what is your main.pie and then it will do the uvicorn for you in the background couple of other things like an option here and a link to go see some more things so i'm going to go ahead and run this and we're going to bring up the command that's run and alexa you're going to talk a little bit about what we discussed yesterday with the sniffing at the app so this is the command that was actually run and if you look in here we don't import uvicorn we don't have a main down here that launches uvacorn or anything like that how do how do we know what is app well that's uh that's the static analysis you saw you uh talked about before so uh if we know where to look we know what to look for we can find it so if you just point us to the file you're using for your fast api project we'll find the name of your ipad put it in for you so no need to right i mean we didn't put what is the name of your app here we discovered it by looking in main.pi for something that had a type of fast api is that correct yeah that's so cool so so it's not faced to the name of the variable but it's actually checking the actual class the actual instance that's very cool okay you can name it anything you like i freaking love this my previous webinar with will mcgugan we had a little bit of an argument about convention over configuration and i just dislike magically named symbols i prefer structure and he actually does a good job in textual of going and sniffing at types to know which things are reactive for example rather than prefixing the name with a reactive under whatever so that worked well we've got it launched and what we're launching is uvicorn that is an asgi server which i'll get sebastian to explain what those are and how standards like that help support this story about the handshake and plugging things together sebastian sure so let's start with the the roots the main thing that you could have heard about before this asgi or asgi standard will be whiskey or wws gi i'll type that ah thank you so this wuzky standard this is a it's actually part of the python standards uh it was the standard that was made a long time ago to define how a server will communicate with an application and then when you say like several applications like what is the difference between those things like what does that mean it normally means that you have a server on one side is one a one application one one binary one program it will be for example unicorn or something like that this program will be able to connect to a port is a machine and this program will be able to speak the http language and to understand it and then this program will convert what is all that communication and will convert that to some dictionary saying like hey someone is asking for this path and he's sending this data and it will put that dictionary inside of some function and now that function that will receive the dictionary that function is provided by the application that's that's all the difference between the a woodski server and the waste application the server will normally be unicorn the application would normally be an instance of jungle or an instance of flask or something like that so that's woozly then when python came with async and a weight and we suddenly had all these additional features and advantages in terms of concurrency and throughput and we can have a very powerful servers that can can work can have performance in terms of throughput on part with gold and or node.js or things like that then this standard that was depending on functions calling our stuff uh was a was not enough for this async and await things so there was this new standard ascii which is just the same thing but using async and wait and then it just creates a dictionary with the data from the http request and a couple of functions that are passed to the application and now the application will be something like fast api or other other frameworks and these applications will use those async functions to receive the data and to send the data and that's that's that's the whole point this means that you can use ubicom with other ascii applications and you can use other server programs with fast api as well so you can mix and match that for example hypercorn has http 3 support exactly exactly so there are different tools that you can throw up yep i'll go ahead and open this in a browser i'll click on this link and open it and we see the message from going to slash the message is basically message look at that so it's returning a dictionary and on your side in sebastian and fast api you set the application json mime type and blah blah blah and do all the right things but there's also this other aspect i want you to explain which is open api integration can you explain what that is a little bit just keep it really short and how it's part of this if we adopt standards we can have smarter tool chains yeah so this ascii thing is the standard for communicating with the application and the under on the server now fast api is built on on top of standards this is another standard that is called open api which is to define how an api works it's just a the end result is just a json file that has all the paths that are available with all the data shapes that it will receive or return and then open api is also integrated with a bunch of tools this web user interface that is documented it is documenting the api that we have here is generated by a tool called swagger ui swagger ui has no idea about fast api but it knows open api and because fast api is based on top of open api and is built around open api then we can have this automatic integration with swagger ui which means that in the end you write code using just the standard type annotations and you declare the data shapes with standard type annotations and fast api generates this provides you this automatic api documentation it's really fascinating that this much code can generate enough information in its structure to drive open api documentation of your project uh so let's do a little bit of discussion and then hurry on to the next part um discussions about dot pineproject.toml uh sebastian do you have any plans to put any configuration knobs into pi project automo uh not for fast api for now because there are not many things to be configured in fast api but it's very easy to write some code that will read from my project.tunnel and then pass those configurations to fast api but it's not something that i want to have integrated by defaulting fast api i would rather explain how to do it than to have some custom logic and to explain how to use fast api instead just i prefer to explain how to use the normal tools all right let's go on to the next step and this is where i get on my soapbox a little bit i've got a big tutorial about um front end development where you traditionally you write some react code you go over the browser hit reload or maybe it's already reload but you click click click open console log go back over to your tool forgot where you were and what you were doing repeat so i like staying in your tool your smart editor your ide and not leaving to go to the terminal to run stuff and the browser to look at stuff instead use tests to consume the results of your work not because i want you to eat your vegetables i'm a test first developer not test driven developer not that interested in quality i just want more joy and and doing my development in tests keep me in the flow help me to fail faster makes things more joyful uh reloading chrome does not spark joy so let's show a little bit about how this structure can help us switch over to that test first approach one of the very first things is what alexi has done for us he's generated something called a dot http file what is this if i created a new file it would be one of the file types that we could create dot http this is postman in a file that's the way you should think of it and you can put it under version control and you can do autocomplete and all those other things and i can walk up to this and i can say all right run all the tests two of them or i can walk up to one of them and run just one of them and it's going to issue these requests and show me some stuff about the output and i could click on one of these things if i wanted to to open the json result and so that's a really cool way of working staying in this but what if what if pycharm professional included something called endpoints support in the most recent eap where on the consumer side we knew the structure of the back end and we could do things like app so i can autocomplete and i can type user etc let's lexi i'll pause there real quick and talk let you talk briefly about endpoints and what we're trying to do with structure and smarter yeah uh so basically when you have a framework frameworks like fast api that uh tend to operate not only with source code but with some type of endpoints be that urls for example uh we need some form of navigation for those and some smart assistants for urls as well we can't operate just by function names for example because uh they become irrelevant in that context so um we did the thing as we read what you put in your fast api project extract the urls from there and then think about where we can provide the support for this so basically http file is just one example of this you can have completion for all your urls you can have navigation and some other useful things for example i could move my configuration values to a separate file called an environment and in here i know that i've got a name so it's going to supply that i'd already typed a little bit of it and if i do if i'm on the other side i can get a little bit of help from structure by saying refactor rename and maybe i just want the first name not the not the name paul here no i think you you meant to go for hello okay sorry so if i want to refactor this and say rename to howdy you mean this yes all right great and so then if i'm on the other side i see i get the refactor rename there on this side and we're still working all the places the structures happen this symbol appears here and here and here if i change my name my mind and i want to call this first name it will change all three of those if i change my mind like damn that was dumb i can just do undo the editor transaction it'll go and find everywhere in my project that was affected by that refactoring and undo it and the thing that's kind of interesting to me about this is we were talking about sebastian's kind of view about better developer experience through structure through type hinting this isn't a type hint but we were able to teach it to be indexed as if it was as if it was providing structures and symbols and things like that and once we do that we're off to the races on what we can do uh what smart tools can do you can get auto complete you can get red squigglies you can get refactory names you can do navigation forwards and backwards find all the usages of something so for example if i want to find all the usages of this i can just click on it and it's in the middle of a string that's not a symbol wrong we found it it's a symbol to us and we can look through all the places in the code where something appears nice that's super cool because this one is this this is one of the places that really don't have a way to have like a standard from python like this this has to be deep integration with the ether that's super cool it is and we would like you over there in chat ricky ticci just said dang that's cool as heck yes ricky ticci that's cool as heck and alexis like two weeks into this so if you've got any ideas out there about how to spend alexei's time reach out to him maybe in chat or bug him somewhere else what can we do with this structure what weird new ideas like endpoints can we do with this structure to put it to work okay i'll speed up a little bit um it was it was kind of a drag to have to uh go look through the entire response i'd like to be warned if the response wasn't the right thing so in this case it'll be the right thing because it's a 200. oh i did the wrong one i wanted to run this i'll run just this one okay so that worked well it's 200 but what if it was 300 i'll run it again and in this case my test is marked as failing and if i ran all the tests you'd see it a little bit better i don't do that i run all the tests you'll see that one pass one failed and then this can just be a really simple way to avoid going to the browser writing little assert like things in a postman style language but you're still in the ide so we can help you with auto complete and things like that lex anything you want to add to that no so um alexis added some things to improve developer experience i won't show them uh just yet but for example these intentions to write one of those things in the http file for you and go ahead and generate some of the boilerplate or go ahead and open it in a web browser if you want to punt out of the ide so we're working on some ways to put all this stuff to work okay over to test first development and i'm going to really emphasize test first development by stopping the server not only do i not need chrome i don't even need fast api server and what i'm going to do first is i'm going to install i'll do it from the command line i'm going to install uh via poetry pi test and it's going to go off and start doing its thing i'm going to make a new folder for tests but what i'll actually do is make a file for comp test since we're going to want that in a little bit and once i have poetry once i have pi test installed how can i work smarter using the structure stuff like that i can walk up to one of my in one of my endpoints and i can navigate to the test for this but wait there's no test for this well can you create a test for me name the file test under main and make a new function called test under root i wasn't i didn't give it enough time to realize that pi test was installed so integrated i need to say auto detect pi tests no pi test runner fix i installed it lexi did you see where i might have gotten that wrong not really i go back to my terminal i think poetry is still installing pie test those problems okay yes i see down here yep you're right so i'll give that a little bit of time to keep going and what i'm going to do is instead of i like to do tests and so what i do is i split my screen i don't do tabs i split my screen and on one side i do my code that i'm testing and on the other side the test code and then at the bottom i'll have the test output yeah that's what was happening ah poetry love you but your resolver there we go all right okay you can keep going and what i can do is walk up to one of these generate a test tell it to put it in there and it writes a test for me and i'll go ahead and run this failing test actually what i'll do is i'll run everything in the tests folder just in case i add some tests later and my tests uh are gonna fail because false is false and what i like to do is put these on autopilot so i'm gonna say go ahead and run whenever anything changes and i would like to wait two seconds and you don't actually have to save we know when things have changed so if i come up here run it one more time so that the auto delay kicks in so it's run and if i'm like oh yeah true false is not true i didn't save it two seconds later it re-ran and passed so great now i need to write a test that is uh unique to the fast api way of testing news flash sebastian's docs are so freaking good not just for sql model but for fast api he explains this really well he uses test client from start so i'm going to write client equal test client auto complete the app app and auto complete that from main so now that i've got a client i can do just the shallowest little test i'm going to say response equals client.get and slash and i look and i'm like okay response says it's any that's not the right thing what's going wrong here it's because it gives us a little hint on that it's because i haven't installed requests test client depends on requests behind the scenes to give you the api for this so once it's installed i guess i should not trust that it will be fast yep once it's installed then i get the autocomplete all right good and we now know that response is a response and i can say great did it work i want to assert that response dot status code ends up wait a couple of seconds test run test pass okay great uh i guess if i wanted to i could also assert that the json was correct so assert response.json and it's got something called name in it and it's going to be all right and i'll wait a second for the test to run tests failed why did they fail let's go back to test route and it doesn't have name it's message i'm going to pretend i did that on purpose because i have a complaint sebastian and actually i have a complaint for both of you on the handshake it's not actually your fault it's going to be request's fault but this is returning addict and later on if we have time we're going to do response model and stuff like that and we'll know that it's a list of heroes or something like that but i can't really write my test to assert that because it's going to be addict i'm going to lose all the information about at the actual function it would be cool if the response in addition to json had the equivalent of assert response dot model something like that zero dot message or in this case response.model.message i would love to have fail faster schema structure auto complete red squiggles when riding my tests what do you think that's an interesting idea the thing that i see is that there is not going to be a way to add the type annotations for the response object given only the path uh the the the root path the string but still like yeah it's a very interesting idea and it's it's uh similar and close to some things that i have been thinking about how to write automatic clients with autocompletion but yeah right i might have to do something that's like this and manually put the annotation on it because we can't transport it back but it would still be nice to get the the actual result of the function rather than the wrapped jsonified thing yeah yeah that's an interesting that's an interesting idea the other thing is that i like the the idea of just having the very pure json result that is actually not completely dependent uh of on fast api is just like the same this is very similar to testing any other api in any other thing that is just a dictionary but yeah that's that's interesting question yeah it's it's nice because you can have ci on your tests that tell you if you violated the contract of the structure on the other side yeah absolutely um alexei do you want to talk at all about code inference on these kinds of things well uh most of the code inference is done via the type annotations we uh we will try to infer something if segmentations are missing but they are a great help so uh if um when talking about which uh what do you mean if uh that entirely depends on on the library if sebastian decides that that's something he wants to implement we'll implement as well it would be interesting to see how we could transport that information uh we're probably not going to get to it today but you mark these with an attribute called response model that tells consumers of this what is the type information of the thing that's being returned it would be cool if we could transport that somehow you know this is also closely related to something that i have been thinking about of a finding a way to write automatically generated clients that communicate with facilitator and there are several tools uh but now with the new standards for declaring a type annotations for dictionaries then it's going to be a lot easier to for example declare the request to send to the server and just instead of having to import some model from somewhere and create an instance uh if we can just declare the dictionary and have a have a editor support for that particular dictionary from the auto generated client that that's going to be amazing but yeah i don't know that that's for the future plans sure sure so i'll finish this off and alexa if you could look back through chat and see if you see any questions or topics that we should discuss while i finish this i wrote a live template called fat for fast api test and i can just fill in the blanks and say say hello um get a hello from someone get slash hello slash user and i want to check that the results a message equals uh hello user and i kind of wrote this in my pi compatible test where they want you to put all the response things and stuff like that let's see what my failure was so say hello failed because client isn't available that's actually for the next for the sql model part of it okay um i'm going to stop here for a second and we will talk a little bit more actually no i'm going to i'm going to go on to endpoints and we'll talk a little bit more see how we're doing on time one of the the bigger things that alexa did to help us with all of our work on endpoints is uh oh because i changed the name of this to howdy see this is why we write tests everybody we have a new tool called endpoints let's see alexi where would my tool be that's a good question can you see the window all right i think this is related to the way i opened it in the background and canceled something on the way up so we'll skip that part um as we express these things on either side of the handshake we have a chance for my pie to get involved and i know sebastian this has been a big deal for you come on terminal yeah a big deal for you trying to make structures that survive the my pie gauntlet because sometimes things that should work don't and things that shouldn't work do and in fact you helped champion one of the big things about deferred annotations i think you also championed a big thing about data about alternate data classes being treated as data classes let's run my pi on fast api code see how sebastian did i'm going to run it on main this is the first time i'm running and i'm not doing the demon it's going to be a little bit slow it helps all of you appreciate how fast pycharm can be a type analysis am i right the other thing is that in in most cases if my was going to fail pychamp will also detect the errors and will show the reds quickly line so but by the fact that we don't have red squiggly lines we know that sure sure okay so i'll run it on test under main as well and i'm not really running it in strict mode if i was running in strict mode it would have gotten mad about not having a return type on the on the function here but it goes to show that without having to sebastian i don't think there was any special my pie stuff you needed for this level of static analysis would you agree with that yeah no like when there was a decision to make something that would require some type of plug-in or something like that i changed the design to make it uh work well with my so that it will work well with editors and with everything uh has there ever been any talk about like any extensions to my pie that you had to write to get any things i had to write extensions for my pie at some point for it was actually for thing the library that goes underneath spacey the natural language packet ah okay but yeah and like writing extensions for my file is not as simple and easy because it's not the development process for writing extensions is not very well documented and then you have the the the support in the my pi tools but then you don't have auto completion in pytharm or here sure um i'm going to actually i'll stop there for a second alexa how do you feel about the discussion about there are multiple type checkers out there my pi's one pyre is one you know google dropbox facebook people have different type checkers and we've had a type checker for python for a very long time uh open source uh do you feel like the state of type checking in the world of python is consistent enough for this vision that sebastian's having about smarter developer experience through tooling well yeah i would say so uh because while some things are supported here or not there most of the important things are supported all across the type type checkers and sebastian by designing a fast api this way makes it a type checker agnostic so it should work across all the type checkers and provide the the tooling support yeah yeah uh do you have anything in chat that we needed to bring up people suggested uh christian mueller suggested uh reading the response model from the decorator about around the time you talked about type checking in your test lines and that's a that's a nice idea that's something i've been thinking about yeah so i've got what i did was i teleported us into the future since we are unsurprisingly running comically out of time to kind of the end point of sebastian's tutorial for sql model and we'll talk about that for a second but in particular on what alexis is discussing here is the response model uh sebastian what's the purpose of the response model i know the answer i somewhat disagree the idea of the response model is to do data validation and data documentation and serialization on the output of the on the response date so by declaring here that this is a list of hero and this is using the same syntax as we use for normal type annotations we are telling facility hey we are going to return a list of these instances of hero which is this model that we created somewhere then fast api will do data filtering using this model so for example if the hero object had for example a password saved in the database and we have some different some different models that we are returning then it will filter using this model to not include the password or things like that and is also going to do data serialization data conversion using this so if we return an object that is just an instance of something from the database fast api will use this class to extract the data and put it in the json contents and it will be automatically included in the automatic documentation and open api and you can't do it here where you would expect to do it because i suspect the decorator wraps it to mean that that's not actually the type the thing is that it's actually it will actually be possible like it's uh it's something that could be done and it's it's one of those common questions why is it not there in the normal place there's a ticket yeah yeah and i i think there are several people come back again and ask the same question again but the thing is that here we are returning heroes but let's say that we have a different model in the database that is hero with password or something like that if we return heroes and heroes is a list of heroes with password and then we annotate the function as returning a list of heroes without password then uh the tooling my pie chart and other tools will complain hey you're returning something that is not a list of heroes without password you're returning something that is a list of heroes with password uh so so that's that's the whole reason why that that is there because it's actually not a it's not one-to-one the thing that we are returning and the thing that we are declaring the response model because a facility doesn't use it only for documentation it uses it for data validation and serialization and data filtering perfect uh one point to bring up alexi just commented to me about this uh in this project the endpoints tab is working and what it does this is one of the truly magical things that he added kind of taking all that structure that you're expressing about endpoints and putting them into a developer experience putting a gui on it so you get a listing of everything that alexi thinks is an endpoint and in this project it's things with the app decorator or you support the route decorate router decorator right i support all the http routing like get post uh options no i mean um if you make an instance of router oh yeah uh api routers will be included as well api router yep nice that's pretty cool and i can go to these and i can see the listing of everything that i've got in this project i can go get documentation i can look and say hey wait a second that thing doesn't have a doc string so i will add a doc string in the magical rest syntax and then when i come back to it in fetch documentation i see that i see all the type information i can walk up to these and if i don't want to manage a separate http file for all of these if i want them to be closer to the actual endpoint i can do the postman style rest requests while sitting on the endpoint and that's actually pretty usain associated with each other and so this um this listing of endpoints is visualizing all those connection kinds of things that he's doing behind the scenes so these things start to look like structures start to look like symbols uh i've teleported us and i'll do this in the last couple of minutes um i saw a question over here about django sebastian i suspect somebody could run fast api and django in the same process as different ascii mount points with um django doing async now presumably you could run django under unicorn you ever got anything uh so yeah like actually the ascii specification came from django for the channels project so recent versions can can work like that but you can also mount a woodski application like django or flask inside the fast api and it will run under under like some specific endpoint or something like that so you could for example mount a mount flask with a flask admin or something inside of the same fast api application that's going to be interesting to see how people mix and match those things and then find out places where the sql alchemy engine gets really mad about that all right so uh i've teleported to the end of the sql model demo and so i don't have time unfortunately to talk about sql model but the basic idea is you before you would in fast api you would write your models in pythanic and then you would have to write them again in sql alchemy sebastian is that correct yep if you were using sql automatically for databases that would be the most common use case yeah which is one model too many wouldn't it be great sebastian from four months ago thought if i could just do some really deep voodoo in sql alchemy and pydantic both of which are little islands of their own voodoo and i don't know if this is arithmetic or exponential but the voodoo quotient had to have gotten out of control and he set it up so that you can write a very pidantic looking thingy that also has some database thingies in it as well one model to rule them all and it will generate tables and all that sql alchemy stuff for you is that a good summary yeah that's a great summary um i will i'll poke at the pie charm guy a little bit i did this yesterday so he knows it's coming for those of you out in python land you might be like huh what's that keyword argument doing where my super classes are usually located that's not possible is it in fact it is it's a dunder in it subclass kind of thing and in the let's see i think i gotta go to it in the super class maybe it is i'll wind up going and chasing yeah where is the spot where you look for the um those are those config arguments i think it's in the in the meta class in the sql mode meta class the sql model meta class so oh this one yes sorry yep i was looking at the wrong thing so this takes some of that information that was passed along in as extra [Music] don't know arguments what place where it is but yeah i thought it was in another yet under subclass is it in the dunder new i don't know if it's in the thunder new orleans so there's a place where um you define these things that have arguments and you can pass them at this level we unfortunately can't sniff that far back and help you but alexi knows about it and that's something that we could provide a little bit of structure and tight completion to so on to finishing up with sql model uh we have something that we call from our main.pie and we call it during the startup event the startup event and that goes and creates i i put this thing in a check if it exists otherwise create some data but we create an engine and we set it up with sql alchemy and then i add a couple of heroes to it and commit and then from that point forward my api endpoints are going to say for reading heroes they're going to get a list of all the heroes looks very sql alchemy these are actually rapper ones not the uh these are from sql model not from the underlying sql alchemy and then the pattern that sebastian did in his tutorial that i thought was great was rather than every one of these having to make a session as a context manager and do all that kind of stuff he made a dependency on something that would go and make a session for you and then yield to you for doing all of your statements and so the context manager is put in one place it also makes it easier to write tests last thing for the day before getting comments from the two of you on this bigger one over in the test i really love his pattern in the tutorial of creating fixtures to set up the session set up the client and never have an actual database on disk that's being used uh instead use the in memory version of sqlite so that my tests don't really have to worry as much about overriding the production database or whatever it is that they're currently running on so lots of great information in the sql model tutorial i will stop there and we'll get some comments before going into the wrap up uh do you uh alexi did you see anything over in chat for us to talk about uh there's a question for sebastian uh is it possible to use depends in the startup event not currently it's something that i have on the plans to be able to use depends on the startup events and media me over but not currently not yet so that's what's being discussed yeah there's also a question uh what about nosql like mongodb so if you are using something like mongodb you can use a tool like a like automatic i think is the name the other one is bini there are two libraries to connect with mongodb based on pythagorean because it's very similar a sql model the idea is that you create a single model and it can be used to interact with mongodb and at the same time to declare the the shapes of the data for the inputs and outputs of the apis and uh jan asks that this is based on the open api spec um i believe you've gone through multiple generations of increasing your open api support is that right yeah i think that question was about the about the end points feature but i'm pretty sure the endpoint is tightly integrated into the fast api code itself right let's see uh yeah that's uh that's statically informed from the fast api structure from the structure of your sdk application so that one is is extracted from the from the actual code from the actual decorators and all the stuff yeah that's uh that's how it's implemented now but uh we could reevaluate it for future implementations if we see that reading it from somewhere is better yeah like that will be the thing is that reading from open api will mean that you will have to execute the code and the whole idea of this is not having to execute the code and just being able to do it with static analysis so i guess yeah it could be done if you wanted to have a client that is independent of any framework or something like that that would be like a whole different project for the facility integration this is the best way to approach it i would think yes i'll answer a question from that question ricky teacher was actually given the answer on this but i'll demo it real quick there was a question about pycharm and fixtures and fixture names if i go and put a typo here and come back um these won't be or it'll auto-complete on that so we actually go and sniff out this name protocol of the pi test came up with for fixture names and we go and find them and then we'll auto complete on them and we will type infer uh the correct one so nice that's very cool so we know that this is a session all right let's see okay um i believe that yeah yes there is an uh there the the question about the link to uh our pike farm guide tutorials so it's jetbrains.com pycharm guide and there's a list of some tutorials in here and moocool is coming up with i think mukul's in the channel uh is working on the mother of all tutorials on fast api and aws and kubernetes nice okay let's go ahead and start the process of wrapping up um before we get into the conclusion sebastian what's next for fast api sql model the whole chain oh a lot of things right now i'm actually working on improving some of the ways that the async libraries work underneath this is actually a currently part of my work at forza uh to improve the way the technical details is how a how sending code to be executed in a thread pool and making it compatible with context virus works because currently it requires a lot of manual handling so i want that to be easier to do but what it will allow and what it will enable and what it will result in the end is some documentation in fast api about how to integrate async with non-async code in the same functions in the same code basis which will be super useful and if people want to get involved in either sql model or fast api what's the best way there's a whole section in the documentation about helping and getting help for fast api and sql model and for example if you help others in fast api then you will you might qualify to the fast api expert session there's a session with the official fast api experts and those are just the people that are helping others with their question and like are showing that they are experts because they know how to solve even others other people's problems so there are several ways to contribute it's all documented there and it's made in a way that should be easy for you to go ahead and help others can they say on their cv that they've been using fast api for 10 years well i guess [Laughter] to be fair and to be compatible with the with the requests from recruiters i guess that's a way to disqualify the recruiter uh alexa um all these ideas that people might have that i encourage them to come see you about ways to put the structure to work weirdo ideas which is my department um what's the best way for people to let us know um what's a good developer experience well uh the best way i would say is our issue tracker we have an open issue tracker anyone can create a ticket so feel free to submit your suggestions for features and uh if you see that someone has already suggested your idea feel free to vote for it because that's something that we monitor constantly and if we see something getting traction we make sure to to take a look at it we actually before every release we do go through and look at votes and things like that we actually do um okay so sebastian i gotta give you a big thank you not just for coming on here and talking with us but everything that you do in the world of python folks don't realize how emotionally exhausting it can be running a high profile open source project i think not will but simon wilson sent a screenshot today of the totally abusive way people treat open source creators who are giving stuff away for free just like why haven't you solved my problem for me you know okay sorry i haven't solved your problem for you so we are just so lucky to have you um and alexa thanks for bringing uh into pycharm a number of the full stack thingies that i care so much about and this whole idea of getting more structure out of things so thanks to both of you thank you very much thank you for having me paul alexie on pycharm and jet fringe thanks for inviting me it's a pleasure to be here it was awesome thanks to you uh if you'd like to get more information on pycharm got slide on the screen uh go to our website at jetbrains.com pycharm if you haven't already take a look at the blog what can you find on the blog well you can probably find an announcement about the recording of this pretty soon and also news about releases we're doing a lot of releases because we're getting into the uh finishing up the eap cycle for 2021.3 as well as events conferences so that we go to announcements for webinars things like this and other educational resources we would really like feedback on this webinar so feel free to contact us on twitter i'll probably be the one that sees it and replies so be nice to me no seriously say what you think also we'll send out an after webinar survey we read these things in great detail feel free to send them out or just drop me an email pretty easy to figure out my email address that's all from us today thank you to our esteemed guests plural which is a lot of fun having both of you as well as our esteemed viewers thank you for joining us have a nice day you
Info
Channel: PyCharm by JetBrains
Views: 4,230
Rating: undefined out of 5
Keywords: web, FastAPI, rest, python, pycharm, webinar
Id: r6fBoBTRfT4
Channel Id: undefined
Length: 71min 50sec (4310 seconds)
Published: Thu Oct 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.