Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DotEnv ValueError when env variable name is shorter than env_prefix #106

Closed
jvllmr opened this issue Jul 5, 2023 · 2 comments · Fixed by #107
Closed

DotEnv ValueError when env variable name is shorter than env_prefix #106

jvllmr opened this issue Jul 5, 2023 · 2 comments · Fixed by #107
Assignees

Comments

@jvllmr
Copy link
Contributor

jvllmr commented Jul 5, 2023

Example:

class DramatiqSettings(BaseSettings):
    broker_url: RedisDsn

    model_config = SettingsConfigDict(
        env_file=".env", env_prefix="plato_worker_", extra="ignore"  # type: ignore
    )

I also have another settings class that holds my general application settings.

When processing my PLATO_DEBUG env var, the following line fails:

first_key, *_ = env_name_without_prefix.split(self.env_nested_delimiter)

ValueError: not enough values to unpack (expected at least 1, got 0)

It's because PLATO_DEBUG is shorter than the env_prefix for my DramatiqSettings class.

Possible Solution:
Check if the env var name actually starts with the env prefix before stripping the name.

Selected Assignee: @samuelcolvin

@jvllmr
Copy link
Contributor Author

jvllmr commented Jul 5, 2023

I realised that this also happens when extras are forbidden

@jvllmr jvllmr changed the title ValueError raised when extras are enabled and env variable shorter than env_prefix ValueError when env variable name is shorter than env_prefix Jul 5, 2023
@hramezani
Copy link
Member

Thanks @jvllmr for reporting this 🙏

Would you mind opening a PR for fixing this? otherwise, I will fix it.

@jvllmr jvllmr changed the title ValueError when env variable name is shorter than env_prefix DotEnv ValueError when env variable name is shorter than env_prefix Jul 5, 2023
jvllmr pushed a commit to jvllmr/pydantic-settings that referenced this issue Jul 5, 2023
jvllmr added a commit to jvllmr/pydantic-settings that referenced this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants