
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
pytest-mypy-testing
provides a
pytest plugin to test that
mypy produces a given output. As mypy can be
told to display the type of an
expression
this allows us to check mypys type interference.
python -m pip install pytest-mypy-testing
The Python distribution package contains an entry
point
so that the plugin is automatically discovered by pytest. To disable
the plugin when it is installed , you can use the pytest command line
option -p no:mypy-testing
.
A mypy test case is a top-level functions decorated with
@pytest.mark.mypy_testing
in a file named *.mypy-testing
or in a
pytest test module. pytest-mypy-testing
follows the pytest logic in
identifying test modules and respects the
python_files
config value.
Note that pytest-mypy-testing
uses the Python
ast module to parse
candidate files and does not import any file, i.e., the decorator must
be exactly named @pytest.mark.mypy_testing
.
In a pytest test module file you may combine both regular pytest test functions and mypy test functions. A single function can be both.
Example: A simple mypy test case could look like this:
@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
foo = "abc"
foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
The plugin runs mypy for every file containing at least one mypy test case. The mypy output is then compared to special Python comments in the file:
# N: <msg>
- we expect a mypy note message# W: <msg>
- we expect a mypy warning message# E: <msg>
- we expect a mypy error message# F: <msg>
- we expect a mypy fatal error message# R: <msg>
- we expect a mypy note message Revealed type is '<msg>'
. This is useful to easily check reveal_type
output:
@pytest.mark.mypy_testing
def mypy_use_reveal_type():
reveal_type(123) # N: Revealed type is 'Literal[123]?'
reveal_type(456) # R: Literal[456]?
The algorithm matching messages parses mypy error code both in the output generated by mypy and in the Python comments.
If both the mypy output and the Python comment contain an error code
and a full message, then the messages and the error codes must
match. The following test case expects that mypy writes out an
assignment
error code and a specific error message:
@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
foo = "abc"
foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
If the Python comment does not contain an error code, then the error code written out by mypy (if any) is ignored. The following test case expects a specific error message from mypy, but ignores the error code produced by mypy:
@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
foo = "abc"
foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
If the Python comment specifies only an error code, then the message
written out by mypy is ignored, i.e., the following test case checks
that mypy reports an assignment
error:
@pytest.mark.mypy_testing
def mypy_test_invalid_assignment() -> None:
foo = "abc"
foo = 123 # E: [assignment]
Mypy test case functions can be decorated with @pytest.mark.skip
and
@pytest.mark.xfail
to mark them as to-be-skipped and as
expected-to-fail, respectively. As with the
@pytest.mark.mypy_testing
mark, the names must match exactly as the
decorators are extracted from the ast.
python -m pip install -U -r requirements.txt
.pytest_collect_file()
with usage of the file_path argument
introduced in pytest 7 (#51, #52)PYTEST_VERSION_INFO
- by @blueyed (#8)--check-untyped-defs
to mypy (#11)python_files
when identifying pytest test modules (#12)inv tox -e py-*
) (#6)LICENSES
directory
(#6)FAQs
Pytest plugin to check mypy output.
We found that pytest-mypy-testing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.