Python future annotations9/13/2023 ![]() In many cases, "static" is used when saying Static Analysis, Static Checking, Static Type Checking, etc. It's treated just as a text file containing code. It just means that the code is not being executed. The counterpart of runtime would be static. And Pydantic and FastAPI take advantage of those features.Īnother term commonly used to refer to doing things at runtime is to do things dynamically. How can these tools do that? Python is so powerful that it has features to allow exactly that, to read type annotations at runtime from the same Python code. And if we send a JSON request with a price that is not a float, they will be able to validate the data for us.įastAPI and Pydantic are written in pure Python. So, for example, in the Item class above, we have:Īt runtime, Pydantic and FastAPI will see that name is a str and price is a float. The way that program works is that, at runtime (when our code is being executed), Pydantic and FastAPI read those type annotations (or type hints) to extract their data and do things with it. main.py, it is not running, so we are not at runtime. When our code is not being executed, for example, when we are editing the file. It's just the period of time when it is executing our code. Now, when that program python is executing our code written in the Python programming language, we call that "runtime". So, you could say that python (the program) can read Python (the programming language). "Python": the name of the programming language we use to write our code.python: the program that runs our code (which is actually written in the C programming language).So, we have two things with more or less the same name "python" that represent something slightly different: Maybe you knew that.Īnd what that program python does is read the file main.py, interpret the code that we wrote in it using the Python Programming Language, and execute it step by step. That program called python (or python.exe) is written in another programming language called "C". Note: In Windows, the program might be called python.exe instead of just python. And to give it the file main.py as a parameter. But for this example, it will be useful to see everything from the point of view of the python command.īy running that command above, you are asking your system to start the program called python. Note: Instead of using the last two lines, you could have used the uvicorn command, and that's what you would normally do. ![]() Then you could open your browser and interact with the API docs at, etc.īut here we want to focus on what happens behind the scenes. main.py with the following code:Įnter fullscreen mode Exit fullscreen mode Let's see a simple example using them both. But still, here I want to help clarify all that and give you a bit more context.īrace yourself, you are about to learn a bit more about how Python works, how FastAPI and Pydantic work, how type annotations work, and more. It might have worried you and maybe confuse you a bit. I didn't fully understand it until I spent hours reading all the related content and doing multiple experiments. ![]() If you read about it, I wouldn't expect you to understand what all that meant. You might have heard not long ago about PEP 563, PEP 649, and some changes that could affect Pydantic and FastAPI in the future. From the Core Developers making Python itself to the new developers that started learning Python this month.Īnd as long as these tools are helping us all solve problems, help ourselves, help others, and be more efficient and productive, we all will keep them working and improving. This is because we all, as the Python community, define their future. The future of FastAPI and Pydantic is bright.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |