Examples

Black (pyproject.toml)

# pyproject.toml

[tool.black]
line-length = 79
from enum import Enum

import click

import typed_settings as ts


class PyVersion(Enum):
    py37 = "3.7"
    py38 = "3.8"
    py39 = "3.9"


@ts.settings
class Settings:
    line_length: int = 88
    skip_string_normalization: bool = False
    # target_version: Optional[PyVersion] = None  # Does not yet work
    target_version: PyVersion = PyVersion.py37


@click.command()
@ts.click_options(
    Settings,
    appname="black",
    config_files=["pyproject.toml"],
    config_file_section="tool.black",
    env_prefix=None,
)
def cli(settings):
    print(settings)


if __name__ == "__main__":
    cli()
$ python black.py --help
Usage: black.py [OPTIONS]

Options:
  --line-length INTEGER           [default: 79]
  --skip-string-normalization / --no-skip-string-normalization
                                  [default: False]
  --target-version [py37|py38|py39]
                                  [default: py37]
  --help                          Show this message and exit.
$ python black.py --skip-string-normalization
Settings(line_length=79, skip_string_normalization=True, target_version=<PyVersion.py37: '3.7'>)

Pytest

Python-Gitlab

# python-gitlab.toml

[global]
default = "gitlab-com"

[gitlab-com]
url = "https://gitlab.com"
private_token = "a93af93ff0adf9j3"
api_version = 4
# python_gitlab.py

import typed_settings as ts


@ts.settings
class GitlabSettings:
    url: str
    private_token: str = ts.secret()
    api_version: int = 3


@ts.settings
class GlobalSettings:
    default: str
    ssl_verify: bool = True


global_settings = ts.load_settings(
    GlobalSettings,
    appname="gitlab",
    config_files=["python-gitlab.toml"],
    config_file_section="global",
)
gitlab_settings = ts.load_settings(
    GitlabSettings,
    appname="gitlab",
    config_files=["python-gitlab.toml"],
    config_file_section=global_settings.default,
)
print(global_settings)
print(gitlab_settings)
$ python python_gitlab.py
GlobalSettings(default='gitlab-com', ssl_verify=True)
GitlabSettings(url='https://gitlab.com', private_token=***, api_version=4)

.pypirc

Original

# pypirc.toml

[distutils]
index-servers = ["pypi", "test"]

[pypi]
repository = "https://upload.pypi.org/legacy/"
username = "test"

[test]
repository = "https://test.pypi.org/legacy/"
username = "test"
# pypirc_0.py

import sys
from typing import List

import typed_settings as ts


@ts.settings
class RepoServer:
    repository: str
    username: str
    password: str = ts.secret(default="")


@ts.settings
class Settings:
    index_servers: List[str]


REPO_NAME = sys.argv[1]
repo_server = ts.load_settings(RepoServer, REPO_NAME, ["pypirc.toml"])
print(repo_server)
$ python pypirc_0.py pypi
RepoServer(repository='https://upload.pypi.org/legacy/', username='test', password=***)

Improved

# pypirc.toml

[distutils.repos.pypi]
repository = "https://upload.pypi.org/legacy/"
username = "test"

[distutils.repos.test]
repository = "https://test.pypi.org/legacy/"
username = "test"
# pypirc_1.py

import sys
from typing import Dict

import typed_settings as ts


@ts.settings
class RepoServer:
    repository: str
    username: str
    password: str = ts.secret(default="")


@ts.settings
class Settings:
    repos: Dict[str, RepoServer]


settings = ts.load_settings(Settings, "distutils", ["pypirc.toml"])
REPO_NAME = sys.argv[1]
print(settings.repos[REPO_NAME])
$ python pypirc_1.py pypi
RepoServer(repository='https://upload.pypi.org/legacy/', username='test', password=***)