- The most valuable thing you have to contribute to the Tornado project is your experience using Tornado to build applications. A clear bug report is very helpful even when it is not accompanied by a fix.
- Whether written by humans or AI, all pull requests are subject to a rigorous code review process. Code review time is the most limited resource, and it's usually more efficient for a maintainer to implement a fix using their own agent than to review a change from an outside contributor.
- Please open an issue before a pull request (except for trivial changes like documentation typos). In general, wait for a comment from a maintainer inviting you to submit a PR before starting to work on the code.
- Pull requests are welcome from contributors who have particular expertise in the change being made, or who have an application or environment for testing their changes that a maintainer may not be able to access.
Pull requests that do not follow these guidelines may be closed without detailed review. This is not a judgment of your work, it is necessary to keep the review workload manageable. The closing comment on the issue will generally link to the corresponding issue if there wasn't one already.
To report a security vulnerability, do not open an issue or pull request; follow the instructions in SECURITY.md instead.
The rest of this document describes how to work on the code, whether for your own use or for a PR.
- Tornado is a mature project and backwards compatibility is always important. Consider backwards compatibility when making or proposing any change.
- We use
blackas code formatter and recommend configuring your editor to run this automatically (using the version specified inrequirements.txt). Commits that are not properly formatted byblackwill be rejected in CI. - We update
requirements.txtinfrequently. It is important to install the versions of tools likeblackandmypyfromrequirements.txtinstead of running their latest versions (don't just useuvx). Older release branches likebranch6.5have their ownrequirements.txtso reinstall these tools (or switch virtualenvs) when switching branches. - The
types-pycurlpackage should be installed when runningmypy. - Run
flake8from the project root directory so it can see.flake8, this prevents spurious warnings. - Before committing, it is recommended to run
tox -e lint,docs,py3. This will verify that the code is formatted correctly, type checking withmypypasses, thesphinxbuild for docs has no errors, and the main test suite passes with the current version of python. - Nearly all code changes should have new or updated tests covering the changed behavior. Red/green TDD is encouraged.
- We use
toxas a test runner to run tests in various configurations with the correct dependencies.tox -e py3runs most of the tests, whiletox -e py3-fullcan be used to run a more extensive version of the test suite which as extra dependencies. The-fullconfigurations are necessary when working on certain modules, includingcurl_httpclient.py,twisted.py, orpycares.py. - The fastest way to run the tests is to bypass
toxand runpython3 -m tornado.test. To run a subset of tests, add a module, class, or method name to the command line:python3 -m tornado.test.httputil_test. tox -e coveragemeasures test coverage across the-fullconfigurations and writes an HTML report to.tox/coverage-data/html. CI fails if total coverage drops below the floor set intox.ini; raise the floor when you raise coverage.tornado/test/property_test.pyholds property-based tests usinghypothesis, which is installed in the-fullconfigurations (the tests are skipped without it). They are a good place for invariants of parsers and encoders: round-trips, agreement with a simple model, and "malformed input raisesHTTPInputErrorand nothing else". When one fails, also add the failing input as an example-based test so it stays covered.tornado/test/api_test.pycompares the public API (names and signatures) to the snapshot intornado/test/api.txt. When you change the API on purpose, runpython -m tornado.test.api_test --updatefrom the repository root (with the-fulldependencies installed) and commit the updated snapshot. Imports are left out of the snapshot unless they use theimport X as Xform that marks a re-export.tox -e docschecks that everything in the docs is in the snapshot.- Code that parses untrusted input should have resource-budget tests.
assert_linear_scalingintornado/test/util.pychecks that time grows linearly with the input, andassert_memory_budgetchecks peak memory per byte of input (or per byte of a configured limit such asmax_body_size). Look for the worst-case input (many tiny fields, deep escaping, highly compressible data) and keep sizes small enough that the test takes a fraction of a second. A fix for a denial-of-service bug should come with such a test, and the test should fail on the code before the fix. - The "Compare benchmarks" CI job runs
maint/benchmark/ci_benchmark.pyagainst the PR and its base branch and fails if anything is more than 20% slower, or if an adversarial input exceeds its absolute budget (BUDGETSin that file). To run the comparison locally, check out the base in another directory and runpython maint/benchmark/compare_benchmarks.py --base ../base --head .. When you add a parser or a resource-budget test for one, add a benchmark of its worst-case input too. - Don't remove a public name, argument or behavior without deprecating it first: it should
emit a
DeprecationWarningand have a.. deprecated::note (saying what to use instead) for at least one minor release before it goes. Seemaint/RELEASING.md. tox -e lintruns flake8, black and mypy. mypy runs in strict mode, relaxed for the tests (seesetup.cfg), and reports# type: ignorecomments that are no longer needed. Names that a module re-exports need theimport X as Xform. It also type-checks the client code inmaint/test/mypyagainst the installed package: add togood.pywhen you change annotations that applications rely on.- Tests can also be run with the standard library's
unittestpackage CLI. This is useful for integration with some editors. However, this mode skips a few checks, notably the requirement that tests do not generate any log output without corresponding ExpectLog assertions. - Tornado does not use
pytest. Some effort has been made to make the tests work with thepytestrunner, but this is not maintained. - Tests must run on Linux, macOS, and Windows (unless they are testing platform-dependent functionality, in which case they should be skipped on other platforms).
- Use mocks sparingly. Prefer to use real sockets, servers, etc as much as possible. It is rarely appropriate to use mocks just to speed up a test. The most common reason to use mocks in Tornado tests is to simulate error conditions that cannot be reproduced without mocks.
- Most tests are covered by a default 5 second timeout. This can be changed during
development with the env var
ASYNC_TEST_TIMEOUT, or with thetimeoutargument to@gen_test(although most tests should be made fast instead of given an increased timeout)
We use Sphinx with the autodoc extension to build our docs. To build the docs run
tox -e docs and find the output in ./.tox/docs/tmp/html/index.html
- Older versions of pycurl bundled specific versions of libcurl. This can be an easy way
to test with old versions of libcurl. Specifically,
pycurl==7.45.4installs libcurl 8.11.1, which is not the oldest version of libcurl we support but is the oldest one that is easy to install. - Easy handles are pooled and reused (_free_list, reset in _finish), so per-request state leaking into the following request is a live bug class: test_reuse_proxy_credentials and test_reuse_certs both exist because of it. New per-request state deserves a reuse test with max_clients=1.
- That state is smuggled in a dict attached to the handle (curl.info, rebuilt in _process_queue). There is a standing TODO about the approach; until it is addressed, this is where such state goes.
- Run
tox -e lint,docs,py3locally and make sure it passes - If you modify
curl_httpclient.py, also runtox -e py3-full - Review your changes for any backwards incompatibilities that may affect downstream applications using Tornado