You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
code-organization-wise we could pull out the documentation features and so on into submodules for clarifying core functionality.
usability-wise, we could add default validators and parsers for known types.
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.
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
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
Whatever is exported should probably have sensible settings baked in like zarr-v3 default or n jobs
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.
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)
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.
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.
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.
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.
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.
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!
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.
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.
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:
you need to set model_config = ConfigDict(validate_assignment=True)
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?
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.
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)
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.
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:
have packages re-export the global settings instance
make a new package just for this
add a context manager to PackageSettings so users can do something like
Activity
ilia-kats commentedon Apr 16, 2026
What should that look like? As far as I can tell, there are at least two options:
@settingsdecorator that wraps@dataclassand adds the additional methods to the class.ilan-gold commentedon Apr 16, 2026
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,
pyifiles are easy to make. But I might be missing something.flying-sheep commentedon Apr 16, 2026
So Ilia’s approach needs a type stub too as the
get,set, andoverridemethods 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:
typing.Annotatedor 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.
ilia-kats commentedon Apr 16, 2026
Is there any advantage of having to do
settings.registerinstead 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 anoverridecontext manager methodilan-gold commentedon Apr 16, 2026
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
propertywith a setter etc IMO.But I think like @grst said there are two different cases here - shared settings and package-specific settings.
n_jobsdoesn'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
Are there are other requirements?
ilia-kats commentedon Apr 16, 2026
You wouldn't need to do this with pydantic(-settings), since it will validate upon assignment (if configured with
validate_assignment=True)melonora commentedon Apr 16, 2026
What would you think of using
Donfig, similar to whatzarr-pythondoes? It only haspyyamlas dependency so it is very light and most of the time is already included as it is included with zarr.ilia-kats commentedon Apr 16, 2026
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.
melonora commentedon Apr 16, 2026
question is whether for all libraries it is really required to have validation, e.g. what if the config settings are not that complicated?
ilan-gold commentedon Apr 16, 2026
@melonora I don't think we should use Donfig. We definitely do need validation + typing as well, which Donfig doesn't have AFAIK
melonora commentedon Apr 16, 2026
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
flying-sheep commentedon Apr 17, 2026
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.
ilia-kats commentedon Apr 21, 2026
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.flying-sheep commentedon Apr 21, 2026
OK, I’ll do that too now! Not a bad start if I understand it correctly:
flying-sheep commentedon Apr 21, 2026
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
pydantic-settingsonly adds env var support and CLI override support, combined with nested namespaces with envvars and so on.pydantic-settingshas certain invariants, such multiple config sources (you e.g. get exactly one.envconfig file). We might simply wantdotenv.loadcombined with rawpydantic.she has solved these in the above linked pydantic wrapper, maybe we can base our code on that!
ilia-kats commentedon Apr 21, 2026
As far as I understand the code you linked, the
delayed_default/DelayedDefaulthandling 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 theSettingsConfigDict(I just tried it and it works). Sincepydantic-settingsalready depends ondotenv, this doesn't add any additional dependencies and we still get env var support frompydantic-settings.flying-sheep commentedon Apr 21, 2026
the set of fields that get passed in depends on field order, whereas
delayed_defaultresolves anything but cycles IIRC.ilia-kats commentedon Apr 21, 2026
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_defaultif those prove too limiting.flying-sheep commentedon Apr 23, 2026
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:
model_config = ConfigDict(validate_assignment=True)@model_validator(mode="wrap"), callself = handler(data)in it to create an instance and check if"my_field" in datato see if we’re setting that one.Only issue is that it says “data can be any object”. How? What can
databe apart from a dict and when?ilia-kats commentedon Apr 23, 2026
As far as I can tell, the
Anytype hint is a consequence of the validator being called from Rust and it can be called with any type that implements theInputtrait. 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 methodsvalidate_float,validate_listetc), not just the wrapped model validator callback. But for our usecase it's probably always a dict.flying-sheep commentedon Apr 23, 2026
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)
ilia-kats commentedon Apr 23, 2026
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.
ilia-kats commentedon Apr 24, 2026
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 itGlobalSettings, and aSCVERSE_GLOBALsentinel value. Using another base class inheriting fromSettings, let's call itPackageSettings, we can check on attribute access if a value is set toSCVERSE_GLOBALand return the global setting in that case, otherwise the actual value. Packages could then subclassPackageSettings.This scheme allows users to change settings for specific packages, but I'm not sure where
GlobalSettingsis supposed to live so user can change global settings as well. Putting it intoscverse-miscfeels weird, since this is mostly a developer-oriented package. So far I can see three options:PackageSettingsso users can do something likeflying-sheep commentedon Apr 24, 2026
let’s open a new issue for common options! #17