Skip to main content

Overrdie Pydantic Settings

Settings management in Pydantic makes it easy to override settings, like below.

# override settings
settgins.env = "production"

However, if you want to change the setting value in each test suite in your test code,
You need to change the values at the beginning of the test and roll back the values at the end of the test.

from app.core.config import settings # Pydantic settings

def test_settings():
    # override setting value
    env = settings.ENV
    settings.ENV = "production"
    
    # assertions
    ...
    
    # rollback setting value, because in order not to affect other tests
    settings.ENV = env

This causes a lot of code duplication. So, in order to remove the code that occurs repeatedly, created a decorator for tests.

Features

You can easily override the setting value by applying a decorator to the test function.
Also, when the test function is finished, the settings are automatically roll back.

That's all.

# for sync tests
@override_settings(settings=settings, ENV="production")
# for async tests
@async_override_settings(settings=settings, ENV="production")

Installation

pip install override-pydantic-settings

How to use

For sync tests

from pydantic import BaseSettings
from override_pydantic_settings import override_settings


class MySettings(BaseSettings):
    ENV: str = "dev"
    NAME: str = "Junki"
    EMAIL: str = "na66421@gmail.com"

    
settings = MySettings()


@override_settings(settings=settings, ENV="production", NAME="Junki Yoon")
def test_function():
    ...

For async tests

@pytest.mark.asyncio
@async_override_settings(settings=settings, ENV="production", EMAIL="na86421@naver.com")
async def test_function():
    ...

Warning

You cannot override the setting value that does not exist.

Development

Compatible with Python >= 3.6

Python >= 3.10, we can create async decorators using the asnyc context manager.
It seems that async & sync can be integrated into one function.

Running tests

$ python -m venv venv
(venv)$ source venv/bin/acitave
(venv)$ pip install -r requirements.txt
(venv)$ pytest

License

This project is licensed under the MIT license.

Metadata

Release files for override-pydantic-settings 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for override-pydantic-settings 1.0.1
File Size Uploaded
override-pydantic-settings-1.0.1.tar.gz 3.4 kB Details

Release files / override-pydantic-settings-1.0.1.tar.gz

Download URL override-pydantic-settings-1.0.1.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0dbc552ff4e78e14156b89a7b67f6adbe8e8fd0105736861c5fe855a37594ab4
BLAKE2b-256 checksum
How to use checksums
60580616d2f1cad65fcb9da3509e7ded79572032a72fd609b96897258bb36d17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.4

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page