Skip to main content

90 posts tagged with "python"

python tag description

View All Tags

Why Use a Pydantic Model for a Single Attribute (The Wrapper Pattern)

6 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

It might seem excessive to define an entire Pydantic BaseModel for a single attribute when a simple type hint like user_id: str would suffice. However, using a single-attribute Pydantic model (often called a Wrapper Model or a Value Object) offers significant advantages, primarily around reusability, centralized validation, and complex parsing.

This pattern transforms a simple type hint into a powerful, reusable validation layer.

Python Annotations Rare Use Cases

7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Python annotations, introduced in PEP 3107 for function parameters and return values, were initially generic metadata slots. While their primary use has become type hinting (PEP 484), expert developers leverage them for advanced and niche applications that go far beyond simple type declarations.

These use cases often involve frameworks or metaprogramming to make annotations act as declarative configuration or runtime execution instructions.

The Wrapper Pattern in Python: Definition and Strategic Use Cases

7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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.

Resolving Pylance(reportMissingImports) in VS Code

6 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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 Dependency Injection: Internal Requests, Request Object, and Parameterized Depends

7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

FastAPI's Dependency Injection (DI) system is one of its most powerful architectural features. It allows developers to encapsulate authentication, parameter validation, database sessions, and low-level HTTP inspection into reusable, composable units.

This guide provides a comprehensive manual on advanced FastAPI dependency injection patterns: executing in-process internal route requests with TestClient, parameterizing dependencies with factories, accessing the Starlette Request object, nesting dependency resolution chains, and leveraging class-based dependencies.