Skip to main content

141 posts tagged with "python"

python tag description

View All Tags

Fastapi Depends with parameters and arguments

· 8 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

FastAPI's Dependency Injection (DI) system is remarkably flexible: dependency functions aren't just executed independently; they can be designed to accept and process arguments from the current HTTP request or the results of other dependencies.

Using parameters within a Depends function allows you to implement complex logic, dynamic configuration, and validation rules while keeping your main route handler clean.

FastAPI Depends and the Request Object

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

While dependencies primarily focus on injecting services or validated data, they can also gain direct access to the raw HTTP request object. This is an advanced technique useful for accessing metadata, non-standard headers, client information, or the complete request body/form data before it's processed by the route.

To access the request object, you simply declare a parameter with the type hint Request in your dependency function. FastAPI's Dependency Injection system automatically resolves the current request and injects it.

Internal HTTP request from one FastAPI route handler to another

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Reliable way to make an internal HTTP request from one FastAPI route handler to another within the same application, specifically to handle a POST request with a request body.

The best and most idiomatic way to handle this in FastAPI (which is built on Starlette) is by using the TestClient from the starlette.testclient module. This allows you to treat your application as an independent service and make internal requests without incurring any actual network overhead, which is crucial for testing and internal service calls.

When working with FastAPI, the correct method to call one route from another is not by importing the handler function directly, but by using the TestClient class. This simulates a genuine HTTP request, ensuring all middleware, dependencies, and validation logic run exactly as they would for an external client.

Consuming Path Arguments Directly in FastAPI Dependency Functions

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The short answer is: Yes, it is fundamentally possible and encouraged to pass Path arguments directly into FastAPI dependency functions.

This feature is a core component of FastAPI's Dependency Injection (DI) system, allowing dependencies to be highly contextual. A dependency can perform validation, authentication, or data fetching based on the URL or request inputs before the route handler ever executes.

Python Enum conversion to Collections and Serialization

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

This article focuses on the practical necessity of converting an entire enum.Enum class into standard Python collections (list, dict) and preparing them for data interchange formats like JSON. This is essential when presenting options in an API response, generating documentation, or storing structured data.

Python Enum Foundation, Basic and Naming

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

🐍 Python Enum: Foundation, Naming, and Primitive Conversion

This article focuses on the necessity of the enum.Enum class, proper definition, standard naming conventions, and the fundamental process of converting Enum members to and from primitive types like integers and strings.

Python Enum Number reverse lookup

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Reverse lookup is the process of retrieving the symbolic member name (e.g., NOT_FOUND) or the member object itself from its associated raw value (e.g., 404). This is an essential technique when dealing with external inputs like HTTP status codes, database keys, or configuration settings.

This article details the most efficient and robust methods for performing reverse lookup using the standard Python enum.Enum library.

Python Enum to String without Class Name

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

When using Python Enums, the default string output includes the class name (e.g., <MyEnum.MEMBER: 'value'>), which is often unsuitable for clean logging, API responses, or direct printing. This article demonstrates how to leverage Python's dunder methods (__str__, __repr__, __format__) to gain complete control over the string representation of your Enum members.

FastAPI Dependency Injection (DI) VS. Depends

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

That's an excellent question that gets right to the core of FastAPI's design.

Yes, FastAPI Dependency Injection (DI) and FastAPI Depends are two different but closely related concepts. You can think of them as the system and the tool used to activate that system.

Here's a breakdown of the difference:

Testing FastAPI Dependency Injection: Where to Start

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

For beginners, testing FastAPI dependencies should start with one fundamental goal: isolation. You must ensure your route logic is tested without making real network calls, hitting a live database, or relying on complex configuration settings.

The key to achieving this is using FastAPI's built-in app.dependency_overrides dictionary. This allows you to replace any real dependency function with a simple, predictable mock function for the duration of a test.