Utils API#

fastapi_restly.utils holds helpers that belong to no single Restly subsystem.

Helpers that belong to no single Restly subsystem.

class fastapi_restly.utils.CurrentSettingsMixin#

Bases: object

Give a settings class one shared instance, built the first time it is read.

Mixed into a pydantic_settings.BaseSettings subclass, it replaces the module-level instance that would otherwise make importing your application require a configured environment:

# myapp/settings.py
class Settings(fr.utils.CurrentSettingsMixin, BaseSettings):
    database_url: str

Settings.current builds the instance on first read and returns the same one after that, so nothing is constructed at import and myapp.main stays importable without configuration. Alembic and the test suite rely on that.

Settings.use(...) installs an instance instead of building one, which is how a test suite hands over settings it constructed itself:

# tests/conftest.py
Settings.use(Settings(database_url=..., _env_file=None))

current and use become reserved names on the class: a settings field may not use either.

current = <fastapi_restly.utils.CurrentSettingsMixin object>#
classmethod use(instance: _T) → _T#

Install an explicit instance as current, and return it.

See also

Project structure covers where settings live in an application, and why importing your main module must not require a configured environment.