diff --git a/dynaconf/utils/parse_conf.py b/dynaconf/utils/parse_conf.py index 902ba2725..5605393b5 100644 --- a/dynaconf/utils/parse_conf.py +++ b/dynaconf/utils/parse_conf.py @@ -309,7 +309,8 @@ def _parse_conf_data(data, tomlfy=False, box_settings=None): ): # Check combination token is used comb_token = re.match( - r"^@(str|int|float|bool|json) @(jinja|format)", data + f"^({'|'.join(converters.keys())}) @(jinja|format)", + data, ) if comb_token: tokens = comb_token.group(0) diff --git a/example/custom_cast_token/app.py b/example/custom_cast_token/app.py new file mode 100644 index 000000000..403e548d3 --- /dev/null +++ b/example/custom_cast_token/app.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from pathlib import Path + +from dynaconf import Dynaconf +from dynaconf.utils import parse_conf + +settings = Dynaconf( + envvar_prefix="DYNACONF", + settings_files=["settings.toml"], + environments=True, +) + +# Add a custom casting token casting string to pathlib.Path object +parse_conf.converters["@path"] = ( + lambda value: value.set_casting(Path) + if isinstance(value, parse_conf.Lazy) + else Path(value) +) + +assert isinstance(settings.parent, Path) +assert isinstance(settings.child, Path) +assert str(settings.child).find("@format") == -1 diff --git a/example/custom_cast_token/settings.toml b/example/custom_cast_token/settings.toml new file mode 100644 index 000000000..bdda15da3 --- /dev/null +++ b/example/custom_cast_token/settings.toml @@ -0,0 +1,3 @@ +[default] +parent = "@path @format {env[HOME]}/parent" +child = "@path @format {this.parent}/child"