Skip to content

Commit

Permalink
Fix issue with string args in GenericRelation.
Browse files Browse the repository at this point in the history
Fixes sphinx-doc#42.

The tests intended to capture the issue but actually
don't trigger the real issue in the test scenario,
so they may not be appropriate. I tried making
a 2nd app to see if the deferred issue would show
up but it does not.
  • Loading branch information
ntouran committed Aug 16, 2023
1 parent 872cb8d commit 224c4f9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
10 changes: 7 additions & 3 deletions sphinxcontrib_django/docstrings/field_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ def get_field_verbose_name(field):
related_name = (
related_name or field.remote_field.model._meta.verbose_name_plural
)
# If field is a foreign key or a ManyToMany field, use the prefix "All"
verbose_name = (
f"All {related_name} of this {field.model._meta.verbose_name}"
# handle potential str-type deferred model names
forward_name = (
field.model
if isinstance(field.model, str)
else field.model._meta.verbose_name
)
# If field is a foreign key or a ManyToMany field, use the prefix "All"
verbose_name = f"All {related_name} of this {forward_name}"
# Link to the origin of the reverse related field if it's not from an abstract model
if not field.remote_field.model._meta.abstract:
verbose_name += (
Expand Down
1 change: 1 addition & 0 deletions tests/roots/test-docstrings/dummy_django_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"dummy_django_app",
"dummy_django_app2",
]

USE_TZ = False
7 changes: 7 additions & 0 deletions tests/roots/test-docstrings/dummy_django_app2/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models


class GenericRelationModel(models.Model):
# specifically test deferred string-type argument
relation_field = GenericRelation("dummy_django_app.TaggedItem")
25 changes: 25 additions & 0 deletions tests/test_attribute_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,28 @@ def test_phonenumber_field(app, do_autodoc):
" Phone number",
"",
]


@pytest.mark.sphinx("html", testroot="docstrings")
def test_generic_relation_field(app, do_autodoc):
actual = do_autodoc(
app, "attribute", "dummy_django_app2.models.GenericRelationModel.relation_field"
)
print(actual)
assert list(actual) == [
"",
".. py:attribute:: GenericRelationModel.relation_field",
" :module: dummy_django_app2.models",
"",
(
" Type: Reverse"
" :class:`~django.contrib.contenttypes.fields.GenericRelation` from"
" :class:`~dummy_django_app2.models.GenericRelationModel`"
),
"",
(
" All + of this tagged item (related name of"
" :attr:`~dummy_django_app2.models.GenericRelationModel.relation_field`)"
),
"",
]
27 changes: 27 additions & 0 deletions tests/test_class_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,30 @@ def test_form(app, do_autodoc):
" * ``test2``: Test2 (:class:`~django.forms.CharField`)",
"",
]


@pytest.mark.sphinx("html", testroot="docstrings")
def test_relation_model(app, do_autodoc):
actual = do_autodoc(app, "class", "dummy_django_app2.models.GenericRelationModel")
print(actual)
assert list(actual) == [
"",
".. py:class:: GenericRelationModel(id)",
" :module: dummy_django_app2.models",
"",
" :param id: Primary key: ID",
" :type id: ~django.db.models.AutoField",
"",
" Relationship fields:",
"",
(
" :param relation_field: Relation field (related name:"
" :attr:`~dummy_django_app.models.TaggedItem.+`)"
),
(
" :type relation_field:"
" :class:`~django.contrib.contenttypes.fields.GenericRelation` to"
" :class:`~dummy_django_app.models.TaggedItem`"
),
"",
]
6 changes: 3 additions & 3 deletions tests/test_data_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ def test_data(app, do_autodoc):
" :module: dummy_django_app.settings",
(
" :value: ['django.contrib.auth', 'django.contrib.contenttypes',"
" 'dummy_django_app']"
" 'dummy_django_app', 'dummy_django_app2']"
),
"",
" These are the installed apps",
"",
" .. code-block:: JavaScript",
"",
(
" ['django.contrib.auth', 'django.contrib.contenttypes',"
" 'dummy_django_app']\n"
" [\n 'django.contrib.auth',\n 'django.contrib.contenttypes',\n"
" 'dummy_django_app',\n 'dummy_django_app2',\n]\n"
),
"",
]

0 comments on commit 224c4f9

Please sign in to comment.