Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dependency_injector/providers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ except ImportError:

has_pydantic_settings = False
cdef bint pydantic_v1 = False
cdef object PydanticSettings = None
PydanticSettings = None
cdef str pydantic_module = "pydantic_settings"
cdef str pydantic_extra = "pydantic2"

Expand Down
60 changes: 60 additions & 0 deletions tests/unit/providers/configuration/test_settings_shutdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Imported settings classes must participate in interpreter cleanup."""

import subprocess
import sys
import textwrap

import pytest


@pytest.mark.pydantic
def test_settings_released_after_exit_callbacks():
result = subprocess.run(
[
sys.executable,
"-u",
"-c",
textwrap.dedent(
"""
import atexit
import os

def check_provider():
config.from_pydantic(Settings(value=43))
assert config.value() == 43
print("EXIT_CALLBACK_OK")

atexit.register(check_provider)
from dependency_injector import providers
try:
from pydantic_settings import BaseSettings
except ImportError:
from pydantic import BaseSettings

class Marker:
def __del__(self, write=os.write):
write(1, b"SETTINGS_RELEASED\\n")

BaseSettings.shutdown_test_marker = Marker()

class Settings(BaseSettings):
value: int = 42

config = providers.Configuration()
config.from_pydantic(Settings)
assert config.value() == 42
atexit.register(check_provider)
"""
),
],
capture_output=True,
text=True,
timeout=30,
check=True,
)
assert result.stdout.splitlines() == [
"EXIT_CALLBACK_OK",
"EXIT_CALLBACK_OK",
"SETTINGS_RELEASED",
]
assert result.stderr == ""