Make inflection package truly optional (#9303)

* Make inflection package truly optional

Fix #9291

* Make inflection compat layer consistent with the others

Co-authored-by: T. Franzel <13507857+tfranzel@users.noreply.github.com>

---------

Co-authored-by: T. Franzel <13507857+tfranzel@users.noreply.github.com>
This commit is contained in:
Bruno Alla 2024-03-18 22:29:02 +00:00 committed by GitHub
parent 337ba211e8
commit 2f66c294e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 5 deletions

View File

@ -56,10 +56,11 @@ The following sections explain more.
### Install dependencies ### Install dependencies
pip install pyyaml uritemplate pip install pyyaml uritemplate inflection
* `pyyaml` is used to generate schema into YAML-based OpenAPI format. * `pyyaml` is used to generate schema into YAML-based OpenAPI format.
* `uritemplate` is used internally to get parameters in path. * `uritemplate` is used internally to get parameters in path.
* `inflection` is used to pluralize operations more appropriately in the list endpoints.
### Generating a static schema with the `generateschema` management command ### Generating a static schema with the `generateschema` management command

View File

@ -46,6 +46,12 @@ try:
except ImportError: except ImportError:
yaml = None yaml = None
# inflection is optional
try:
import inflection
except ImportError:
inflection = None
# requests is optional # requests is optional
try: try:

View File

@ -14,7 +14,7 @@ from django.utils.encoding import force_str
from rest_framework import ( from rest_framework import (
RemovedInDRF315Warning, exceptions, renderers, serializers RemovedInDRF315Warning, exceptions, renderers, serializers
) )
from rest_framework.compat import uritemplate from rest_framework.compat import inflection, uritemplate
from rest_framework.fields import _UnvalidatedField, empty from rest_framework.fields import _UnvalidatedField, empty
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -247,9 +247,8 @@ class AutoSchema(ViewInspector):
name = name[:-len(action)] name = name[:-len(action)]
if action == 'list': if action == 'list':
from inflection import pluralize assert inflection, '`inflection` must be installed for OpenAPI schema support.'
name = inflection.pluralize(name)
name = pluralize(name)
return name return name