Skip to main content

Description of the image

Test Coverage Package version Supported Python versions

typed-configparser

typed-configparser is an extension of the standard configparser module with support for typed configurations using dataclasses. It leverages Python's type hints and dataclasses to provide a convenient way of parsing and validating configuration files.

Features

✓ Fully typed.
✓ Use dataclasses to parse the configuration file.
✓ Support for almost all python built-in data types - int, float, str, list, tuple, dict and complex data types using Union and Optional.
✓ Supports almost all features of dataclasses including field level init flag, post_init method, InitVars and more.
✓ Built on top of configparser, hence retains all functionalities of configparser.
✓ Support for optional values (optional values are automatically set to None if not provided).
✓ Smarter defaults (see below).

Installation

You can install typed_configparser using pip:

pip install typed-configparser

Usage

examples/basic.py

# This is a complete example and should work as is

from typing import List
from typed_configparser import ConfigParser
from dataclasses import dataclass


@dataclass
class BASIC:
    option1: int
    option2: str
    option3: float
    option4: List[str]


config = """
[BASIC]
option1 = 10
option2 = value2
option3 = 5.2
option4 = [foo,bar]
"""

parser = ConfigParser()
parser.read_string(config)
section = parser.parse_section(using_dataclass=BASIC)

print(section)
BASIC(option1=10, option2=value2, option3=5.2, option4=['foo', 'bar'])

examples/unions_and_optionals.py

# This is a complete example and should work as is

from typing import List, Union, Optional, Dict, Tuple
from typed_configparser import ConfigParser
from dataclasses import dataclass, field


@dataclass
class DEFAULT_EXAMPLE:
    option1: int
    option2: Union[List[Tuple[str, str]], List[int]]
    option3: Dict[str, str] = field(default_factory=lambda: {"default_key": "default_value"})
    option4: Optional[float] = None


config = """
[DEFAULT]
option1 = 20
option2 = default_value2

[MY_SECTION_1]
option2 = [10,20]
option4 = 5.2

[MY_SECTION_2]
option2 = [(value2a, value2b), (value2c, value2b), (value2c, value2d)]
option3 = {key: value}
option4 = none
"""

parser = ConfigParser()
parser.read_string(config)
my_section_1 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name="MY_SECTION_1")
my_section_2 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name="MY_SECTION_2")

print(my_section_1)
print(my_section_2)
DEFAULT_EXAMPLE(option1=20, option2=[10, 20], option3={'default_key': 'default_value'}, option4=5.2)
DEFAULT_EXAMPLE(option1=20, option2=[('value2a', 'value2b'), ('value2c', 'value2b'), ('value2c', 'value2d')], option3={'key': 'value'}, option4=None)

Check example directory for more examples.

Defaults

  • configparser includes sensible defaults options which allows you to declare a [DEFAULT] section in the config file for fallback values.
  • typed_configparser goes a step further and allows you to set a final (last) level of defaults at dataclass level.

License

MIT License

Contribution

If you are interested in contributing to typed_configparser, please take a look at the contributing guidelines.

Metadata

Release files for typed-configparser 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typed-configparser 1.1.0
File Size Uploaded
typed_configparser-1.1.0.tar.gz 27.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typed-configparser 1.1.0
File Interpreter ABI Platform
typed_configparser-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 49.3 kB

Release files / typed_configparser-1.1.0.tar.gz

Download URL typed_configparser-1.1.0.tar.gz
Size 27.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0a0ab47965151d095e0ad5f00aeb59a7ef95a5f9b6f045ac8d4417d91ba46d4d
BLAKE2b-256 checksum
How to use checksums
0c3abf80bb8cd2d97e5df46553fe03259c5ca1e8f75cab4682670a329f17554c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.8

Release files / typed_configparser-1.1.0-py3-none-any.whl

Download URL typed_configparser-1.1.0-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07ac120849267129c9228e3bbcb458113a5c3efddb771f7c930b8902ce185acc
BLAKE2b-256 checksum
How to use checksums
07003cc3bc0c450ca30b8f85795129ee704db3231323c5880d7989b9a9c91744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.8

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page