Backward incompatible changes:
- A loader no longer hides the reason it could not load.
IniFileraises
InvalidConfigurationFileErrorfor a file that does not parse and
MissingSettingsSectionErrorfor a file without the[settings]
section, andAwsParameterStorelets theboto3error through when
the parameter store cannot be reached. Both used to be skipped in silence:
with wrong AWS credentials or a typo in an.inifile the application
started with thedefaultof every setting. A file that does not exist
is still skipped. If the same code runs where there is no access to AWS,
leaveAwsParameterStoreout ofconfig.loadersthere. config.evalraisesInvalidConfigurationErrorfor an invalid value,
where it used to raise theValueErrororSyntaxErrorof
ast.literal_eval. Code that caught those needs to catch
InvalidConfigurationErrorinstead.
Other changes:
- Add a
wrapperargument toconfig(). It is called with the value the
cast returned:config("PIN", cast=config.int, wrapper=config.secret). - Add the
config.secretwrapper. It returns aSecretValue, which prints
as***and gives the value back throughreveal(). ASecretValue
is not a string:json.dumpsand concatenation raiseTypeError. Type
checkers keep the type of the value, so the example above is a
SecretValue[int]. - Add the
config.intandconfig.floatcasts. Unlikecast=int, they
raiseInvalidConfigurationErrornaming the variable, and never put the
rejected value in the message. - Add the
config.decimalandconfig.pathcasts, fordecimal.Decimal
andpathlib.Path. - Add the
config.timedeltacast, which reads a duration into a
datetime.timedelta: a number of seconds, or numbers with the units
ms,s,m,handd, as in500msor1h30m. - Add the
config.urlcast, which returns the result of
urllib.parse.urlsplitafter checking that the value has a scheme
followed by://and a valid port. Integer,Float,DecimalandTimedeltatake aminimum
and amaximum:cast=Integer(minimum=1, maximum=65535). A value
outside the limits raisesInvalidConfigurationErrornaming the limit.- Add the
config.enumcast: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.listandconfig.tupletake an element cast:
config.list[config.int]returns alist[int], for the type checker
and at runtime.config.base64takes 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 toBase64.- The casts page describes the builtin casts by what they do: return a type,
read a format, or undo an encoding. config.evalis 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, soconfig["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), andLoaderjoins 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. IniFileandRecursiveSearchskip a directory whose name ends in
.inior.cfg, asEnvFilealready did, instead of raising
IsADirectoryError.RecursiveSearchskips every file that does not parse. Atox.inior
setup.cfgwith a duplicated option, or a.envthat is not text,
used to stopconfig()with an error ofconfigparseror a
UnicodeDecodeError.AbstractCastis generic on the type the cast returns:
class Percentage(AbstractCast[float]). PyCharm now infers the type of
config.base64,Base64("utf-8")andconfig.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
urllib3in the
lock file to 2.8.0, closing three security alerts. Neither is part of what
pip install prettyconfbrings: 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
AbstractCastinstance 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")isTrueandlist("a,b")splits the string into
characters. Use theconfig.*casts, or inherit fromAbstractCastto
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