Skip to main content

158 posts tagged with "python"

python tag description

View All Tags

The Wrapper Pattern in Python: Definition and Strategic Use Cases

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

The Wrapper Pattern (often referred to in design literature as the Decorator Pattern or Adapter Pattern when applied to classes, but used here in the broader sense of wrapping functionality or data) involves encapsulating an object or a function within another object.

In the context of Python, particularly with frameworks like FastAPI and Pydantic, the Wrapper Pattern is primarily used to:

  1. Enhance or Extend Functionality without modifying the original object (Decorator/Adapter).
  2. Validate and Centralize Logic for a simple data type, turning it into a Value Object (as seen with Pydantic).

Mandatory Python hints Enforcement

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

This is a critical question for developers moving from dynamically-typed code to modern, type-hinted Python. The concise answer is: No, Python does not have mandatory type hints built into the language itself.

By default, the Python interpreter is dynamically typed and will ignore type hints entirely at runtime. However, you can make type hints mandatory and runtime-enforced by utilizing external tools and libraries.

The Python Type Hinting Paradox: Why it Doesn't Raise an Error

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

This is a fundamental and often confusing concept when developers transition to using static type checking in Python: Python's type hints are advisory, not mandatory.

The core reason why passing None to a function expecting a specific type like str or int does not raise an error at runtime (on entrance) is that the Python interpreter, by default, ignores type hints.

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 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.