Skip to content

Commit e794e5e

Browse files
authored
blacken-docs (#8906)
* blacken-docs: Manual fixes for command to pass without errors * blacken-docs: Adds blacken-docs step to precommit hook. * blacken-docs: Adds changes made by command itself. * blacken-docs: Modifies blacken-docs step to only process files under "docs" directory * blacken-docs: Updates pre-commit config file to exclude all directories other than "docs" to be compatible with "--all-files" flag. * blacken-docs: Adds commas at the end to make it look identical to pre-black version
1 parent 18b02ce commit e794e5e

10 files changed

+87
-61
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ repos:
1818
- id: flake8
1919
additional_dependencies:
2020
- flake8-tidy-imports
21+
- repo: https://github.com/adamchainz/blacken-docs
22+
rev: 1.13.0
23+
hooks:
24+
- id: blacken-docs
25+
exclude: ^(?!docs).*$
26+
additional_dependencies:
27+
- black==23.1.0

docs/api-guide/caching.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,33 @@ from rest_framework import viewsets
2828

2929
class UserViewSet(viewsets.ViewSet):
3030
# With cookie: cache requested url for each user for 2 hours
31-
@method_decorator(cache_page(60*60*2))
31+
@method_decorator(cache_page(60 * 60 * 2))
3232
@method_decorator(vary_on_cookie)
3333
def list(self, request, format=None):
3434
content = {
35-
'user_feed': request.user.get_user_feed()
35+
"user_feed": request.user.get_user_feed(),
3636
}
3737
return Response(content)
3838

3939

4040
class ProfileView(APIView):
4141
# With auth: cache requested url for each user for 2 hours
42-
@method_decorator(cache_page(60*60*2))
43-
@method_decorator(vary_on_headers("Authorization",))
42+
@method_decorator(cache_page(60 * 60 * 2))
43+
@method_decorator(vary_on_headers("Authorization"))
4444
def get(self, request, format=None):
4545
content = {
46-
'user_feed': request.user.get_user_feed()
46+
"user_feed": request.user.get_user_feed(),
4747
}
4848
return Response(content)
4949

5050

5151
class PostView(APIView):
5252
# Cache page for the requested url
53-
@method_decorator(cache_page(60*60*2))
53+
@method_decorator(cache_page(60 * 60 * 2))
5454
def get(self, request, format=None):
5555
content = {
56-
'title': 'Post title',
57-
'body': 'Post content'
56+
"title": "Post title",
57+
"body": "Post content",
5858
}
5959
return Response(content)
6060
```

docs/api-guide/schemas.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ urlpatterns = [
9494
# Use the `get_schema_view()` helper to add a `SchemaView` to project URLs.
9595
# * `title` and `description` parameters are passed to `SchemaGenerator`.
9696
# * Provide view name for use with `reverse()`.
97-
path('openapi', get_schema_view(
98-
title="Your Project",
99-
description="API for all things …",
100-
version="1.0.0"
101-
), name='openapi-schema'),
97+
path(
98+
"openapi",
99+
get_schema_view(
100+
title="Your Project", description="API for all things …", version="1.0.0"
101+
),
102+
name="openapi-schema",
103+
),
102104
# ...
103105
]
104106
```
@@ -259,11 +261,13 @@ class CustomSchema(AutoSchema):
259261
"""
260262
AutoSchema subclass using schema_extra_info on the view.
261263
"""
264+
262265
...
263266

267+
264268
class CustomView(APIView):
265269
schema = CustomSchema()
266-
schema_extra_info = ... some extra info ...
270+
schema_extra_info = ... # some extra info
267271
```
268272

269273
Here, the `AutoSchema` subclass goes looking for `schema_extra_info` on the
@@ -278,10 +282,13 @@ class BaseSchema(AutoSchema):
278282
"""
279283
AutoSchema subclass that knows how to use extra_info.
280284
"""
285+
281286
...
282287

288+
283289
class CustomSchema(BaseSchema):
284-
extra_info = ... some extra info ...
290+
extra_info = ... # some extra info
291+
285292

286293
class CustomView(APIView):
287294
schema = CustomSchema()
@@ -302,10 +309,9 @@ class CustomSchema(BaseSchema):
302309
self.extra_info = kwargs.pop("extra_info")
303310
super().__init__(**kwargs)
304311

312+
305313
class CustomView(APIView):
306-
schema = CustomSchema(
307-
extra_info=... some extra info ...
308-
)
314+
schema = CustomSchema(extra_info=...) # some extra info
309315
```
310316

311317
This saves you having to create a custom subclass per-view for a commonly used option.

docs/api-guide/viewsets.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,16 @@ To view all extra actions, call the `.get_extra_actions()` method.
201201
Extra actions can map additional HTTP methods to separate `ViewSet` methods. For example, the above password set/unset methods could be consolidated into a single route. Note that additional mappings do not accept arguments.
202202

203203
```python
204-
@action(detail=True, methods=['put'], name='Change Password')
205-
def password(self, request, pk=None):
206-
"""Update the user's password."""
207-
...
208-
209-
@password.mapping.delete
210-
def delete_password(self, request, pk=None):
211-
"""Delete the user's password."""
212-
...
204+
@action(detail=True, methods=["put"], name="Change Password")
205+
def password(self, request, pk=None):
206+
"""Update the user's password."""
207+
...
208+
209+
210+
@password.mapping.delete
211+
def delete_password(self, request, pk=None):
212+
"""Delete the user's password."""
213+
...
213214
```
214215

215216
## Reversing action URLs
@@ -220,14 +221,14 @@ Note that the `basename` is provided by the router during `ViewSet` registration
220221

221222
Using the example from the previous section:
222223

223-
```python
224-
>>> view.reverse_action('set-password', args=['1'])
224+
```pycon
225+
>>> view.reverse_action("set-password", args=["1"])
225226
'http://localhost:8000/api/users/1/set_password'
226227
```
227228

228229
Alternatively, you can use the `url_name` attribute set by the `@action` decorator.
229230

230-
```python
231+
```pycon
231232
>>> view.reverse_action(view.set_password.url_name, args=['1'])
232233
'http://localhost:8000/api/users/1/set_password'
233234
```

docs/community/3.10-announcement.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ update your REST framework settings to include `DEFAULT_SCHEMA_CLASS` explicitly
4141

4242
```python
4343
REST_FRAMEWORK = {
44-
...
45-
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
44+
...: ...,
45+
"DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema",
4646
}
4747
```
4848

@@ -74,10 +74,11 @@ urlpatterns = [
7474
# Use the `get_schema_view()` helper to add a `SchemaView` to project URLs.
7575
# * `title` and `description` parameters are passed to `SchemaGenerator`.
7676
# * Provide view name for use with `reverse()`.
77-
path('openapi', get_schema_view(
78-
title="Your Project",
79-
description="API for all things …"
80-
), name='openapi-schema'),
77+
path(
78+
"openapi",
79+
get_schema_view(title="Your Project", description="API for all things …"),
80+
name="openapi-schema",
81+
),
8182
# ...
8283
]
8384
```

docs/community/3.11-announcement.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ be extracted from the class docstring:
4343

4444
```python
4545
class DocStringExampleListView(APIView):
46-
"""
47-
get: A description of my GET operation.
48-
post: A description of my POST operation.
49-
"""
46+
"""
47+
get: A description of my GET operation.
48+
post: A description of my POST operation.
49+
"""
50+
5051
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
5152

5253
def get(self, request, *args, **kwargs):

docs/community/3.12-announcement.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The tags used for a particular view may also be overridden...
4141

4242
```python
4343
class MyOrders(APIView):
44-
schema = AutoSchema(tags=['users', 'orders'])
44+
schema = AutoSchema(tags=["users", "orders"])
4545
...
4646
```
4747

@@ -68,7 +68,7 @@ may be overridden if needed](https://www.django-rest-framework.org/api-guide/sch
6868

6969
```python
7070
class MyOrders(APIView):
71-
schema = AutoSchema(component_name="OrderDetails")
71+
schema = AutoSchema(component_name="OrderDetails")
7272
```
7373

7474
## More Public API
@@ -118,10 +118,11 @@ class SitesSearchView(generics.ListAPIView):
118118
by a search against the site name or location. (Location searches are
119119
matched against the region and country names.)
120120
"""
121+
121122
queryset = Sites.objects.all()
122123
serializer_class = SitesSerializer
123124
filter_backends = [filters.SearchFilter]
124-
search_fields = ['site_name', 'location__region', 'location__country']
125+
search_fields = ["site_name", "location__region", "location__country"]
125126
```
126127

127128
### Searches against annotate fields
@@ -135,10 +136,11 @@ class PublisherSearchView(generics.ListAPIView):
135136
Search for publishers, optionally filtering the search against the average
136137
rating of all their books.
137138
"""
138-
queryset = Publisher.objects.annotate(avg_rating=Avg('book__rating'))
139+
140+
queryset = Publisher.objects.annotate(avg_rating=Avg("book__rating"))
139141
serializer_class = PublisherSerializer
140142
filter_backends = [filters.SearchFilter]
141-
search_fields = ['avg_rating']
143+
search_fields = ["avg_rating"]
142144
```
143145

144146
---

docs/community/3.9-announcement.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ from rest_framework.renderers import JSONOpenAPIRenderer
6565
from django.urls import path
6666

6767
schema_view = get_schema_view(
68-
title='Server Monitoring API',
69-
url='https://www.example.org/api/',
70-
renderer_classes=[JSONOpenAPIRenderer]
68+
title="Server Monitoring API",
69+
url="https://www.example.org/api/",
70+
renderer_classes=[JSONOpenAPIRenderer],
7171
)
7272

73-
urlpatterns = [
74-
path('schema.json', schema_view),
75-
...
76-
]
73+
urlpatterns = [path("schema.json", schema_view), ...]
7774
```
7875

7976
And here's how you can use the `generateschema` management command:

docs/community/release-notes.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ Be sure to upgrade to Python 3 before upgrading to Django REST Framework 3.10.
306306
class NullableCharField(serializers.CharField):
307307
def __init__(self, *args, **kwargs):
308308
super().__init__(*args, **kwargs)
309-
self.validators = [v for v in self.validators if not isinstance(v, ProhibitNullCharactersValidator)]
309+
self.validators = [
310+
v
311+
for v in self.validators
312+
if not isinstance(v, ProhibitNullCharactersValidator)
313+
]
310314
```
311315
* Add `OpenAPIRenderer` and `generate_schema` management command. [#6229][gh6229]
312316
* Add OpenAPIRenderer by default, and add schema docs. [#6233][gh6233]

docs/topics/documenting-your-api.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ urlpatterns = [
9696
# ...
9797
# Route TemplateView to serve Swagger UI template.
9898
# * Provide `extra_context` with view name of `SchemaView`.
99-
path('swagger-ui/', TemplateView.as_view(
100-
template_name='swagger-ui.html',
101-
extra_context={'schema_url':'openapi-schema'}
102-
), name='swagger-ui'),
99+
path(
100+
"swagger-ui/",
101+
TemplateView.as_view(
102+
template_name="swagger-ui.html",
103+
extra_context={"schema_url": "openapi-schema"},
104+
),
105+
name="swagger-ui",
106+
),
103107
]
104108
```
105109

@@ -145,10 +149,13 @@ urlpatterns = [
145149
# ...
146150
# Route TemplateView to serve the ReDoc template.
147151
# * Provide `extra_context` with view name of `SchemaView`.
148-
path('redoc/', TemplateView.as_view(
149-
template_name='redoc.html',
150-
extra_context={'schema_url':'openapi-schema'}
151-
), name='redoc'),
152+
path(
153+
"redoc/",
154+
TemplateView.as_view(
155+
template_name="redoc.html", extra_context={"schema_url": "openapi-schema"}
156+
),
157+
name="redoc",
158+
),
152159
]
153160
```
154161

0 commit comments

Comments
 (0)