Files
Eduard van Valkenburg 534e5f5bf7 Python: improve .env handling and observability samples (#4032)
* Python: improve .env precedence and observability samples

- Switch load_settings to explicit precedence: overrides -> explicit .env -> environment -> defaults\n- Raise when env_file_path is provided but missing\n- Update settings docs and tests for new behavior\n- Refresh observability samples and README guidance for env loading options\n\nCloses #3864\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fixed some imports

* Fix load_settings CI regressions

Allow explicit env_file_path values that exist but are not regular files (for example /dev/null) by checking path existence before dotenv parsing, and restore a dict accumulator with typed return cast to satisfy mypy.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Avoid implicit dotenv in observability

Only load dotenv in observability helpers when env_file_path is explicitly provided, and remove test os.devnull workarounds that are no longer necessary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-18 11:18:52 +00:00

53 lines
1.6 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import copy
import os
from collections.abc import Awaitable, Callable
from typing import Any
import pytest
from pytest import MonkeyPatch, mark, param
from samples.getting_started.threads.custom_chat_message_store_thread import main as threads_custom_store
from samples.getting_started.threads.suspend_resume_thread import main as threads_suspend_resume
# Environment variable for controlling sample tests
RUN_SAMPLES_TESTS = "RUN_SAMPLES_TESTS"
# All thread samples
thread_samples = [
param(
threads_custom_store,
[], # Non-interactive sample
id="threads_custom_store",
marks=[
pytest.mark.openai,
pytest.mark.skipif(os.getenv(RUN_SAMPLES_TESTS, None) is None, reason="Not running sample tests."),
],
),
param(
threads_suspend_resume,
[], # Non-interactive sample
id="threads_suspend_resume",
marks=[
pytest.mark.openai,
pytest.mark.skipif(os.getenv(RUN_SAMPLES_TESTS, None) is None, reason="Not running sample tests."),
],
),
]
@mark.parametrize("sample, responses", thread_samples)
async def test_thread_samples(sample: Callable[..., Awaitable[Any]], responses: list[str], monkeypatch: MonkeyPatch):
"""Test thread samples with input mocking and retry logic."""
saved_responses = copy.deepcopy(responses)
def reset():
responses.clear()
responses.extend(saved_responses)
def mock_input(prompt: str = "") -> str:
return responses.pop(0) if responses else "exit"
monkeypatch.setattr("builtins.input", mock_input)
await sample