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

Error when inherit a class that is inherited from pydantic.BaseModel #291

Open Fazatholomew opened 10 months ago

Fazatholomew commented 10 months ago

This problem seems happening with sphinx.automodule and autodoc. People solve it with this library. However, I am not sure how to apply the fix with sphinx-immaterial.

from pydantic import BaseModel

class Foo(BaseModel):
  value: str

class Bar(Foo):
  key: str

This is the error:

WARNING: autodoc: failed to determine send_the_raven.address::BaseModel.__pydantic_validator__ (<pydantic._internal._mock_val_ser.MockValSer object at 0x1123ab580>) to be documented, the following exception was raised:
Handler <function _skip_member at 0x11214f9a0> for event 'autodoc-skip-member' threw an exception (exception: Pydantic models should inherit from BaseModel, BaseModel cannot be instantiated directly
2bndy5 commented 10 months ago

Other than the fact that this theme requires pydantic v2.0+, I'm not sure how this question is related to this theme.

Fazatholomew commented 10 months ago

Other than the fact that this theme requires pydantic v2.0+, I'm not sure how this question is related to this theme.

I indeed using pydantic v2.0+. I encounter this issue while trying to use the Python API documentation generation. If this is not the appropriate place, where do yout think I should create an issue instead?

2bndy5 commented 10 months ago

autodoc is an extension that ships with Sphinx. Unless I read something wrong, I don't see anything in your comments that specify this theme (sphinx-immaterial) as a cause of the problem.

jbms commented 10 months ago

You could try just using the autodoc_pydantic extension, though it is likely our Python apigen extension will need some changes to work with it.

Fazatholomew commented 9 months ago

You could try just using the autodoc_pydantic extension, though it is likely our Python apigen extension will need some changes to work with it.

I just tried it and it went haywire quick. I was hoping someone here will be able to help. When I installed the autodoc_pydantic the only meaningful error I can get is:

File "~/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/json_schema.py", line 2124, in model_json_schema
    assert '__pydantic_core_schema__' in cls.__dict__, 'this is a bug! please report it'
AssertionError: this is a bug! please report it

Which looks like a pydantic error? Do you have any suggestion on how to make changes in this theme apigen config? There is an issue that talks about this. Unfortunately, in order to activate this feature, I need to put :inherited-members: BaseModel under .. automodule::. How could I do this in this theme?

2bndy5 commented 9 months ago

Use sphinx-build -v ... and post the full trace back error. I suspect this is a pydantic error.

Fazatholomew commented 9 months ago

No config on auto_pydantic

Running Sphinx v5.3.0
locale_dir /Users/jimboy/Documents/CS/send-the-raven/source/locales/en/LC_MESSAGES does not exists

Traceback (most recent call last):
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/events.py", line 94, in emit
    results.append(listener.handler(self.app, *args))
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx_immaterial/apidoc/python/apigen.py", line 1663, in _builder_inited
    _ApiEntityCollector(
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx_immaterial/apidoc/python/apigen.py", line 1529, in collect_documenter_members
    member_canonical_object_name = self.collect_entity_recursively(entry)
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx_immaterial/apidoc/python/apigen.py", line 1452, in collect_entity_recursively
    entry.documenter.generate()
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/ext/autodoc/__init__.py", line 1798, in generate
    return super().generate(more_content=more_content,
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/ext/autodoc/__init__.py", line 951, in generate
    self.add_content(more_content)
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinxcontrib/autodoc_pydantic/directives/autodocumenters.py", line 290, in add_content
    self.add_collapsable_schema()
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinxcontrib/autodoc_pydantic/directives/autodocumenters.py", line 328, in add_collapsable_schema
    schema = self.pydantic.inspect.schema.sanitized
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinxcontrib/autodoc_pydantic/inspection.py", line 420, in sanitized
    schema = self.model.model_json_schema()
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/main.py", line 386, in model_json_schema
    return model_json_schema(
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/json_schema.py", line 2124, in model_json_schema
    assert '__pydantic_core_schema__' in cls.__dict__, 'this is a bug! please report it'
AssertionError: this is a bug! please report it

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/cmd/build.py", line 276, in build_main
    app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/application.py", line 262, in __init__
    self._init_builder()
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/application.py", line 335, in _init_builder
    self.events.emit('builder-inited')
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/events.py", line 105, in emit
    raise ExtensionError(__("Handler %r for event %r threw an exception") %
sphinx.errors.ExtensionError: Handler <function _builder_inited at 0x10b00d2d0> for event 'builder-inited' threw an exception (exception: this is a bug! please report it)

Extension error (sphinx_immaterial.apidoc.python.apigen):
Handler <function _builder_inited at 0x10b00d2d0> for event 'builder-inited' threw an exception (exception: this is a bug! please report it)
make: *** [html] Error 2

With autodoc_pydantic config in conf.py

autodoc_pydantic_model_show_json = False
autodoc_pydantic_model_show_config_summary = False

Trace back

Running Sphinx v5.3.0
locale_dir /Users/jimboy/Documents/CS/send-the-raven/source/locales/en/LC_MESSAGES does not exists
WARNING: error while formatting arguments for send_the_raven.address.normalize_address_record: Handler <function update_annotations_using_type_comments at 0x10ef6b520> for event 'autodoc-before-process-signature' threw an exception (exception: list index out of range)
WARNING: error while formatting arguments for send_the_raven.address.normalize_address_record: Handler <function update_annotations_using_type_comments at 0x10ef6b520> for event 'autodoc-before-process-signature' threw an exception (exception: list index out of range)

Traceback (most recent call last):
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/events.py", line 94, in emit
    results.append(listener.handler(self.app, *args))
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx_immaterial/apidoc/python/apigen.py", line 1649, in _builder_inited
    importlib.import_module(module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/jimboy/Documents/CS/send-the-raven/send_the_raven/geoaddress.py", line 17, in <module>
    class GeoAddress(Address):
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 177, in __new__
    set_model_fields(cls, bases, config_wrapper, types_namespace)
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 405, in set_model_fields
    fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/pydantic/_internal/_fields.py", line 119, in collect_model_fields
    raise NameError(
NameError: Field "model_fields" conflicts with member {'full_address': FieldInfo(annotation=Union[str, NoneType], required=False), 'street': FieldInfo(annotation=Union[str, NoneType], required=False), 'address_line_2': FieldInfo(annotation=Union[str, NoneType], required=False), 'city': FieldInfo(annotation=Union[str, NoneType], required=False), 'state': FieldInfo(annotation=Union[str, NoneType], required=False), 'zip_code': FieldInfo(annotation=Union[str, NoneType], required=False), 'id': FieldInfo(annotation=Union[str, NoneType], required=False, default='2f684c9ccc8990a')} of protected namespace "model_".

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/cmd/build.py", line 276, in build_main
    app = Sphinx(args.sourcedir, args.confdir, args.outputdir,
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/application.py", line 262, in __init__
    self._init_builder()
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/application.py", line 335, in _init_builder
    self.events.emit('builder-inited')
  File "/Users/jimboy/Documents/CS/send-the-raven/.venv/lib/python3.10/site-packages/sphinx/events.py", line 105, in emit
    raise ExtensionError(__("Handler %r for event %r threw an exception") %
sphinx.errors.ExtensionError: Handler <function _builder_inited at 0x10f8dd2d0> for event 'builder-inited' threw an exception (exception: Field "model_fields" conflicts with member {'full_address': FieldInfo(annotation=Union[str, NoneType], required=False), 'street': FieldInfo(annotation=Union[str, NoneType], required=False), 'address_line_2': FieldInfo(annotation=Union[str, NoneType], required=False), 'city': FieldInfo(annotation=Union[str, NoneType], required=False), 'state': FieldInfo(annotation=Union[str, NoneType], required=False), 'zip_code': FieldInfo(annotation=Union[str, NoneType], required=False), 'id': FieldInfo(annotation=Union[str, NoneType], required=False, default='2f684c9ccc8990a')} of protected namespace "model_".)

Extension error (sphinx_immaterial.apidoc.python.apigen):
Handler <function _builder_inited at 0x10f8dd2d0> for event 'builder-inited' threw an exception (exception: Field "model_fields" conflicts with member {'full_address': FieldInfo(annotation=Union[str, NoneType], required=False), 'street': FieldInfo(annotation=Union[str, NoneType], required=False), 'address_line_2': FieldInfo(annotation=Union[str, NoneType], required=False), 'city': FieldInfo(annotation=Union[str, NoneType], required=False), 'state': FieldInfo(annotation=Union[str, NoneType], required=False), 'zip_code': FieldInfo(annotation=Union[str, NoneType], required=False), 'id': FieldInfo(annotation=Union[str, NoneType], required=False, default='2f684c9ccc8990a')} of protected namespace "model_".)
make: *** [html] Error 2