11## Regression tests for typeshed
22
33Regression tests for the standard library stubs can be found in the
4- ` stdlib/@tests/test_cases ` directory. Stubs for third-party libraries that do
5- have test cases can be found in ` @tests/test_cases ` subdirectories for each
6- stubs package. For example, the test cases for ` requests ` can be found in the
7- ` stubs/requests/@tests/test_cases ` directory.
4+ ` stdlib/@tests/test_cases ` directory. Not all third-party-library stub
5+ packages in typeshed have test cases, and not all of them need test cases --
6+ but for those that do, their test cases can be found in ` @tests/test_cases `
7+ subdirectories for each stubs package. For example, the test cases for
8+ ` requests ` can be found in the ` stubs/requests/@tests/test_cases ` directory.
89
910** Regression test cases should only be written for functions and classes which
1011are known to have caused problems in the past, where the stubs are difficult to
@@ -14,34 +15,45 @@ multiple other mechanisms for spotting errors in the stubs.
1415
1516### The purpose of these tests
1617
17- Different test cases in this directory serve different purposes. For some stubs in
18- typeshed , the type annotations are complex enough that it's useful to have
18+ Different test cases in typeshed serve different purposes. For some typeshed
19+ stubs , the type annotations are complex enough that it's useful to have
1920sanity checks that test whether a type checker understands the intent of
2021the annotations correctly. Examples of tests like these are
21- ` builtins/check_pow.py ` and ` asyncio/check_gather.py ` .
22+ ` stdlib/@tests/test_cases/builtins/check_pow.py ` and
23+ ` stdlib/@tests/test_cases/asyncio/check_gather.py ` .
2224
23- Other test cases, such as the samples for ` ExitStack ` in ` check_contextlib.py `
24- and the samples for ` LogRecord ` in ` check_logging.py ` , do not relate to
25+ Other test cases, such as the samples for ` ExitStack ` in
26+ ` stdlib/@tests/test_cases/check_contextlib.py ` and the samples for ` LogRecord `
27+ in ` stdlib/@tests/test_cases/check_logging.py ` , do not relate to
2528stubs where the annotations are particularly complex, but they * do* relate to
2629stubs where decisions have been taken that might be slightly unusual. These
2730test cases serve a different purpose: to check that type checkers do not emit
2831false-positive errors for idiomatic usage of these classes.
2932
3033## Running the tests
3134
32- To verify the test cases in this directory pass with mypy, run ` python tests/regr_test.py stdlib `
33- from the root of the typeshed repository. This assumes that the development
34- environment has been set up as described in the [ CONTRIBUTING.md] ( ../CONTRIBUTING.md )
35- document.
35+ To verify the stdlib test cases pass with mypy, run
36+ ` python tests/regr_test.py stdlib ` from the root of the typeshed repository.
37+ This assumes that the development environment has been set up as described in
38+ the [ CONTRIBUTING.md] ( ../CONTRIBUTING.md ) document.
39+
40+ For third-party-library stubs, pass the name of the runtime library the stubs
41+ are for. For example, to run the tests for our ` requests ` stubs, run
42+ ` python tests/regr_test.py requests ` .
43+
44+ Run ` python tests/regr_test.py -h ` for the full range of CLI options this script
45+ supports. There is no equivalent script for pyright currently; for pyright, the
46+ tests are checked in CI using a GitHub Action.
3647
3748### How the tests work
3849
39- The code in this directory is not intended to be directly executed. Instead,
40- type checkers are run on the code, to check that typing errors are
41- emitted at the correct places .
50+ The code in typeshed's test cases is not intended to be directly executed.
51+ Instead, type checkers are run on the code, to check that type checker
52+ diagnostics are emitted at the correct locations .
4253
43- Some files in this directory simply contain samples of idiomatic Python, which
44- should not (if the stubs are correct) cause a type checker to emit any errors.
54+ Some files in these directories simply contain samples of idiomatic Python,
55+ which should not (if the stubs are correct) cause a type checker to emit any
56+ diagnostics.
4557
4658Many test cases also make use of
4759[ ` assert_type ` ] ( https://docs.python.org/3.11/library/typing.html#typing.assert_type ) ,
5466setting and pyright's
5567[ ` reportUnnecessaryTypeIgnoreComment ` ] ( https://github.com/microsoft/pyright/blob/main/docs/configuration.md#type-check-diagnostics-settings )
5668setting) to test instances where a type checker * should* emit some kind of
57- error, if the stubs are correct. Both settings are enabled by default for the entire
58- subdirectory .
69+ error, if the stubs are correct. Both settings are enabled by default for
70+ all ` @tests/test_cases/ ` subdirectories in typeshed .
5971
6072For more information on using ` assert_type ` and
6173` --warn-unused-ignores ` /` reportUnnecessaryTypeIgnoreComment ` to test type
@@ -65,21 +77,21 @@ provides a useful guide.
6577
6678### Naming convention
6779
68- Use the same top-level name for the module / package you would like to test.
80+ Use the same top-level name for the module or package you would like to test.
6981Use the ` check_${thing}.py ` naming pattern for individual test files.
7082
7183By default, test cases go into a file with the same name as the stub file, prefixed with ` check_ ` .
72- For example: ` check_contextlib.py ` .
84+ For example: ` stdlib/@tests/test_cases/ check_contextlib.py` .
7385
7486If that file becomes too big, we instead create a directory with files named after individual objects being tested.
75- For example: ` builtins/check_dict.py ` .
87+ For example: ` stdlib/@tests/test_cases/ builtins/check_dict.py` .
7688
7789### Differences to the rest of typeshed
7890
79- Unlike the rest of typeshed, this directory largely contains ` .py ` files. This
80- is because the purpose of this folder is to test the implications of typeshed
81- changes for end users, who will mainly be using ` .py ` files rather than ` .pyi `
82- files.
91+ Unlike the rest of typeshed, the ` @tests/test_cases ` directories largely
92+ contain ` .py ` files. This is because the purpose of these directories is to
93+ test the implications of typeshed changes for end users, who will mainly be
94+ using ` .py ` files rather than ` .pyi ` files.
8395
8496Another difference to the rest of typeshed
8597(which stems from the fact that the test-case files are all ` .py ` files
@@ -109,7 +121,8 @@ with a specific Python version passed on the command line to the `tests/regr_tes
109121To mark a test-case file as being skippable on lower versions of Python,
110122append ` -py3* ` to the filename.
111123For example, if ` foo ` is a stdlib feature that's new in Python 3.11,
112- test cases for ` foo ` should be put in a file named ` check_foo-py311.py ` .
124+ test cases for ` foo ` should be put in a file named
125+ ` stdlib/@tests/test_cases/check_foo-py311.py ` .
113126This means that mypy will only run the test case
114127if ` --python-version 3.11 ` , ` --python-version 3.12 ` , etc.
115128is passed on the command line to ` tests/regr_test.py ` ,
@@ -118,4 +131,4 @@ is passed on the command line.
118131
119132However, ` if sys.version_info >= (3, target): ` is still required for ` pyright `
120133in the test file itself.
121- Example: [ ` check_exception_group-py311.py ` ] ( ../stdlib/@tests/test_cases/builtins/check_exception_group-py311.py )
134+ Example: [ ` stdlib/@tests/test_cases/ check_exception_group-py311.py` ] ( ../stdlib/@tests/test_cases/builtins/check_exception_group-py311.py )
0 commit comments