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.2.tar.gz (4.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.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for config_types-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a21919053b17392c286d207e9c546cb100903cfe676b4e33543629c09073465e
MD5 26e7e6c45b5a7557d742cd42b2d75cc5
BLAKE2b-256 fbe37808a5ef79fe0f13f7b96bc65b932c164466da34b55d3f9f176cd92fcb46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for config_types-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f6539d5b8b37048b5224053db0995a7ae74c0c9deb3bdbe4e0a4ebce1b22c7be
MD5 26d55494e0a5036b3b35b05edca2b73a
BLAKE2b-256 3a704aed3bb84ddfbb6ae0ba7b23cd6a46bfe2937c9683c7f7bd2473e2b6f8aa

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