Al Wonder
Al Wonder

Reputation: 31

Loading setting using Dynaconf - different behaviors in python 3.10 and 3.11

I am using Dynaconf 3.2.5 to load project settings from multiple files. When I run this code in Python 3.10.9, everything works fine, but when I use 3.11.1, the settings are not loading.

Here are some of the main parts of the code.


DEFAULT_CONFIG_DIR = Path(__file__).parent / 'config'

class Config(BaseModel):
    class FileFormats(str, Enum):
        """Supported configuration file formats."""

        TOML = 'toml'
        YAML = 'yaml'
        JSON = 'json'
        INI = 'ini'
        ENV = 'env'


   config_dir: DirectoryPath = Field(
        DEFAULT_CONFIG_DIR,
        description='The directory containing the configuration files.',
    )

    recursive: bool = Field(
        True,
        description='If True, load config files from subdirectories.',
    )
    
    use_file_formats: Annotated[
        list[FileFormats],
        Field(
            [FileFormats.TOML, FileFormats.YAML],
            min_length=1,
            description='The file formats to use for loading configuration parameters.',
        ),
    ]

    _params: Dynaconf | None = None

    @computed_field  # type: ignore[misc]
    @cached_property
    def params(self) -> Dynaconf:

        if self._params is None:
            self._params = self._load_params()

        return self._params

    def _load_params(self) -> Dynaconf:

        assert self._params is None

        if not self.config_dir.exists():
            raise FileNotFoundError(
                f'Config directory {self.config_dir} does not exist.'
            )

        # Find the list of configuration files to load
        settings_files: list[DirectoryPath | str] = []
        for file_format in self.use_file_formats:
            if self.recursive:
                settings_files += list(self.config_dir.rglob(f'*.{file_format}'))
            else:
                settings_files += list(self.config_dir.glob(f'*.{file_format}'))

        return Dynaconf(
            # envvar_prefix=False,
            merge_enabled=True,
            # environments=False,
            settings_files=settings_files,
        )

The above code works fine in Python 3.10. When I run it in Python 3.11, it runs, but none of the data from the files are actually getting loaded. Not very sure why this is happening.

Upvotes: 0

Views: 353

Answers (1)

Al Wonder
Al Wonder

Reputation: 31

This is silly error where I used file_format instead of file_format.value. I was thrown off by the fact that it worked in 3.10

Upvotes: 0

Related Questions