pypi prettyconf 3.1.0

one hour ago

Backward incompatible changes:

  • A loader no longer hides the reason it could not load. IniFile raises
    InvalidConfigurationFileError for a file that does not parse and
    MissingSettingsSectionError for a file without the [settings]
    section, and AwsParameterStore lets the boto3 error through when
    the parameter store cannot be reached. Both used to be skipped in silence:
    with wrong AWS credentials or a typo in an .ini file the application
    started with the default of every setting. A file that does not exist
    is still skipped. If the same code runs where there is no access to AWS,
    leave AwsParameterStore out of config.loaders there.
  • config.eval raises InvalidConfigurationError for an invalid value,
    where it used to raise the ValueError or SyntaxError of
    ast.literal_eval. Code that caught those needs to catch
    InvalidConfigurationError instead.

Other changes:

  • Add a wrapper argument to config(). It is called with the value the
    cast returned: config("PIN", cast=config.int, wrapper=config.secret).
  • Add the config.secret wrapper. It returns a SecretValue, which prints
    as *** and gives the value back through reveal(). A SecretValue
    is not a string: json.dumps and concatenation raise TypeError. Type
    checkers keep the type of the value, so the example above is a
    SecretValue[int].
  • Add the config.int and config.float casts. Unlike cast=int, they
    raise InvalidConfigurationError naming the variable, and never put the
    rejected value in the message.
  • Add the config.decimal and config.path casts, for decimal.Decimal
    and pathlib.Path.
  • Add the config.timedelta cast, which reads a duration into a
    datetime.timedelta: a number of seconds, or numbers with the units
    ms, s, m, h and d, as in 500ms or 1h30m.
  • Add the config.url cast, which returns the result of
    urllib.parse.urlsplit after checking that the value has a scheme
    followed by :// and a valid port.
  • Integer, Float, Decimal and Timedelta take a minimum
    and a maximum: cast=Integer(minimum=1, maximum=65535). A value
    outside the limits raises InvalidConfigurationError naming the limit.
  • Add the config.enum cast: config.enum(LogLevel) accepts the name or
    the value of a member and returns the member. An invalid value is reported
    with the list of valid names.
  • config.list and config.tuple take an element cast:
    config.list[config.int] returns a list[int], for the type checker
    and at runtime.
  • config.base64 takes the cast of its content the same way:
    config.base64[config.json] decodes the value and reads the result as
    JSON. The content reaches the other cast as UTF-8 text, or in the encoding
    given to Base64.
  • The casts page describes the builtin casts by what they do: return a type,
    read a format, or undo an encoding.
  • config.eval is now a cast object like the others, instead of a bare
    function, and its error message no longer carries the value.
  • Add namespaces: config["AUTH0_"] returns a configuration that adds the
    prefix to every name, so config["AUTH0_"]("TOKEN") reads
    AUTH0_TOKEN. Namespaces can be nested and take the same arguments as
    config.
  • Split the loaders into sources and formats. A source knows where the
    configuration lives (EnvironmentSource, FileSource,
    CommandLineSource, ParameterStoreSource), a format turns its
    content into names and values (KeyValueFormat, EnvFormat,
    IniFormat, JsonFormat), and Loader joins one of each:
    Loader(FileSource("secrets.json"), JsonFormat()). Reading from a vault
    or a new file format now takes one small class instead of a whole loader.
    The existing loaders are built this way and keep their arguments,
    attributes and behaviour.
  • IniFile and RecursiveSearch skip a directory whose name ends in
    .ini or .cfg, as EnvFile already did, instead of raising
    IsADirectoryError.
  • RecursiveSearch skips every file that does not parse. A tox.ini or
    setup.cfg with a duplicated option, or a .env that is not text,
    used to stop config() with an error of configparser or a
    UnicodeDecodeError.
  • AbstractCast is generic on the type the cast returns:
    class Percentage(AbstractCast[float]). PyCharm now infers the type of
    config.base64, Base64("utf-8") and config.option(...), which it
    reported as an unsolved type variable since 3.0.0.
  • Add support for Python 3.15.
  • Compare prettyconf to pydantic-settings in the FAQ.
  • Update the action that publishes to PyPI to 1.14.3 and urllib3 in the
    lock file to 2.8.0, closing three security alerts. Neither is part of what
    pip install prettyconf brings: the first runs in the release workflow
    and the second is only pinned in the lock file used for development.

Deprecations:

  • Passing anything that is not an AbstractCast instance as a cast emits a
    DeprecationWarning, and will stop working in the next major release.
    That covers the native types (cast=int, cast=bool, cast=list),
    plain functions and lambdas. The native types were never reliable casts:
    bool("false") is True and list("a,b") splits the string into
    characters. Use the config.* casts, or inherit from AbstractCast to
    write your own.

What's Changed

  • Use language-neutral values in the custom boolean cast test by @osantana in #54
  • Guard against custom boolean values leaking into other instances by @osantana in #55
  • Update the PyPI publish action and urllib3 by @osantana in #59
  • Add wrappers, typed casts and namespaces for 3.1.0 by @osantana in #58

Full Changelog: 3.0.1...3.1.0

Don't miss a new prettyconf release

NewReleases is sending notifications on new releases.