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

Add ability to pass prefix #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

denis-savran
Copy link

Summary

Added prefix keyword argument to environ.to_config(), environ.generate_help() and related class methods.

This allows to dynamically change a prefix or to instantiate a nested configuration class without all of its parent configuration classes.

Usage example:

...
@environ.config
class PostgresConfig:
    host: str = environ.var()
    port: int = environ.var(converter=int)


@environ.config(prefix="APP")
class GrpcWriteConfig:
    postgres: PostgresConfig = environ.group(PostgresConfig)
    ...

@environ.config(prefix="APP")
class GrpcReadConfig:
    postgres: PostgresConfig = environ.group(PostgresConfig)
    ...
>>> environ.to_config(PostgresConfig, {"APP_POSTGRES_HOST": "postgres", "APP_POSTGRES_PORT": "5432"}, prefix="APP_POSTGRES")
PostgresConfig(host='postgres', port=5432)

Pull Request Check List

  • Added tests for changed code.
  • New or changed APIs are added to our typing examples to ensure our external type hints are sound.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/api.md by hand.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Documentation in .md files are written using semantic newlines.
  • Changes (and possible deprecations) are documented in the changelog.

Add 'prefix' keyword argument to 'environ.to_config()',
'environ.generate_help()' and related class methods.
@denis-savran denis-savran force-pushed the add-ability-to-pass-prefix branch from 7ef904b to 6a15a30 Compare November 12, 2024 14:02
@denis-savran
Copy link
Author

Hello.

I decided to add a separate config for each entry point:
src/my_project/configs.py

@environ.config(prefix=ENV_PREFIX, frozen=True)
class GrpcWriteServerConfig:
    postgres: PostgresConfig = environ.group(PostgresConfig)
    logging: LoggingConfig = environ.group(LoggingConfig)
    write_dictionary_grpc = environ.group(WriteDictionaryGrpcConfig)
    write_dictionary = environ.group(WriteDictionaryConfig)


@environ.config(prefix=ENV_PREFIX, frozen=True)
class GrpcReadServerConfig:
    postgres: PostgresConfig = environ.group(PostgresConfig)
    logging: LoggingConfig = environ.group(LoggingConfig)
    read_dictionary_grpc = environ.group(ReadDictionaryGrpcConfig)
    read_dictionary = environ.group(ReadDictionaryConfig)


@environ.config(prefix=ENV_PREFIX, frozen=True)
class AlembicScriptConfig:
    postgres: PostgresConfig = environ.group(PostgresConfig)
    logging: LoggingConfig = environ.group(LoggingConfig)


@environ.config(prefix=ENV_PREFIX, frozen=True)
class CreateRowsScriptConfig:
    logging: LoggingConfig = environ.group(LoggingConfig)
    write_dictionary_grpc = environ.group(WriteDictionaryGrpcConfig)
    write_dictionary = environ.group(WriteDictionaryConfig)
    read_dictionary_grpc = environ.group(ReadDictionaryGrpcConfig)

So in my case there is no real need for this feature except for rare cases with interactive shell. Maybe you will find it useful.

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 this pull request may close these issues.

1 participant