Debian Codegraph @

docs/community/contributing.md

raw
1 # Contributing to REST framework
2
3 > The world can only really be changed one piece at a time. The art is picking that piece.
4 >
5 > — [Tim Berners-Lee][cite]
6
7 There are many ways you can contribute to Django REST framework. We'd like it to be a community-led project, so please get involved and help shape the future of the project.
8
9 !!! note
10
11 At this point in its lifespan we consider Django REST framework to be feature-complete. We focus on pull requests that track the continued development of Django versions, and generally do not accept new features or code formatting changes.
12
13 ## Community
14
15 The most important thing you can do to help push the REST framework project forward is to be actively involved wherever possible. Code contributions are often overvalued as being the primary way to get involved in a project, we don't believe that needs to be the case.
16
17 If you use REST framework, we'd love you to be vocal about your experiences with it - you might consider writing a blog post about using REST framework, or publishing a tutorial about building a project with a particular JavaScript framework. Experiences from beginners can be particularly helpful because you'll be in the best position to assess which bits of REST framework are more difficult to understand and work with.
18
19 Other really great ways you can help move the community forward include helping to answer questions on the [discussion group][google-group], or setting up an [email alert on StackOverflow][so-filter] so that you get notified of any new questions with the `django-rest-framework` tag.
20
21 When answering questions make sure to help future contributors find their way around by hyperlinking wherever possible to related threads and tickets, and include backlinks from those items if relevant.
22
23 ## Code of conduct
24
25 Please keep the tone polite & professional. For some users a discussion on the REST framework mailing list or ticket tracker may be their first engagement with the open source community. First impressions count, so let's try to make everyone feel welcome.
26
27 Be mindful in the language you choose. As an example, in an environment that is heavily male-dominated, posts that start 'Hey guys,' can come across as unintentionally exclusive. It's just as easy, and more inclusive to use gender neutral language in those situations.
28
29 The [Django code of conduct][code-of-conduct] gives a fuller set of guidelines for participating in community forums.
30
31 ## Issues
32
33 Our contribution process is that the [GitHub discussions page](https://github.com/encode/django-rest-framework/discussions) should generally be your starting point. Some tips on good potential issue reporting:
34
35 * Django REST framework is considered feature-complete. Please do not file requests to change behavior, unless it is required for security reasons or to maintain compatibility with upcoming Django or Python versions.
36 * Search the GitHub project page for related items, and make sure you're running the latest version of REST framework before reporting an issue.
37 * Feature requests will typically be closed with a recommendation that they be implemented outside the core REST framework library (e.g. as third-party libraries). This approach allows us to keep down the maintenance overhead of REST framework, so that the focus can be on continued stability and great documentation.
38
39 ### Triaging issues
40
41 Getting involved in triaging incoming issues is a good way to start contributing. Every single ticket that comes into the ticket tracker needs to be reviewed in order to determine what the next steps should be. Anyone can help out with this, you just need to be willing to
42
43 * Read through the ticket - does it make sense, is it missing any context that would help explain it better?
44 * Is the ticket reported in the correct place, would it be better suited as a discussion on the discussion group?
45 * If the ticket is a bug report, can you reproduce it? Are you able to write a failing test case that demonstrates the issue and that can be submitted as a pull request?
46 * If the ticket is a feature request, could the feature request instead be implemented as a third party package?
47 * If a ticket hasn't had much activity and addresses something you need, then comment on the ticket and try to find out what's needed to get it moving again.
48
49 ## Development
50
51 To start developing on Django REST framework, first create a Fork from the
52 [Django REST Framework repo][repo] on GitHub.
53
54 Then clone your fork. The clone command will look like this, with your GitHub
55 username instead of YOUR-USERNAME:
56
57 git clone https://github.com/YOUR-USERNAME/django-rest-framework
58
59 See GitHub's [_Fork a Repo_][how-to-fork] Guide for more help.
60
61 Changes should broadly follow the [PEP 8][pep-8] style conventions, and we recommend you set up your editor to automatically indicate non-conforming styles.
62 You can check your contributions against these conventions each time you commit using the [pre-commit](https://pre-commit.com/) hooks, which we also run on CI.
63 To set them up, first ensure you have the pre-commit tool installed, for example:
64
65 python -m pip install pre-commit
66
67 Then run:
68
69 pre-commit install
70
71 ### Testing
72
73 To run the tests, clone the repository, and then:
74
75 # Setup the virtual environment
76 python3 -m venv env
77 source env/bin/activate
78 pip install -e . --group dev
79
80 # Run the tests
81 ./runtests.py
82
83 !!! tip
84 If your tests require access to the database, do not forget to inherit from `django.test.TestCase` or use the `@pytest.mark.django_db()` decorator.
85
86 For example, with TestCase:
87
88 from django.test import TestCase
89
90 class MyDatabaseTest(TestCase):
91 def test_something(self):
92 # Your test code here
93 pass
94
95 Or with decorator:
96
97 import pytest
98
99 @pytest.mark.django_db()
100 class MyDatabaseTest:
101 def test_something(self):
102 # Your test code here
103 pass
104
105 You can reuse existing models defined in `tests/models.py` for your tests.
106
107 #### Test options
108
109 Run using a more concise output style.
110
111 ./runtests.py -q
112
113
114 If you do not want the output to be captured (for example, to see print statements directly), you can use the `-s` flag.
115
116 ./runtests.py -s
117
118
119 Run the tests for a given test case.
120
121 ./runtests.py MyTestCase
122
123 Run the tests for a given test method.
124
125 ./runtests.py MyTestCase.test_this_method
126
127 Shorter form to run the tests for a given test method.
128
129 ./runtests.py test_this_method
130
131
132 !!! note
133 The test case and test method matching is fuzzy and will sometimes run other tests that contain a partial string match to the given command line input.
134
135 #### Running against multiple environments
136
137 You can also use the excellent [tox][tox] testing tool to run the tests against all supported versions of Python and Django. Install `tox` globally, and then simply run:
138
139 tox
140
141 ### Pull requests
142
143 It's a good idea to make pull requests early on. A pull request represents the start of a discussion, and doesn't necessarily need to be the final, finished submission.
144
145 It's also always best to make a new branch before starting work on a pull request. This means that you'll be able to later switch back to working on another separate issue without interfering with an ongoing pull requests.
146
147 It's also useful to remember that if you have an outstanding pull request then pushing new commits to your GitHub repo will also automatically update the pull requests.
148
149 GitHub's documentation for working on pull requests is [available here][pull-requests].
150
151 Always run the tests before submitting pull requests, and ideally run `tox` in order to check that your modifications are compatible on all supported versions of Python and Django.
152
153 Once you've made a pull request take a look at the build status in the GitHub interface and make sure the tests are running as you'd expect.
154
155 ![Build status][build-status]
156
157 *Above: build notifications*
158
159 ### Managing compatibility issues
160
161 Sometimes, in order to ensure your code works on various different versions of Django, Python or third party libraries, you'll need to run slightly different code depending on the environment. Any code that branches in this way should be isolated into the `compat.py` module, and should provide a single common interface that the rest of the codebase can use.
162
163 ### Deprecating a feature
164
165 REST framework follows a formal [deprecation policy][deprecation-policy]: a feature deprecated during the development of `3.X` keeps working until `3.X+2` removes it. In practice this means that a pull request that deprecates something should not change any behavior, only warn about the change to come.
166
167 The warning classes live in `rest_framework/deprecation.py`, and are named after the release that removes the feature. For example, during the development of 3.0 you'll find:
168
169 * `RemovedInDRF31Warning`, a `DeprecationWarning`, aliased as `RemovedInNextDRFVersionWarning`. Features flagged with it were deprecated one cycle ago, and are removed in the next release.
170 * `RemovedInDRF32Warning`, a `PendingDeprecationWarning`, aliased as `RemovedAfterNextDRFVersionWarning`. This is the class **new deprecations** should use.
171
172 To deprecate a feature:
173
174 1. Keep the existing behavior working, and raise the pending deprecation warning from the deprecated code path. Refer to the warning class by its concrete name rather than the alias, so that it's easy to grep for everything that has to go when the removal comes around.
175
176 ```python
177 import warnings
178
179 from rest_framework.deprecation import RemovedInDRF32Warning
180
181 warnings.warn(
182 "The `foo` argument is deprecated and will be removed in DRF 3.2. "
183 "Use `bar` instead.",
184 RemovedInDRF32Warning,
185 stacklevel=2,
186 )
187 ```
188
189 The message should say what is deprecated, which release removes it, and what to use instead. Pick a `stacklevel` that points the warning at the user's code rather than at REST framework internals.
190
191 2. If the deprecation can be worked around ahead of time, consider adding a setting that opts into the new behavior early, and mention it in the warning message.
192
193 3. Add a test asserting that the warning is raised, and update the existing tests of the old behavior to expect it:
194
195 ```python
196 with pytest.warns(RemovedInDRF32Warning, match="Use `bar` instead"):
197 ...
198 ```
199
200 4. Update the documentation for the feature, flagging the deprecation with an admonition:
201
202 ```
203 !!! warning
204 The `foo` argument is deprecated and will be removed in DRF 3.2. Use `bar` instead.
205 ```
206
207 Removing a deprecated feature is the mirror image, and happens as part of the release process. When 3.1 is released, everything raising a `RemovedInDRF31Warning` is deleted, along with the class itself and the documentation of the deprecated behavior, `RemovedInDRF32Warning` is escalated to subclass `DeprecationWarning`, a fresh `RemovedInDRF33Warning` is added for the next cycle's deprecations, and the two aliases are moved on to point at them.
208
209 ## Documentation
210
211 The documentation for REST framework is built from the [Markdown][markdown] source files in [the docs directory][docs].
212
213 There are many great Markdown editors that make working with the documentation really easy. The [Mou editor for Mac][mou] is one such editor that comes highly recommended.
214
215 ### Building the documentation
216
217 To build the documentation, install MkDocs with `pip install mkdocs` and then run the following command.
218
219 mkdocs build
220
221 This will build the documentation into the `site` directory.
222
223 You can build the documentation and open a preview in a browser window by using the `serve` command.
224
225 mkdocs serve
226
227 ### Language style
228
229 Documentation should be in American English. The tone of the documentation is very important - try to stick to a simple, plain, objective and well-balanced style where possible.
230
231 Some other tips:
232
233 * Keep paragraphs reasonably short.
234 * Don't use abbreviations such as 'e.g.' but instead use the long form, such as 'For example'.
235
236 ### Markdown style
237
238 There are a couple of conventions you should follow when working on the documentation.
239
240 #### 1. Headers
241
242 Headers should use the hash style. For example:
243
244 ### Some important topic
245
246 The underline style should not be used. **Don't do this:**
247
248 Some important topic
249 ====================
250
251 #### 2. Links
252
253 Links should always use the reference style, with the referenced hyperlinks kept at the end of the document.
254
255 Here is a link to [some other thing][other-thing].
256
257 More text...
258
259 [other-thing]: http://example.com/other/thing
260
261 This style helps keep the documentation source consistent and readable.
262
263 If you are hyperlinking to another REST framework document, you should use a relative link, and link to the `.md` suffix. For example:
264
265 [authentication]: ../api-guide/authentication.md
266
267 Linking in this style means you'll be able to click the hyperlink in your Markdown editor to open the referenced document. When the documentation is built, these links will be converted into regular links to HTML pages.
268
269 #### 3. Notes
270
271 If you want to draw attention to a note or warning, use an [admonition], like so:
272
273 !!! note
274 A useful documentation note.
275
276 The documentation theme styles `info`, `warning`, `tip` and `danger` admonition types, but more could be added if the need arise.
277
278
279 [cite]: https://www.w3.org/People/Berners-Lee/FAQ.html
280 [code-of-conduct]: https://www.djangoproject.com/conduct/
281 [google-group]: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
282 [so-filter]: https://stackexchange.com/filters/66475/rest-framework
283 [pep-8]: https://www.python.org/dev/peps/pep-0008/
284 [build-status]: ../img/build-status.png
285 [pull-requests]: https://help.github.com/articles/using-pull-requests
286 [tox]: https://tox.readthedocs.io/en/latest/
287 [markdown]: https://daringfireball.net/projects/markdown/basics
288 [docs]: https://github.com/encode/django-rest-framework/tree/main/docs
289 [mou]: http://mouapp.com/
290 [repo]: https://github.com/encode/django-rest-framework
291 [how-to-fork]: https://help.github.com/articles/fork-a-repo/
292 [admonition]: https://python-markdown.github.io/extensions/admonition/
293 [deprecation-policy]: release-notes.md#deprecation-policy