Skip to main content

67 posts tagged with "python"

python tag description

View All Tags

Mocking __init__ methods in Python

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

Patching a class's __init__ method is a common and powerful technique for unit testing code that relies on objects whose initialization performs unwanted side effects. These side effects can include making API calls, connecting to a database, or performing other time-consuming or stateful operations [1]. By mocking __init__, you can prevent these actions and verify that the class was instantiated correctly.

Mocking Time and Dates in Python

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

Mocking time and dates is a common and often essential practice when writing tests for time-dependent logic. If your code's behavior changes based on the current time (e.g., a function that checks for a subscription's expiration, a daily report generator, or a cache invalidation rule), relying on the system's clock will lead to unreliable and non-repeatable tests.

You need a way to "freeze" time or fast-forward it to a specific point so your tests run in a controlled, predictable environment. Python's unittest.mock.patch is the perfect tool for this, allowing you to replace datetime.now() with a mock that returns a fixed value.

Pytest mock pitfalls

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

Writing tests that are effective and maintainable is an art. While mocking is a powerful tool, it's not a silver bullet and can be misused. This final article of the mocking series will cover the best practices and common pitfalls of mocking in Python, ensuring your tests are robust, readable, and provide a high degree of confidence in your code.

Pytest: Mocking Objects and Classes

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

Mocking entire classes and objects is a crucial skill for unit testing complex code. When a function instantiates a class or calls methods on a passed-in object, you need a way to control that object's behavior without executing its real logic. This article will cover advanced mocking techniques, focusing on how to mock classes and instance methods using both the standard unittest.mock library and the more convenient pytest-mock plugin. We will also clarify key distinctions between different types of test doubles.

Mock external dependencies in Python unittest

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

External dependencies are a major source of pain in unit testing. They can be slow, unreliable, and difficult to control. Mocking these external services is the single most common and valuable use of the unittest.mock library [1]. This article will provide practical, hands-on examples for mocking three of the most frequent external dependencies: HTTP requests, database connections, and file system operations.

Mocking in Python

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

Mocking in Python is a fundamental practice for writing robust, reliable, and efficient tests. It allows developers to isolate a unit of code from its external dependencies, ensuring that a test is only concerned with the logic it is meant to verify [1, 2].

Deep dive into pydantic BaseModel class decorators

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

Pydantic BaseModel class decorators are a powerful and modern way to customize a model's behavior and validation logic. While a lot of Pydantic's functionality is configured through class attributes or the ConfigDict, decorators offer a more explicit and code-centric approach, especially for complex validation.

Pydantic V2 introduced several new decorators to enhance validation and model configuration. The most important ones are:

  • @model_validator
  • @field_validator
  • @computed_field

These decorators are typically imported from pydantic.

Benchmark: msgspec vs. Pydantic v2

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

msgspec is an extremely fast serialization and validation library that consistently outperforms Pydantic v2 in benchmarks. This performance advantage comes from its design as a lean, compiled-code-based library focused on a narrow set of data handling tasks, whereas Pydantic v2 is a feature-rich framework.

The performance differences are most pronounced in two key areas: parsing/decoding (converting data like JSON into Python objects) and serialization/encoding (converting Python objects into data like JSON).

Ultimate pre-commit Configuration for Python

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

Maintaining a clean and consistent codebase is crucial for any successful project. Enforcing standards across your team can be challenging, but with pre-commit, you can automate this process directly into your development workflow. This guide outlines a powerful pre-commit configuration that combines formatting, linting, and static type checking to ensure your code is always in top shape.

A Guide to Preserving YAML Formatting with PyYAML

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

Python's PyYAML library is a powerful tool for working with YAML, but it has one common limitation: when you load a YAML file and dump it back, it doesn't preserve the original formatting. This can be a problem if you have multi-line strings formatted as literal blocks (|) and want to keep them that way for readability.

Fortunately, there's a straightforward and effective way to solve this by creating a custom representer for PyYAML. This guide will walk you through the process, using the code you provided, to ensure your multi-line strings are always dumped as literal blocks.