Skip to content

Commit 2f66c29

Browse files
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 <[email protected]> --------- Co-authored-by: T. Franzel <[email protected]>
1 parent 337ba21 commit 2f66c29

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

docs/api-guide/schemas.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ The following sections explain more.
5656

5757
### Install dependencies
5858

59-
pip install pyyaml uritemplate
59+
pip install pyyaml uritemplate inflection
6060

6161
* `pyyaml` is used to generate schema into YAML-based OpenAPI format.
6262
* `uritemplate` is used internally to get parameters in path.
63+
* `inflection` is used to pluralize operations more appropriately in the list endpoints.
6364

6465
### Generating a static schema with the `generateschema` management command
6566

rest_framework/compat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def unicode_http_header(value):
4646
except ImportError:
4747
yaml = None
4848

49+
# inflection is optional
50+
try:
51+
import inflection
52+
except ImportError:
53+
inflection = None
54+
4955

5056
# requests is optional
5157
try:

rest_framework/schemas/openapi.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from rest_framework import (
1515
RemovedInDRF315Warning, exceptions, renderers, serializers
1616
)
17-
from rest_framework.compat import uritemplate
17+
from rest_framework.compat import inflection, uritemplate
1818
from rest_framework.fields import _UnvalidatedField, empty
1919
from rest_framework.settings import api_settings
2020

@@ -247,9 +247,8 @@ def get_operation_id_base(self, path, method, action):
247247
name = name[:-len(action)]
248248

249249
if action == 'list':
250-
from inflection import pluralize
251-
252-
name = pluralize(name)
250+
assert inflection, '`inflection` must be installed for OpenAPI schema support.'
251+
name = inflection.pluralize(name)
253252

254253
return name
255254

0 commit comments

Comments
 (0)