Skip to content

Scoped global config #226

@jorenham

Description

@jorenham

Useful for globally or temporarily overriding defaults, e.g. those used for kwargs such as dtype, trim, cache, sort, rowvar, n_extra, etc.

See polars.Config for a similar feature in polars with a clean API.

Usage

Global config:

Setting:

lmo.config.set(sort='stable')

Local config

with lmo.config(sort='stable'):
    ...

or

@lmo.config.override(sort='stable')
def spam(...):
    ...

Implementation

  • The code will live in a new lmo.config namespace.
  • Specify the config options using a class ConfigOptions(TypedDict, total=False): ....
  • Use a _CONFIG: collections.ChainMap for global config (although maybe an alternative or subclass is needed for TypedDict support).
  • Initialize the root of _CONFIG using default values.
  • Implement lmo.config.override(**options: *ConfigOptions) contextmanager + decorator by having it push/pop from _CONFIG.
  • Implement lmo.config.set(*ConfigOptions) by replacing the root _CONFIG, so that any potential locally overridden options aren't affected

Bonus feature: Environment variables

So that e.g. LMO_SORT="stable" can be used to specifiy sort='stable' (supersedes the root config).

Bonus feature: pyproject.toml

Something like

[tool.lmo]
sort = "stable"

It should have lower priorty than the environment variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions