Skip to main content

141 posts tagged with "python"

python tag description

View All Tags

Typeguard Examples: Mandatory Runtime Type Checking in Python

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

Typeguard is a lightweight yet powerful Python library that enforces type hints at runtime. Unlike static checkers (like MyPy or Pylance), which only check code before execution, Typeguard uses function decorators to ensure that function arguments and return values strictly adhere to their type annotations during execution. If a type mismatch occurs, Typeguard raises a TypeError, effectively making your type hints mandatory contracts.

This article explores various practical use cases and examples for deploying Typeguard.

Python Annotations Rare Use Cases

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

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