Skip to content

Commit be21d37

Browse files
feat: Allow empty strings for directive prefixes (#285)
docs: Add hint mentioning that pydantic directive prefixes allow empty strings
1 parent 80cf3e9 commit be21d37

File tree

6 files changed

+51
-30
lines changed

6 files changed

+51
-30
lines changed

docs/source/users/configuration.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ Contains all modifications for pydantic `BaseModel`.
178178
:directive_option: model-signature-prefix
179179
:values: pydantic model, class, foobar
180180

181-
Define the signature prefix for pydantic models.
181+
Define the signature prefix for pydantic model. This is especially useful
182+
when you want to distinguish pydantic models from other classes.
183+
184+
.. hint::
185+
186+
An empty string is also a valid value. This will remove the prefix
187+
completely.
182188

183189

184190
.. config_description:: autopydantic_model
@@ -395,7 +401,13 @@ Contains all modifications for pydantic `BaseSettings`.
395401
:directive_option: settings-signature-prefix
396402
:values: pydantic settings, class, foobar
397403

398-
Define the signature prefix for pydantic settings.
404+
Define the signature prefix for pydantic settings. This is especially useful
405+
when you want to distinguish pydantic settings from other classes.
406+
407+
.. hint::
408+
409+
An empty string is also a valid value. This will remove the prefix
410+
completely.
399411

400412

401413
.. config_description:: autopydantic_settings
@@ -575,7 +587,13 @@ Fields
575587
:enable: members, field-doc-policy=docstring
576588
:values: field, attribute, foobar
577589

578-
Define the signature prefix for pydantic field.
590+
Define the signature prefix for pydantic fields. This is especially useful
591+
when you want to distinguish pydantic fields from other attributes.
592+
593+
.. hint::
594+
595+
An empty string is also a valid value. This will remove the prefix
596+
completely.
579597

580598

581599
----------
@@ -621,7 +639,13 @@ Validators
621639
:enable: members, model-show-validator-members, undoc-members
622640
:values: validator, classmethod, foobar
623641

624-
Define the signature prefix for pydantic validator.
642+
Define the signature prefix for pydantic validators. This is especially useful
643+
when you want to distinguish pydantic validators from other attributes.
644+
645+
.. hint::
646+
647+
An empty string is also a valid value. This will remove the prefix
648+
completely.
625649

626650

627651
-------

sphinxcontrib/autodoc_pydantic/directives/directives.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class PydanticDirectiveMixin:
3333
"""Base class for pydantic directive providing common functionality."""
3434

3535
config_name: str
36-
default_prefix: str
3736

3837
def __init__(self, *args) -> None: # noqa: ANN002
3938
super().__init__(*args)
@@ -44,15 +43,18 @@ def get_signature_prefix(self, *_) -> list[Node]: # noqa: ANN002
4443

4544
config_name = f'{self.config_name}-signature-prefix'
4645
prefix = self.pyautodoc.get_value(config_name)
47-
value = prefix or self.default_prefix
46+
47+
# empty prefix should not add any nodes
48+
if prefix == '':
49+
return []
4850

4951
# account for changed signature in sphinx 4.3, see #62
5052
if sphinx.version_info < (4, 3):
51-
return f'{value} ' # type: ignore[return-value]
53+
return f'{prefix} ' # type: ignore[return-value]
5254

5355
from sphinx.addnodes import desc_sig_space
5456

55-
return [Text(value), desc_sig_space()]
57+
return [Text(prefix), desc_sig_space()]
5658

5759

5860
class PydanticModel(PydanticDirectiveMixin, PyClasslike):
@@ -67,7 +69,6 @@ class PydanticModel(PydanticDirectiveMixin, PyClasslike):
6769
)
6870

6971
config_name = 'model'
70-
default_prefix = 'class'
7172

7273

7374
class PydanticSettings(PydanticDirectiveMixin, PyClasslike):
@@ -82,7 +83,6 @@ class PydanticSettings(PydanticDirectiveMixin, PyClasslike):
8283
)
8384

8485
config_name = 'settings'
85-
default_prefix = 'class'
8686

8787

8888
class PydanticField(PydanticDirectiveMixin, PyAttribute):
@@ -102,7 +102,6 @@ class PydanticField(PydanticDirectiveMixin, PyAttribute):
102102
)
103103

104104
config_name = 'field'
105-
default_prefix = 'attribute'
106105

107106
def get_field_name(self, sig: str) -> str:
108107
"""Get field name from signature. Borrows implementation from
@@ -218,7 +217,6 @@ class PydanticValidator(PydanticDirectiveMixin, PyMethod):
218217
)
219218

220219
config_name = 'validator'
221-
default_prefix = 'classmethod'
222220

223221
def get_field_href_from_mapping(
224222
self, inspector: ModelInspector, mapping: ValidatorFieldMap

tests/test_configuration_fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,7 @@ def test_autodoc_pydantic_field_signature_prefix_directive(parse_rst):
885885

886886
# empty
887887
doctree = parse_rst(input_rst, conf={'autodoc_pydantic_field_signature_prefix': ''})
888-
prefix = desc_annotation_directive_prefix('attribute')
889-
assert_node(doctree[1][0][0], [desc_annotation, prefix])
888+
assert_node(doctree[1][0][0], [desc_addname, 'FieldSignaturePrefix.'])
890889

891890
# custom
892891
input_rst = [

tests/test_configuration_model.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This module contains tests for pydantic model configurations."""
22

33
import pytest
4-
from sphinx.addnodes import desc_annotation
4+
from sphinx.addnodes import desc_annotation, desc_addname
55
from sphinx.testing.util import assert_node
66

77
from sphinxcontrib.autodoc_pydantic.directives.autodocumenters import (
@@ -1271,8 +1271,7 @@ def test_autodoc_pydantic_model_signature_prefix_directive(parse_rst):
12711271

12721272
# empty
12731273
doctree = parse_rst(input_rst, conf={'autodoc_pydantic_model_signature_prefix': ''})
1274-
prefix = desc_annotation_directive_prefix('class')
1275-
assert_node(doctree[1][0][0], [desc_annotation, prefix])
1274+
assert_node(doctree[1][0][0], [desc_addname, 'target.configuration.'])
12761275

12771276
# custom
12781277
input_rst = [

tests/test_configuration_settings.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
from typing import List
44

5-
from sphinx.addnodes import desc_annotation
5+
from sphinx.addnodes import desc_addname, desc_annotation
66
from sphinx.testing.util import assert_node
77

88
from sphinxcontrib.autodoc_pydantic.directives.autodocumenters import (
99
PydanticSettingsDocumenter,
1010
)
11+
1112
from .compatibility import desc_annotation_directive_prefix
1213

1314
KWARGS = dict(documenter=PydanticSettingsDocumenter.objtype, deactivate_all=True)
@@ -1004,10 +1005,10 @@ def test_autodoc_pydantic_settings_signature_prefix(autodocument, parse_rst):
10041005
actual = autodocument(options_doc={'settings-signature-prefix': ''}, **kwargs)
10051006
assert result == actual
10061007

1007-
1008-
def test_autodoc_pydantic_settings_signature_prefix_directive(parse_rst):
10091008
"""Tests pydantic_settings directive."""
10101009

1010+
1011+
def test_autodoc_pydantic_settings_signature_prefix_directive(parse_rst):
10111012
# default
10121013
input_rst = [
10131014
'',
@@ -1026,8 +1027,7 @@ def test_autodoc_pydantic_settings_signature_prefix_directive(parse_rst):
10261027
doctree = parse_rst(
10271028
input_rst, conf={'autodoc_pydantic_settings_signature_prefix': ''}
10281029
)
1029-
prefix = desc_annotation_directive_prefix('class')
1030-
assert_node(doctree[1][0][0], [desc_annotation, prefix])
1030+
assert_node(doctree[1][0][0], [desc_addname, 'target.configuration.'])
10311031

10321032
# custom
10331033
input_rst = [

tests/test_configuration_validator.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
"""This module contains tests for pydantic validator configurations."""
22

33
from docutils.nodes import (
4-
paragraph,
54
emphasis,
5+
paragraph,
66
)
77
from sphinx.addnodes import (
88
desc,
9-
desc_signature,
10-
desc_name,
11-
desc_content,
12-
desc_annotation,
139
desc_addname,
14-
pending_xref,
10+
desc_annotation,
11+
desc_content,
12+
desc_name,
13+
desc_signature,
1514
index,
15+
pending_xref,
1616
)
1717
from sphinx.testing.util import assert_node
18+
1819
from sphinxcontrib.autodoc_pydantic.directives.autodocumenters import (
1920
PydanticValidatorDocumenter,
2021
)
22+
2123
from .compatibility import desc_annotation_directive_prefix
2224

2325
KWARGS = dict(documenter=PydanticValidatorDocumenter.objtype, deactivate_all=True)
@@ -449,8 +451,7 @@ def test_autodoc_pydantic_validator_signature_prefix_directive(parse_rst):
449451
doctree = parse_rst(
450452
input_rst, conf={'autodoc_pydantic_validator_signature_prefix': ''}
451453
)
452-
prefix = desc_annotation_directive_prefix('classmethod')
453-
assert_node(doctree[1][0][0], [desc_annotation, prefix])
454+
assert_node(doctree[1][0][0], [desc_addname, 'ValidatorSignaturePrefix.'])
454455

455456
# custom
456457
input_rst = [

0 commit comments

Comments
 (0)