Skip to content

settings class #8

Closed

Description

@grst

Description of feature

every package could subclass the scverse settings class, but certain common options like n_jobs could be set globally for all scverse packages

Activity

  1. added theissue type on Apr 14, 2026
  2. ilia-kats commented on Apr 16, 2026

    @ilia-kats
    Collaborator

    What should that look like? As far as I can tell, there are at least two options:

    1. Pull out the settings class from AnnData, which tbh looks a bit overkill to me (maybe @ilan-gold or @flying-sheep can comment)
    2. Do something more minimal. We have a fairly minimal, but functioning, settings class in mofaflex, I could generalize this by making a @settings decorator that wraps @dataclass and adds the additional methods to the class.
  3. ilan-gold commented on Apr 16, 2026

    @ilan-gold

    I think most of the "overkill" comes from the fact that it is self-documenting: https://anndata.readthedocs.io/en/stable/generated/anndata.settings.html#anndata.settings.zarr_write_format

    Aside from that, pyi files are easy to make. But I might be missing something.

  4. flying-sheep commented on Apr 16, 2026

    @flying-sheep
    Member

    So Ilia’s approach needs a type stub too as the get, set, and override methods have no typing and therefore no autocomplete. Maybe we could pair whatever we come up with with a typestub generator that can be used as a hatch plugin.

    I think apart from the documentation, Ilan’s approach also allowed for extra per-setting features, namely validation and parsing (from env vars).

    So I could imagine a simpler-looking decorator-based design that still has the power needed by anndata:

    1. code-organization-wise we could pull out the documentation features and so on into submodules for clarifying core functionality.
    2. usability-wise, we could add default validators and parsers for known types.
    3. then we could use typing.Annotated or so to handle customization, dependencies, …

    Unfortunately extending pydantic (which comes with validation designed like that) is a pain as far as I remember, so we could evaluate it but shouldn’t commit too hard.

  5. ilia-kats commented on Apr 16, 2026

    @ilia-kats
    Collaborator

    Is there any advantage of having to do settings.register instead of writing the documentation directly into the class attribute? I can see how the former is more modular, but AnnData currently has everything in the same file anyway. If we drop this requirement, I think pydantic-settings has basically everything we need: validation, loading from environment variables, dotenv files, etc. We just need to add an override context manager method

  6. ilan-gold commented on Apr 16, 2026

    @ilan-gold

    instead of writing the documentation directly into the class attribute?

    It was to avoid this exactly while being modular about validation. This feature makes it easier/clearer to add new settings than having to write out a new property with a setter etc IMO.

    But I think like @grst said there are two different cases here - shared settings and package-specific settings. n_jobs doesn't apply to the data structures, for example. Does it go in the shared object? Do we provide an instantiated version of a shared object that can be extended by packages? What about subclassing?

    I don't think my dynamic-update business should be a deal breaker but I think

    1. Whatever is exported should probably have sensible settings baked in like zarr-v3 default or n jobs
    2. The docs for these may need to be overriden so that interpretations can differ - n jobs may not mean the same thing across packages and it would be good for packages to be able to specify that.
    3. There needs to be extensibility (as you can probably see form the wide array of anndata-settings there are). This extensibility should allow for documentation so that each scverse package can export its own version of the settings object with its own docs (see 2)

    Are there are other requirements?

  7. ilia-kats commented on Apr 16, 2026

    @ilia-kats
    Collaborator

    This feature makes it easier/clearer to add new settings than having to write out a new property with a setter etc IMO.

    You wouldn't need to do this with pydantic(-settings), since it will validate upon assignment (if configured with validate_assignment=True)

  8. melonora commented on Apr 16, 2026

    @melonora

    What would you think of using Donfig, similar to what zarr-python does? It only has pyyaml as dependency so it is very light and most of the time is already included as it is included with zarr.

  9. ilia-kats commented on Apr 16, 2026

    @ilia-kats
    Collaborator

    As far as I can tell that doesn't do validation, so we'll need to a) write custom validation logic and b) do more extensive subclassing than with pydantic-settings. I think validation is one of our requirements.

  10. melonora commented on Apr 16, 2026

    @melonora

    question is whether for all libraries it is really required to have validation, e.g. what if the config settings are not that complicated?

  11. ilan-gold commented on Apr 16, 2026

    @ilan-gold

    @melonora I don't think we should use Donfig. We definitely do need validation + typing as well, which Donfig doesn't have AFAIK

  12. melonora commented on Apr 16, 2026

    @melonora

    Can't donfig be combined with pydantic when the configs get more complicated?

    Though I agree in that case just pydantic-settings could be simpler

  13. flying-sheep commented on Apr 17, 2026

    @flying-sheep
    Member

    I think it looks promising too. I only advised against extending Pydantic ourselves, but using a solution they provide is fine if it works for us! So:

    I think we should evaluate it by each making a PR in which we port our main project to pydantic-settings and see if it works for us.

    Even if we don’t merge that PR right away, that should be enough to inform us if it’s all we need. I’d love if the answer to “should we roll our own” was “no” for once.

    PS: I didn’t have good experiences with Typer (the CLI framework based on Pydantic made by the FastAPI people). But I think Pydantic{,-settings} is maintained by different people, so that shouldn’t detract.

  14. ilia-kats commented on Apr 21, 2026

    @ilia-kats
    Collaborator

    I've ported MuData over here. There is still a bit of boilerplate required to make it nice, but I think all the boilerplate can be moved into a class in scverse-misc with an __init_subclass__ method. Then other packages can just subclass from that and define their options like a normal dataclass.

  15. flying-sheep commented on Apr 21, 2026

    @flying-sheep
    Member

    OK, I’ll do that too now! Not a bad start if I understand it correctly:

    class Settings(pydantic_settings.BaseSettings):
        preset: Annotated[Preset, AfterValidator(Preset.check)] = Preset.ScanpyV1
        """Preset to use."""
    
        ...
  16. flying-sheep commented on Apr 21, 2026

    @flying-sheep
    Member

    OK, so my partner has evaluated this one for her specific needs, and built something that matches those instead: https://gitlab.com/unitelabs/cdk/python-cdk/-/blob/main/src/unitelabs/cdk/config/config.py?ref_type=heads

    She says that

    1. pydantic-settings only adds env var support and CLI override support, combined with nested namespaces with envvars and so on. pydantic-settings has certain invariants, such multiple config sources (you e.g. get exactly one .env config file). We might simply want dotenv.load combined with raw pydantic.
    2. She also needed
      • JSON schema generation (probably not relevant for us)
      • settings depending on each other (very relevant for us!)

    she has solved these in the above linked pydantic wrapper, maybe we can base our code on that!

  17. ilia-kats commented on Apr 21, 2026

    @ilia-kats
    Collaborator

    As far as I understand the code you linked, the delayed_default / DelayedDefault handling can be implemented using PyDantic default factories (last example, The default factory can also take a single required argument, in which case the already validated data will be passed as a dictionary.)

    The dotenv is a good point, I didn't pay attention to that. However, we can just use env_file=dotenv.find_dotenv() in the SettingsConfigDict (I just tried it and it works). Since pydantic-settings already depends on dotenv, this doesn't add any additional dependencies and we still get env var support from pydantic-settings.

  18. flying-sheep commented on Apr 21, 2026

    @flying-sheep
    Member

    the set of fields that get passed in depends on field order, whereas delayed_default resolves anything but cycles IIRC.

  19. ilia-kats commented on Apr 21, 2026

    @ilia-kats
    Collaborator

    It does cycle, but it doesn't handle fully cyclic dependencies. I guess one can imagine some situation where a default depends on multiple other variables and gets resolved after a few cycles without exceeding the maximum recursion depth, but I think in the vast majority of usecases the dependencies will be a DAG. And placing fields after their dependencies in the class feels sort of natural. But I guess it will depend on how complex the dependencies get. So I would suggest starting with the plain default factories, and implementing something like delayed_default if those prove too limiting.

  20. flying-sheep commented on Apr 23, 2026

    @flying-sheep
    Member

    Yeah. Apart from this arbitrary limitation, pydantic seems to have the necessary flexibility, we just need to really understand the API to be effective with it.

    It took me a while to understand that there is a way to have a hook that allows accessing the instance after setting a property:

    1. you need to set model_config = ConfigDict(validate_assignment=True)
    2. use @model_validator(mode="wrap"), call self = handler(data) in it to create an instance and check if "my_field" in data to see if we’re setting that one.

    Only issue is that it says “data can be any object”. How? What can data be apart from a dict and when?

  21. ilia-kats commented on Apr 23, 2026

    @ilia-kats
    Collaborator

    As far as I can tell, the Any type hint is a consequence of the validator being called from Rust and it can be called with any type that implements the Input trait. As far as I can tell, this is implemented for all Python objects because it's used for all validation (look at all the trait methods validate_float, validate_list etc), not just the wrapped model validator callback. But for our usecase it's probably always a dict.

  22. flying-sheep commented on Apr 23, 2026

    @flying-sheep
    Member

    Yeah. So I think if we adopt this, this change (scverse/scanpy#4081) would be positive for scanpy: less code for validation logic and transferable skills for the more complicated bits. What do you think from a mudata perspective?

    I also commented here about the docs part: #12 (comment)

  23. ilia-kats commented on Apr 23, 2026

    @ilia-kats
    Collaborator

    We only have very simple settings in MuData without extensive dependencies and validation, so it definitely works. I've already started moving all the boilerplate into scverse-misc and will open a PR either today or tomorrow.

    Re docs: Good idea in principle, but I'm not sure at the moment what functionality is needed there and how to do this in a generic way. This definitely needs some more thinking and discussion. I think for now we stick with the custom solution and move to the generic one if and when it materializes.

  24. ilia-kats commented on Apr 24, 2026

    @ilia-kats
    Collaborator

    To keep the discussion going: The first part (general settings base class) is now implemented, question is how we do the common options. I imagine something like a global settings class that subclasses the new Settings, let's call it GlobalSettings, and a SCVERSE_GLOBAL sentinel value. Using another base class inheriting from Settings, let's call it PackageSettings, we can check on attribute access if a value is set to SCVERSE_GLOBAL and return the global setting in that case, otherwise the actual value. Packages could then subclass PackageSettings.

    This scheme allows users to change settings for specific packages, but I'm not sure where GlobalSettings is supposed to live so user can change global settings as well. Putting it into scverse-misc feels weird, since this is mostly a developer-oriented package. So far I can see three options:

    1. have packages re-export the global settings instance
    2. make a new package just for this
    3. add a context manager to PackageSettings so users can do something like
    with settings.set_global_settings:
        settings.n_jobs = 42
  25. flying-sheep commented on Apr 24, 2026

    @flying-sheep
    Member

    let’s open a new issue for common options! #17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions