Skip to main content

Resolving Pylance(reportMissingImports) in VS Code

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

The Pylance(reportMissingImports) error is one of the most common issues Python developers encounter in Visual Studio Code (VS Code). Pylance is a language server that provides intelligent code completion and type checking. This specific error means Pylance cannot find the installed package in the Python environment it is currently configured to inspect.

This issue is almost never a code problem; it is an environment configuration problem within VS Code.

SQLAlchemy joinedload vs. join()

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

In SQLAlchemy, both the relationship loading option (.options(joinedload(...))) and the query builder method (.join(TableClass)) result in a SQL JOIN clause. However, they serve fundamentally different purposes and lead to distinct results in the ORM (Object Relational Mapper) layer.

Understanding this difference is crucial for avoiding the common "N+1 problem" and correctly shaping the data returned by your queries.

SQLAlchemy Relationships Without Database Foreign Keys

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

This pattern is often used for:

  1. Legacy Databases: Working with existing schemas that lack proper constraints.
  2. Performance: Avoiding the overhead of transactional foreign key checks.
  3. Data Warehousing: Dealing with schemas where relationships are semantic, not structural.

The key to achieving this is the relationship() function combined with the primaryjoin argument. This allows SQLAlchemy to define the join condition required for the relationship, enabling essential features like eager loading (joinedload, selectinload).

FastAPI Authentication Middleware Example

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

Authentication Middleware is a crucial component for securing an API by inspecting incoming requests for credentials (like a JWT or API Key) and validating them before the request reaches the route handler. This ensures every protected endpoint is secured automatically and cleanly.

This example demonstrates how to implement a custom authentication middleware to check for a required Authorization header and either inject a valid user/context or reject the request early.

FastAPI Core Middleware Examples and Use Cases

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

Middleware in FastAPI (which is built on Starlette) is a powerful mechanism for executing code before and after a request is processed by the route handler. Middleware allows you to apply logic globally or across a group of routes, such as logging, authentication, CORS, and response manipulation.

The standard way to implement custom middleware is by defining an async function that takes the request and the call_next callable.

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.

Looking for more content?
Hrekov Blog contains 240 articles. Browse the blog archive or Explore the full timeline.