Skip to main content

Types for use with environment imported configurations

Project description

config-types

Type aliases and validators for configuration settings.

This package provides aliases for types typically found in configuration files. When the pydantic versions are used, they come with validators that transform the strings to their intended type (and value).

When the standard types are used, they all resolve to string and it's your job to transform them in your application code.

The rest of this document assumes usage with pydantic or more specifically with pydantic-settings.

Quick start

Install with: pip install config-types[pydantic]

Create your application settings:

from pydantic_settings import BaseSettings
from config_types.pydantic import IsTrue, DottedPathAttribute

class Config(BaseSettings):
    debug: IsTrue = False
    cache_backend: DottedPathAttribute

settings = Config()

Populate your environment, for example using a .env file:

DEBUG=yes
CACHE_BACKEND=django_redis.cache.RedisCache

Now import your settings singleton where needed.

Reference

DottedPath

A reference to a python module that is imported and returned if found.

Example:

from config_types.pydantic import DottedPath
from pydantic_settings import BaseSettings


class AppConfig(BaseSettings):
    logging_module: DottedPath

# In `.env`:
# LOGGING_MODULE=loguru

ModuleAttributeRef

A reference to a module attribute, typically a class name, singleton or callable. The module path is a DottedPath and the attribute to be imported is separated by a colon.

Example:

from config_types.pydantic import ModuleAttributeRef
from pydantic_settings import BaseSettings

class AppConfig(BaseSettings):
    asgi_application: ModuleAttributeRef

# In `.env`:
# ASGI_APPLICATiON=app.main:application

DottedPathAttribute

The same as a ModuleAttributeRef, but done with all dots. The last part is the entity reference.

Example:

from config_types.pydantic import DottedPathAttribute
from pydantic_settings import BaseSettings

class AppConfig(BaseSettings):
    CACHE_BACKEND: DottedPathAttribute

# In `.env`:
# CACHED_BACKEND=django_redis.cache.RedisCache

IsTrue

A switch that defaults to false. The following values are recognised as true (case-insensitive):

  • "yes"
  • "on"
  • "1"
  • "true"

Example:

from config_types.pydantic import IsTrue
from pydantic_settings import BaseSettings

class AppConfig(BaseSettings):
    debug: IsTrue = False

# In your development `.env`:
# DEBUG=yes

IsFalse

A switch that defaults to true. The following values are recognised as false (case-insensitive):

  • "no"
  • "off"
  • "0"
  • "false"

Example:

from config_types.pydantic import IsFalse
from pydantic_settings import BaseSettings

class AppConfig(BaseSettings):
    use_https: IsFalse = True

# In your development `.env`:
# USE_HTTPS=off

RelativeUrlRef

A relative URL reference, in the strict sense. While the URL specs may classify a URL starting with '/' as relative, this does not. It is meant to be used as the final part of a URL, where the first part is different for each environment. The classic case, is an asset reference part of static resources, which is then prepended with the STATIC_URL.

The validator does not strip a leading slash, but throws an error, to prevent users from providing references to the root of the server, which a typical application does not allow.

Example:

from config_types.pydantic import RelativeUrlRef
from pydantic_settings import BaseSettings

class AppConfig(BaseSettings):
    favicon: RelativeUrlRef = 'favicons/16x16.ico'

# In `.env`:
# FAVICON=branding/logo-32.png

TODO

The following are planned:

  • containers
  • support for other packages that can support a similar interface, if any

Note: numbers aren't planned as pydantic-settings (and typing) have plenty support for it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

config_types-0.2.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

config_types-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file config_types-0.2.1.tar.gz.

File metadata

  • Download URL: config_types-0.2.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for config_types-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0816882999ae7c8828702fd6af2ab5048006763d39e7bf690c6ccce961fc6754
MD5 fe53247519bf58b870706e0eaa441f54
BLAKE2b-256 7166466e0c181eb6ae798c911860e97dfd9ed66f28429e0cd9b10f5082b844cc

See more details on using hashes here.

File details

Details for the file config_types-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for config_types-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6fe513c3df4fcf1c579c1f043c35eda9208b5ddb7976ebae87715b26002ea17b
MD5 0278866817614c4419aa996918506060
BLAKE2b-256 1da779b6c69cc2782ed62a71da9b1cdce81a9f156d85d30a0ff7f3a1ed8a7993

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page