jbms / sphinx-immaterial

Adaptation of the popular mkdocs-material material design theme to the sphinx documentation system
https://jbms.github.io/sphinx-immaterial/
Other
177 stars 28 forks source link

pydantic v2.0 breaking custom admonitions #268

Closed 2bndy5 closed 1 year ago

2bndy5 commented 1 year ago

pydantic just released v2.0.

Now I'm getting errors related to the CustomAdmonitionConfig dataclass:

Traceback ``` Running Sphinx v7.0.1 Traceback (most recent call last): File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/cmd/build.py", line 280, in build_main app = Sphinx(args.sourcedir, args.confdir, args.outputdir, File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/application.py", line 229, in __init__ self.setup_extension(extension) File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/application.py", line 402, in setup_extension self.registry.load_extension(self, extname) File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/registry.py", line 442, in load_extension metadata = setup(app) File "/home/brendan/.local/lib/python3.10/site-packages/sphinx_immaterial/__init__.py", line 297, in setup app.setup_extension("sphinx_immaterial.custom_admonitions") File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/application.py", line 402, in setup_extension self.registry.load_extension(self, extname) File "/home/brendan/.local/lib/python3.10/site-packages/sphinx/registry.py", line 429, in load_extension mod = import_module(extname) File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1050, in _gcd_import File "", line 1027, in _find_and_load File "", line 1006, in _find_and_load_unlocked File "", line 688, in _load_unlocked File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "/home/brendan/.local/lib/python3.10/site-packages/sphinx_immaterial/custom_admonitions.py", line 32, in class CustomAdmonitionConfig(pydantic.BaseModel): File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 172, in __new__ complete_model_class( File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 420, in complete_model_class schema = cls.__get_pydantic_core_schema__(cls, handler) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/main.py", line 533, in __get_pydantic_core_schema__ return __handler(__source) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__ schema = self._handler(__source_type) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 264, in generate_schema return self._generate_schema_for_type( File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 285, in _generate_schema_for_type schema = self._generate_schema(obj) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 475, in _generate_schema return self._model_schema(obj) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 351, in _model_schema {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()}, File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 351, in {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()}, File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 614, in _generate_md_field_schema common_field = self._common_field_schema(name, field_info, decorators) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 670, in _common_field_schema schema = apply_each_item_validators(schema, each_item_validators) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 148, in apply_each_item_validators schema['schema'] = apply_each_item_validators(schema['schema'], each_item_validators) File "/home/brendan/.local/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 163, in apply_each_item_validators raise TypeError( TypeError: `@validator(..., each_item=True)` cannot be applied to fields with a schema of tuple-positional ```

I think we need to simply update the validator for custom admonitions' color & classes config fields. See pydantic's migration guide. However, there could be other breaking changes that haven't surfaced yet.

We should also discuss pinning our dependence of pydantic. Although, this idea is subject to a solution to this issue.

2bndy5 commented 1 year ago

So, I devised a solution for this, but it would mean pinning pydantic to v2+ because there are so many breaking changes that supporting pydantic v1.x and v2.x is rather impossible.

I think they switched their codebase to rust in v2 which yields a performance increase (naturally).

I also found out that there's an optional package to validate color values. Using this as an additional dependency would relax the constraints on the custom admonitions color config value (which is currently limited to only a 3-tuple of integers).