pax_global_header00006660000000000000000000000064145714525310014521gustar00rootroot0000000000000052 comment=c6b339f9cd549e50ac60ec8bc7befd1bb2b4567b pytest-mypy-testing-0.1.3/000077500000000000000000000000001457145253100155215ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/.bumpversion.cfg000066400000000000000000000002101457145253100206220ustar00rootroot00000000000000[bumpversion] commit = True tag = True sign_tags = True current_version = 0.1.3 [bumpversion:file:src/pytest_mypy_testing/__init__.py] pytest-mypy-testing-0.1.3/.bumpversion.cfg.license000066400000000000000000000001151457145253100222470ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 pytest-mypy-testing-0.1.3/.editorconfig000066400000000000000000000022251457145253100201770ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 # # See https://EditorConfig.org # # PyCharm support: # - https://www.jetbrains.com/help/pycharm/configuring-code-style.html#editorconfig # # Visual Studio Code: # - https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig # - https://github.com/editorconfig/editorconfig-vscode # # Emacs: # - https://github.com/editorconfig/editorconfig-emacs#readme # top-most EditorConfig file root = true [*.{cfg,ini,py,proto,sh}] charset = utf-8 indent_style = space indent_size = 4 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [{.editorconfig,MANIFEST.in,Pipfile}] charset = utf-8 indent_style = space indent_size = 4 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true # No indent_size [*.{rst,txt,yml}] charset = utf-8 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true # Do not trim trailing whitespace [*.md] charset = utf-8 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = false # CRLF line endings [*.bat] end_of_line = crlf pytest-mypy-testing-0.1.3/.flake8000066400000000000000000000012551457145253100166770ustar00rootroot00000000000000# -*- mode: conf; -*- # SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [flake8] exclude = *.egg-info .eggs .build .git .tox .venv build dist docs downloads generated venv src/prettypb/protobuf/*.py runtime/src/prettypb/protobuf/*.py ignore = # F811: redefinition of unused '...' from line ... F811 # W503: line break before binary operator W503 # E203: whitespace before ':' E203 # E231: missing whitespace after ',' E231 # E501:line too long E501 # E731: do not assign a lambda expression, use a def E731 builtins = reveal_type max-line-length = 88 pytest-mypy-testing-0.1.3/.gitattributes000066400000000000000000000014431457145253100204160ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 # Git attributes # # See # - git help attributes # - https://git-scm.com/docs/gitattributes # Set the default behavior, in case people don't have core.autocrlf set. * text=auto # Pure text files: Use LF in repo and checkout *.c text eol=lf *.cfg text eol=lf *.in text eol=lf *.ini text eol=lf *.md text eol=lf *.proto text eol=lf *.puml text eol=lf *.py text eol=lf *.rst text eol=lf *.sh text eol=lf *.txt text eol=lf *.typed text eol=lf *.yml text eol=lf .editorconfig text eol=lf .gitattributes text eol=lf .gitignore text eol=lf .pylintrc text eol=lf Jenkinsfile* text eol=lf Pipfile* text eol=lf pylintrc text eol=lf # Binary/DOS files: Don't treat as text *.bat -text *.desc -text *.exe -text *.inv -text *.png -text pytest-mypy-testing-0.1.3/.github/000077500000000000000000000000001457145253100170615ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/.github/workflows/000077500000000000000000000000001457145253100211165ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/.github/workflows/pythonpackage.yml000066400000000000000000000043151457145253100245010ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 name: Python package on: [push] jobs: test-py37: runs-on: ${{ matrix.os }} strategy: max-parallel: 4 matrix: os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: | 3.7 3.11 - name: Install dependencies run: | python -m pip install -c constraints.txt pip python -m pip install -c constraints.txt invoke tox - name: Run tox env: TOX_PARALLEL_NO_SPINNER: "1" run: | inv mkdir build/coverage-data inv tox -e "py37-*" test-py38-py39-py310-py311-py312: runs-on: ${{ matrix.os }} strategy: max-parallel: 8 matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install -c constraints.txt pip python -m pip install -c constraints.txt invoke tox - name: Run tox env: TOX_PARALLEL_NO_SPINNER: "1" run: | inv mkdir build/coverage-data inv tox -e "py-*" lint: runs-on: ubuntu-latest strategy: max-parallel: 4 matrix: python-version: ["3.11"] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install -c constraints.txt black flake8 flake8-isort mypy pytest pip list - name: Run black run: | black --check --diff . - name: Run mypy run: | mypy src tests - name: Run isort run: | isort . --check --diff - name: Run flake8 run: | flake8 --help flake8 -v pytest-mypy-testing-0.1.3/.gitignore000066400000000000000000000007571457145253100175220ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 !.bumpversion.cfg !.bumpversion.cfg.license !.editorconfig !.flake8 !.gitattributes !.gitignore !.isort.cfg !/.github *.bak *.egg-info *.pyc *~ .*_cache .coverage .coverage.* .dir-locals.el .eggs .envrc .hypothesis .idea .mypy* .python-version .secrets .tox .vs .vscode /.build* /.venv /build* /dist /install* /old-* /pip-wheel-metadata /protox /public /target Pipfile.lock __pycache__ _version.py pip-wheel-metadata pytest-mypy-testing-0.1.3/.isort.cfg000066400000000000000000000007271457145253100174260ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [isort] # See https://github.com/timothycrosley/isort/wiki/isort-Settings line_length = 88 combine_as_imports = true combine_star = true multi_line_output = 3 include_trailing_comma = true order_by_type = true skip = .eggs,.git,.tox,build,dist,docs skip_gitignore = true lines_after_imports = 2 known_first_party = pytest_mypy_testing sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER pytest-mypy-testing-0.1.3/LICENSES/000077500000000000000000000000001457145253100167265ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/LICENSES/Apache-2.0.txt000066400000000000000000000241471457145253100211550ustar00rootroot00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. pytest-mypy-testing-0.1.3/LICENSES/CC0-1.0.txt000066400000000000000000000154041457145253100203340ustar00rootroot00000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. pytest-mypy-testing-0.1.3/LICENSES/MIT.txt000066400000000000000000000021241457145253100201170ustar00rootroot00000000000000MIT License Copyright (c) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pytest-mypy-testing-0.1.3/README.md000066400000000000000000000225361457145253100170100ustar00rootroot00000000000000 [![PyPI](https://img.shields.io/pypi/v/pytest-mypy-testing.svg)](https://pypi.python.org/pypi/pytest-mypy-testing) [![GitHub Action Status](https://github.com/davidfritzsche/pytest-mypy-testing/workflows/Python%20package/badge.svg)](https://github.com/davidfritzsche/pytest-mypy-testing/actions) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) # pytest-mypy-testing — Plugin to test mypy output with pytest `pytest-mypy-testing` provides a [pytest](https://pytest.readthedocs.io/en/latest/) plugin to test that [mypy](http://mypy-lang.org/) produces a given output. As mypy can be told to [display the type of an expression](https://mypy.readthedocs.io/en/latest/common_issues.html#displaying-the-type-of-an-expression) this allows us to check mypys type interference. # Installation ``` shell python -m pip install pytest-mypy-testing ``` The Python distribution package contains an [entry point](https://docs.pytest.org/en/latest/writing_plugins.html#making-your-plugin-installable-by-others) 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`. # Writing Mypy Output Test Cases 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`](https://docs.pytest.org/en/latest/reference.html#confval-python_files) config value. Note that ``pytest-mypy-testing`` uses the Python [ast](https://docs.python.org/3/library/ast.html) 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: ``` python @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: ` - we expect a mypy note message * `# W: ` - we expect a mypy warning message * `# E: ` - we expect a mypy error message * `# F: ` - we expect a mypy fatal error message * `# R: ` - we expect a mypy note message `Revealed type is ''`. This is useful to easily check `reveal_type` output: ```python @pytest.mark.mypy_testing def mypy_use_reveal_type(): reveal_type(123) # N: Revealed type is 'Literal[123]?' reveal_type(456) # R: Literal[456]? ``` ## mypy Error Code Matching 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: ``` python @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: ``` python @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: ``` python @pytest.mark.mypy_testing def mypy_test_invalid_assignment() -> None: foo = "abc" foo = 123 # E: [assignment] ``` ## Skipping and Expected Failures 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. # Development * Create and activate a Python virtual environment. * Install development dependencies by calling `python -m pip install -U -r requirements.txt`. * Start developing. * To run all tests with [tox](https://tox.readthedocs.io/en/latest/), Python 3.7, 3.8, 3.9, 3.10, 3.11 and 3.12 must be available. You might want to look into using [pyenv](https://github.com/pyenv/pyenv). # Changelog ## v0.1.3 (2024-03-05) * Replace usage of deprecated path argument to pytest hook ``pytest_collect_file()`` with usage of the file_path argument introduced in pytest 7 ([#51][i51], [#52][p52]) ## v0.1.2 (2024-02-26) * Add support for pytest 8 (no actual change, but declare support) ([#46][i46], [#47][p47]) * Declare support for Python 3.12 ([#50][p50]) * Update GitHub actions ([#48][p48]) * Update development dependencies ([#49][p49]) * In GitHub PRs run tests with Python 3.11 and 3.12 ([#50][p50]) ## v0.1.1 * Compare just mypy error codes if given and no error message is given in the test case Python comment ([#36][i36], [#43][p43]) ## v0.1.0 * Implement support for flexible matching of mypy error codes (towards [#36][i36], [#41][p41]) * Add support for pytest 7.2.x ([#42][p42]) * Add support for mypy 1.0.x ([#42][p42]) * Add support for Python 3.11 ([#42][p42]) * Drop support for pytest 6.x ([#42][p42]) * Drop support for mypy versions less than 0.931 ([#42][p42]) ## v0.0.12 * Allow Windows drives in filename ([#17][i17], [#34][p34]) * Support async def tests ([#30][i30], [#31][p31]) * Add support for mypy 0.971 ([#35][i35], [#27][i27]) * Remove support for Python 3.6 ([#32][p32]) * Bump development dependencies ([#40][p40]) ## v0.0.11 * Add support for mypy 0.960 ([#25][p25]) ## v0.0.10 * Add support for pytest 7.0.x and require Python >= 3.7 ([#23][p23]) * Bump dependencies ([#24][p24]) ## v0.0.9 * Disable soft error limit ([#21][p21]) ## v0.0.8 * Normalize messages to enable support for mypy 0.902 and pytest 6.2.4 ([#20][p20]) ## v0.0.7 * Fix `PYTEST_VERSION_INFO` - by [@blueyed](https://github.com/blueyed) ([#8][p8]) * Always pass `--check-untyped-defs` to mypy ([#11][p11]) * Respect pytest config `python_files` when identifying pytest test modules ([#12][p12]) ## v0.0.6 - add pytest 5.4 support * Update the plugin to work with pytest 5.4 ([#7][p7]) ## v0.0.5 - CI improvements * Make invoke tasks work (partially) on Windows ([#6][p6]) * Add an invoke task to run tox environments by selecting globs (e.g., `inv tox -e py-*`) ([#6][p6]) * Use coverage directly for code coverage to get more consistent parallel run results ([#6][p6]) * Use flit fork dflit to make packaging work with `LICENSES` directory ([#6][p6]) * Bump dependencies ([#6][p6]) [i17]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/17 [i27]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/27 [i30]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/30 [i35]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/35 [i36]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/36 [i46]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/46 [i51]: https://github.com/davidfritzsche/pytest-mypy-testing/issues/51 [p6]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/6 [p7]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/7 [p8]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/8 [p11]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/11 [p12]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/12 [p20]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/20 [p21]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/21 [p23]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/23 [p24]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/24 [p25]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/25 [p31]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/31 [p32]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/32 [p34]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/34 [p40]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/40 [p41]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/41 [p42]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/42 [p43]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/43 [p47]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/47 [p48]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/48 [p49]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/49 [p50]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/50 [p52]: https://github.com/davidfritzsche/pytest-mypy-testing/pull/52 pytest-mypy-testing-0.1.3/constraints.in000066400000000000000000000005321457145253100204200ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 atomicwrites==1.4.0 filelock <3.12.3; python_version < "3.8" importlib-metadata <6.8; python_version < "3.8" platformdirs <4.1; python_version < "3.8" pluggy <1.3; python_version < "3.8" typing-extensions <4.8; python_version < "3.8" zipp <3.16; python_version < "3.8" pytest-mypy-testing-0.1.3/constraints.txt000066400000000000000000000030431457145253100206310ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 # # This file is autogenerated by lock-requirements.sh # To update, run: # # ./lock-requirements.sh # attrs==23.2.0 binaryornot==0.4.4 black==24.2.0 boolean-py==4.0 build==1.0.3 bump2version==1.0.1 certifi==2024.2.2 chardet==5.2.0 charset-normalizer==3.3.2 click==8.1.7 coverage==7.4.3 dflit==2.3.0.1 dflit-core==2.3.0.1 distlib==0.3.8 docutils==0.20.1 exceptiongroup==1.2.0 filelock==3.13.1 flake8==7.0.0 flake8-bugbear==24.2.6 flake8-comprehensions==3.14.0 flake8-html==0.4.3 flake8-logging-format==0.9.0 flake8-mutable==1.2.0 flake8-pyi==24.1.0 fsfe-reuse==1.0.0 idna==3.6 iniconfig==2.0.0 invoke==2.2.0 isort==5.13.2 jinja2==3.1.3 license-expression==30.2.0 markupsafe==2.1.5 mccabe==0.7.0 mypy==1.8.0 mypy-extensions==1.0.0 packaging==23.2 pathspec==0.12.1 pip==24.0 pip-tools==7.4.0 platformdirs==4.2.0 pluggy==1.4.0 py==1.11.0 pycodestyle==2.11.1 pyflakes==3.2.0 pygments==2.17.2 pyproject-hooks==1.0.0 pytest==8.0.2 pytest-cov==4.1.0 pytest-html==4.1.1 pytest-metadata==3.1.1 python-debian==0.1.49 pytoml==0.1.21 requests==2.31.0 reuse==3.0.1 setuptools==69.1.1 six==1.16.0 tomli==2.0.1 tox==3.28.0 tox-pyenv==1.1.0 types-invoke==2.0.0.10 typing-extensions==4.10.0 urllib3==2.2.1 virtualenv==20.25.1 wheel==0.42.0 atomicwrites==1.4.0 filelock <3.12.3; python_version < "3.8" importlib-metadata <6.8; python_version < "3.8" platformdirs <4.1; python_version < "3.8" pluggy <1.3; python_version < "3.8" typing-extensions <4.8; python_version < "3.8" zipp <3.16; python_version < "3.8" pytest-mypy-testing-0.1.3/lock-requirements.sh000077500000000000000000000012531457145253100215320ustar00rootroot00000000000000#!/bin/sh # SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 export CUSTOM_COMPILE_COMMAND="./lock-requirements.sh" export PYTHONWARNINGS=ignore pip-compile \ --unsafe-package='' \ --no-emit-index-url \ --resolver=backtracking \ -o requirements.txt \ requirements.in \ "$@" cat >constraints.txt <>constraints.txt cat constraints.in | grep -v -E '^#' >>constraints.txt pytest-mypy-testing-0.1.3/mypy.ini000066400000000000000000000026701457145253100172250ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [mypy] python_version = 3.8 mypy_path = src verbosity = 0 # Show some context in the error message show_error_context = True # Unfortunately, outputting the column number confuses Visual Studio Code show_column_numbers = True # follow_imports = (normal|silent|skip|error) # cf. https://mypy.readthedocs.io/en/latest/running_mypy.html#follow-imports # silent = Follow all imports and type check, but suppress any error messages # in imported modules follow_imports = silent # Do not complain about missing imports ignore_missing_imports = False # Enables PEP 420 style namespace packages. (default False) namespace_packages = False # explicit_package_bases = True # Type-checks the interior of functions without type annotations (default False) check_untyped_defs = True # Warn about unused per-module sections (default False) warn_unused_configs = True # Warns about casting an expression to its inferred type (default False) warn_redundant_casts = True # Warn about unused `# type: ignore` comments (default False) warn_unused_ignores = True # Shows a warning when returning a value with type Any from a function declared # with a non-Any return type (default False) warn_return_any = True # Strict Optional checks. # If False, mypy treats None as compatible with every type. (default True) strict_optional = True [mypy-py.*] ignore_missing_imports = True pytest-mypy-testing-0.1.3/mypy_tests/000077500000000000000000000000001457145253100177415ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/mypy_tests/test_mypy_tests_in_test_file.py000066400000000000000000000010621457145253100263150ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 # flake8: noqa # ruff: noqa import pytest @pytest.mark.mypy_testing def err(): import foo # E: Cannot find implementation or library stub for module named 'foo' @pytest.mark.mypy_testing def test_invalid_assginment(): """An example test function to be both executed and mypy-tested""" foo = "abc" foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") assert foo == 123 reveal_type(123) # R: Literal[123]? pytest-mypy-testing-0.1.3/pyproject.toml000066400000000000000000000032551457145253100204420ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [build-system] requires = ["dflit_core >=2,<3"] build-backend = "flit_core.buildapi" [tool.flit.metadata] module = "pytest_mypy_testing" author = "David Fritzsche" author-email = "david.fritzsche@mvua.de" classifiers = [ "Framework :: Pytest", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "License :: OSI Approved :: MIT License", "Operating System :: MacOS", "Operating System :: Microsoft :: Windows", "Operating System :: OS Independent", "Operating System :: POSIX", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Typing :: Typed", ] description-file = "README.md" dist-name = "pytest-mypy-testing" home-page = "https://github.com/davidfritzsche/pytest-mypy-testing" license = "Apache-2.0 OR MIT" requires = [ "pytest>=7,<9", "mypy>=1.0", ] requires-python = ">=3.7" [tool.flit.entrypoints.pytest11] mypy-testing = "pytest_mypy_testing.plugin" [tool.flit.sdist] include = ["src/pytest_mypy_testing/_version.py"] [tool.black] line-length = 88 target-version = ['py37', 'py38', 'py39', 'py310', 'py311', 'py312'] include = '\.pyi?$' extend-exclude = ''' ( /_version\.py | /dist/ ) ''' [tool.coverage.run] include = [ 'src/*', 'mypy_tests/*', 'tests/*', ] data_file = 'build/coverage-data/coverage' parallel = true [tool.ruff.lint.isort] combine-as-imports = true lines-after-imports = 2 pytest-mypy-testing-0.1.3/pytest.ini000066400000000000000000000012531457145253100175530ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [pytest] testpaths = tests mypy_tests pytest_mypy_testing addopts = --durations=20 --doctest-continue-on-failure --doctest-modules --failed-first --pyargs --showlocals -p no:mypy-testing --verbose --verbose doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL ELLIPSIS log_level = DEBUG junit_family = xunit2 # By default report warnings as errors filterwarnings = error # Ignore some Python 3.12 related deprecations ignore:datetime.datetime.utc.* is deprecated ignore:ast.[A-Za-z]* is deprecated ignore:Attribute s is deprecated pytest-mypy-testing-0.1.3/requirements.in000066400000000000000000000006251457145253100205770ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 black >=24,<25 bump2version coverage[toml] dflit flake8-bugbear flake8-comprehensions flake8-html flake8-logging-format flake8-mutable flake8-pyi fsfe-reuse invoke isort mypy ~=1.8 pip pip-tools pytest pytest-cov pytest-html setuptools >=69 tox <4 tox-pyenv types-invoke # Consider constraints constraints.in -c constraints.in pytest-mypy-testing-0.1.3/requirements.txt000066400000000000000000000065321457145253100210130ustar00rootroot00000000000000# # This file is autogenerated by pip-compile with Python 3.10 # by the following command: # # ./lock-requirements.sh # attrs==23.2.0 # via flake8-bugbear binaryornot==0.4.4 # via reuse black==24.2.0 # via -r requirements.in boolean-py==4.0 # via # license-expression # reuse build==1.0.3 # via pip-tools bump2version==1.0.1 # via -r requirements.in certifi==2024.2.2 # via requests chardet==5.2.0 # via # binaryornot # python-debian charset-normalizer==3.3.2 # via requests click==8.1.7 # via # black # pip-tools coverage[toml]==7.4.3 # via # -r requirements.in # pytest-cov dflit==2.3.0.1 # via -r requirements.in dflit-core==2.3.0.1 # via dflit distlib==0.3.8 # via virtualenv docutils==0.20.1 # via dflit exceptiongroup==1.2.0 # via pytest filelock==3.13.1 # via # tox # virtualenv flake8==7.0.0 # via # flake8-bugbear # flake8-comprehensions # flake8-html # flake8-mutable # flake8-pyi flake8-bugbear==24.2.6 # via -r requirements.in flake8-comprehensions==3.14.0 # via -r requirements.in flake8-html==0.4.3 # via -r requirements.in flake8-logging-format==0.9.0 # via -r requirements.in flake8-mutable==1.2.0 # via -r requirements.in flake8-pyi==24.1.0 # via -r requirements.in fsfe-reuse==1.0.0 # via -r requirements.in idna==3.6 # via requests iniconfig==2.0.0 # via pytest invoke==2.2.0 # via -r requirements.in isort==5.13.2 # via -r requirements.in jinja2==3.1.3 # via # flake8-html # pytest-html # reuse license-expression==30.2.0 # via reuse markupsafe==2.1.5 # via jinja2 mccabe==0.7.0 # via flake8 mypy==1.8.0 # via -r requirements.in mypy-extensions==1.0.0 # via # black # mypy packaging==23.2 # via # black # build # pytest # tox pathspec==0.12.1 # via black pip==24.0 # via # -r requirements.in # pip-tools pip-tools==7.4.0 # via -r requirements.in platformdirs==4.2.0 # via # black # virtualenv pluggy==1.4.0 # via # pytest # tox py==1.11.0 # via tox pycodestyle==2.11.1 # via flake8 pyflakes==3.2.0 # via # flake8 # flake8-pyi pygments==2.17.2 # via flake8-html pyproject-hooks==1.0.0 # via # build # pip-tools pytest==8.0.2 # via # -r requirements.in # pytest-cov # pytest-html # pytest-metadata pytest-cov==4.1.0 # via -r requirements.in pytest-html==4.1.1 # via -r requirements.in pytest-metadata==3.1.1 # via pytest-html python-debian==0.1.49 # via reuse pytoml==0.1.21 # via # dflit # dflit-core requests==2.31.0 # via dflit reuse==3.0.1 # via fsfe-reuse setuptools==69.1.1 # via # -r requirements.in # pip-tools six==1.16.0 # via tox tomli==2.0.1 # via # black # build # coverage # mypy # pip-tools # pyproject-hooks # pytest # tox tox==3.28.0 # via # -r requirements.in # tox-pyenv tox-pyenv==1.1.0 # via -r requirements.in types-invoke==2.0.0.10 # via -r requirements.in typing-extensions==4.10.0 # via # black # mypy urllib3==2.2.1 # via requests virtualenv==20.25.1 # via tox wheel==0.42.0 # via pip-tools pytest-mypy-testing-0.1.3/requirements.txt.license000066400000000000000000000001151457145253100224230ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 pytest-mypy-testing-0.1.3/src/000077500000000000000000000000001457145253100163105ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/000077500000000000000000000000001457145253100224535ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/__init__.py000066400000000000000000000002351457145253100245640ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT """Pytest plugin to check mypy output.""" __version__ = "0.1.3" pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/message.py000066400000000000000000000235761457145253100244660ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT """Severity and Message""" import dataclasses import enum import os import pathlib import re from typing import Optional, Tuple, Union __all__ = [ "Message", "Severity", ] class Severity(enum.Enum): """Severity of a mypy message.""" NOTE = 1 WARNING = 2 ERROR = 3 FATAL = 4 @classmethod def from_string(cls, string: str) -> "Severity": return _string_to_severity[string.upper()] def __str__(self) -> str: return self.name.lower() def __repr__(self) -> str: return f"{self.__class__.__qualname__}.{self.name}" _string_to_severity = { "R": Severity.NOTE, "N": Severity.NOTE, "W": Severity.WARNING, "E": Severity.ERROR, "F": Severity.FATAL, } _COMMENT_MESSAGES = frozenset( [ ( Severity.NOTE, "See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports", ), ] ) @dataclasses.dataclass class Message: """Mypy message""" filename: str = "" lineno: int = 0 colno: Optional[int] = None severity: Severity = Severity.ERROR message: str = "" revealed_type: Optional[str] = None error_code: Optional[str] = None TupleType = Tuple[ str, int, Optional[int], Severity, str, Optional[str], Optional[str] ] _prefix: str = dataclasses.field(init=False, repr=False, default="") COMMENT_RE = re.compile( r"^(?:# *type: *ignore *)?(?:# *)?" r"(?P[RENW]):" r"((?P\d+):)?" r" *" r"(?P[^#]*)" r"(?:#.*?)?$" ) MESSAGE_AND_ERROR_CODE = re.compile( r"(?P[^\[][^#]*?)" r" +" r"\[(?P[^\]]*)\]" ) OUTPUT_RE = re.compile( r"^(?P([a-zA-Z]:)?[^:]+):" r"(?P[0-9]+):" r"((?P[0-9]+):)?" r" *(?P(error|note|warning)):" r"(?P.*?)" r"$" ) _OUTPUT_REVEALED_RE = re.compile( "^Revealed type is (?P'[^']+'|\"[^\"]+\")$" ) _INFERRED_TYPE_ASTERISK_RE = re.compile("(?<=[A-Za-z])[*]") def __post_init__(self): parts = [self.filename, str(self.lineno)] if self.colno: parts.append(str(self.colno)) self._prefix = ":".join(parts) + ":" if not self.revealed_type: revealed_m = self._OUTPUT_REVEALED_RE.match(self.message) if revealed_m: self.revealed_type = revealed_m.group("quoted_type")[1:-1] if self.revealed_type: # Remove the '*' for inferred types from reveal_type output. # This matches the behavior of mypy 0.950 and newer. self.revealed_type = self._INFERRED_TYPE_ASTERISK_RE.sub( "", self.revealed_type ) @property def normalized_message(self) -> str: """Normalized message. >>> m = Message("foo.py", 1, 1, Severity.NOTE, 'Revealed type is "float"') >>> m.normalized_message "Revealed type is 'float'" """ if self.revealed_type: return "Revealed type is {!r}".format(self.revealed_type) else: return self.message.replace("'", '"') def astuple(self, *, normalized: bool = False) -> "Message.TupleType": """Return a tuple representing this message. >>> m = Message("foo.py", 1, 1, Severity.NOTE, 'Revealed type is "float"') >>> m.astuple() ('foo.py', 1, 1, Severity.NOTE, 'Revealed type is "float"', 'float', None) """ return ( self.filename, self.lineno, self.colno, self.severity, self.normalized_message if normalized else self.message, self.revealed_type, self.error_code, ) def is_comment(self) -> bool: return (self.severity, self.message) in _COMMENT_MESSAGES def _as_short_tuple( self, *, normalized: bool = False, default_message: str = "", default_error_code: Optional[str] = None, ) -> "Message.TupleType": if normalized: message = self.normalized_message else: message = self.message return ( self.filename, self.lineno, None, self.severity, message or default_message, self.revealed_type, self.error_code or default_error_code, ) def __hash__(self) -> int: t = (self.filename, self.lineno, self.severity, self.revealed_type) return hash(t) def __eq__(self, other): """Compare if *self* and *other* are equal. Returns `True` if *other* is a :obj:`Message:` object considered to be equal to *self*. >>> Message() == Message() True >>> Message(error_code="baz") == Message(message="some text", error_code="baz") True >>> Message(message="some text") == Message(message="some text", error_code="baz") True >>> Message() == Message(message="some text", error_code="baz") False >>> Message(error_code="baz") == Message(error_code="bax") False """ if isinstance(other, Message): default_error_code = self.error_code or other.error_code if self.error_code and other.error_code: default_message = self.normalized_message or other.normalized_message else: default_message = "" def to_tuple(m: Message): return m._as_short_tuple( normalized=True, default_message=default_message, default_error_code=default_error_code, ) if self.colno is None or other.colno is None: return to_tuple(self) == to_tuple(other) else: return self.astuple(normalized=True) == other.astuple(normalized=True) else: return NotImplemented def __str__(self) -> str: return self.to_string(prefix=f"{self._prefix} ") def to_string(self, prefix: Optional[str] = None) -> str: prefix = prefix or f"{self._prefix} " error_code = f" [{self.error_code}]" if self.error_code else "" return f"{prefix}{self.severity.name.lower()}: {self.message}{error_code}" @classmethod def __split_message_and_error_code(cls, msg: str) -> Tuple[str, Optional[str]]: msg = msg.strip() if msg.startswith("[") and msg.endswith("]"): return "", msg[1:-1] else: m = cls.MESSAGE_AND_ERROR_CODE.fullmatch(msg) if m: return m.group("message"), m.group("error_code") else: return msg, None @classmethod def from_comment( cls, filename: Union[pathlib.Path, str], lineno: int, comment: str ) -> "Message": """Create message object from Python *comment*. >>> Message.from_comment("foo.py", 1, "R: foo") Message(filename='foo.py', lineno=1, colno=None, severity=Severity.NOTE, message="Revealed type is 'foo'", revealed_type='foo', error_code=None) >>> Message.from_comment("foo.py", 1, "E: [assignment]") Message(filename='foo.py', lineno=1, colno=None, severity=Severity.ERROR, message='', revealed_type=None, error_code='assignment') """ m = cls.COMMENT_RE.match(comment.strip()) if not m: raise ValueError("Not a valid mypy message comment") colno = int(m.group("colno")) if m.group("colno") else None message, error_code = cls.__split_message_and_error_code( m.group("message_and_error_code") ) if m.group("severity") == "R": revealed_type = message message = "Revealed type is {!r}".format(message) else: revealed_type = None return Message( str(filename), lineno=lineno, colno=colno, severity=Severity.from_string(m.group("severity")), message=message, revealed_type=revealed_type, error_code=error_code, ) @classmethod def from_output(cls, line: str) -> "Message": """Create message object from mypy output line. >>> m = Message.from_output("z.py:1: note: bar") >>> (m.lineno, m.colno, m.severity, m.message, m.revealed_type, m.error_code) (1, None, Severity.NOTE, 'bar', None, None) >>> m = Message.from_output("z.py:1:13: note: bar") >>> (m.lineno, m.colno, m.severity, m.message, m.revealed_type, m.error_code) (1, 13, Severity.NOTE, 'bar', None, None) >>> m = Message.from_output("z.py:1: note: Revealed type is 'bar'") >>> (m.lineno, m.colno, m.severity, m.message, m.revealed_type, m.error_code) (1, None, Severity.NOTE, "Revealed type is 'bar'", 'bar', None) >>> m = Message.from_output('z.py:1: note: Revealed type is "bar"') >>> (m.lineno, m.colno, m.severity, m.message, m.revealed_type, m.error_code) (1, None, Severity.NOTE, 'Revealed type is "bar"', 'bar', None) >>> m = Message.from_output("z.py:1:13: error: bar [baz]") >>> (m.lineno, m.colno, m.severity, m.message, m.revealed_type, m.error_code) (1, 13, Severity.ERROR, 'bar', None, 'baz') """ m = cls.OUTPUT_RE.match(line) if not m: raise ValueError("Not a valid mypy message") message, error_code = cls.__split_message_and_error_code( m.group("message_and_error_code") ) return cls( os.path.abspath(m.group("fname")), lineno=int(m.group("lineno")), colno=int(m.group("colno")) if m.group("colno") else None, severity=Severity[m.group("severity").upper()], message=message, error_code=error_code, ) pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/output_processing.py000066400000000000000000000116451457145253100266300ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT import dataclasses import difflib import itertools from typing import Dict, Iterator, List, Sequence, Tuple from .message import Message, Severity from .strutil import common_prefix @dataclasses.dataclass class OutputMismatch: actual: List[Message] = dataclasses.field(default_factory=lambda: []) expected: List[Message] = dataclasses.field(default_factory=lambda: []) lineno: int = dataclasses.field(init=False, default=0) lines: List[str] = dataclasses.field(init=False, default_factory=lambda: []) error_message: str = dataclasses.field(init=False, default="") @property def actual_lineno(self) -> int: if self.actual: return self.actual[0].lineno raise RuntimeError("No actual messages") @property def expected_lineno(self) -> int: if self.expected: return self.expected[0].lineno raise RuntimeError("No expected messages") @property def actual_severity(self) -> Severity: if not self.actual: raise RuntimeError("No actual messages") return Severity(max(msg.severity.value for msg in self.actual)) @property def expected_severity(self) -> Severity: if not self.expected: raise RuntimeError("No expected messages") return Severity(max(msg.severity.value for msg in self.expected)) def __post_init__(self) -> None: def _fmt(msg: Message, actual_expected: str = "", *, indent: str = " ") -> str: if actual_expected: actual_expected += ": " return msg.to_string(prefix=f"{indent}{actual_expected}") if not any([self.actual, self.expected]): raise ValueError("At least one of actual and expected must be given") if self.actual: self.lineno = self.actual_lineno elif self.expected: self.lineno = self.expected_lineno assert self.lines == [] if self.actual and self.expected: if self.actual_lineno != self.expected_lineno: raise ValueError("line numbers do not match") self.error_message = f"{self.actual[0].severity} (mismatch):" if len(self.actual) == len(self.expected) == 1: sp = " " * len( common_prefix(self.actual[0].message, self.expected[0].message) ) sp_lines = [f" {sp}^"] else: sp_lines = [] self.lines = ( [_fmt(msg, "A") for msg in self.actual] + [_fmt(msg, "E") for msg in self.expected] + sp_lines ) elif self.actual: if len(self.actual) == 1: self.error_message = ( f"{self.actual_severity} (unexpected): {self.actual[0].message}" ) else: self.error_message = f"{self.actual_severity} (unexpected):" self.lines = [_fmt(msg, "A") for msg in self.actual] else: assert self.expected if len(self.expected) == 1: self.error_message = ( f"{self.expected_severity} (missing): {self.expected[0].message}" ) else: self.error_message = f"{self.expected_severity} (missing):" self.lines = [_fmt(msg, "E") for msg in self.expected] def diff_message_sequences( actual_messages: Sequence[Message], expected_messages: Sequence[Message] ) -> List[OutputMismatch]: """Diff lists of messages""" def _chunk_to_dict(chunk: Sequence[Message]) -> Dict[int, List[Message]]: d: Dict[int, List[Message]] = {} for msg in chunk: d.setdefault(msg.lineno, []).append(msg) return d errors: List[OutputMismatch] = [] for a_chunk, b_chunk in iter_msg_seq_diff_chunks( actual_messages, expected_messages ): a_dict = _chunk_to_dict(a_chunk) b_dict = _chunk_to_dict(b_chunk) linenos_set = set(a_dict.keys()) | set(b_dict.keys()) linenos = sorted(linenos_set) for lineno in linenos: actual = a_dict.get(lineno, []) expected = b_dict.get(lineno, []) if any((not msg.is_comment()) for msg in itertools.chain(actual, expected)): errors.append(OutputMismatch(actual=actual, expected=expected)) return errors def iter_msg_seq_diff_chunks( a: Sequence[Message], b: Sequence[Message] ) -> Iterator[Tuple[Sequence[Message], Sequence[Message]]]: """Iterate over sequences of not matching messages""" seq_matcher = difflib.SequenceMatcher(isjunk=None, a=a, b=b, autojunk=False) for tag, i1, i2, j1, j2 in seq_matcher.get_opcodes(): if tag == "equal": continue actual = a[i1:i2] expected = b[j1:j2] if actual or expected: yield actual, expected pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/parser.py000066400000000000000000000141541457145253100243260ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT """Parse a Python file to determine the mypy test cases.""" import ast import dataclasses import io import itertools import os import pathlib import sys import tokenize from typing import Iterable, Iterator, List, Optional, Set, Tuple, Union from .message import Message __all__ = ["parse_file"] @dataclasses.dataclass class MypyTestItem: name: str lineno: int end_lineno: int expected_messages: List[Message] func_node: Optional[Union[ast.FunctionDef, ast.AsyncFunctionDef]] = None marks: Set[str] = dataclasses.field(default_factory=lambda: set()) actual_messages: List[Message] = dataclasses.field(default_factory=lambda: []) @classmethod def from_ast_node( cls, func_node: Union[ast.FunctionDef, ast.AsyncFunctionDef], marks: Optional[Set[str]] = None, unfiltered_messages: Optional[Iterable[Message]] = None, ) -> "MypyTestItem": if not isinstance(func_node, (ast.FunctionDef, ast.AsyncFunctionDef)): raise ValueError( f"Invalid func_node type: Got {type(func_node)}, " f"expected {ast.FunctionDef} or {ast.AsyncFunctionDef}" ) lineno = func_node.lineno end_lineno = getattr(func_node, "end_lineno", 0) for node in func_node.decorator_list: lineno = min(lineno, node.lineno) if unfiltered_messages is not None: expected_messages = [ msg for msg in unfiltered_messages if lineno <= msg.lineno <= end_lineno ] else: expected_messages = [] return cls( name=func_node.name, lineno=lineno, end_lineno=end_lineno, expected_messages=expected_messages, func_node=func_node, marks=(marks or set()), ) @dataclasses.dataclass class MypyTestFile: filename: str source_lines: List[str] = dataclasses.field(default_factory=lambda: []) items: List[MypyTestItem] = dataclasses.field(default_factory=lambda: []) messages: List[Message] = dataclasses.field(default_factory=lambda: []) def iter_comments( filename: Union[pathlib.Path, str], token_lists: List[List[tokenize.TokenInfo]] ) -> Iterator[tokenize.TokenInfo]: for toks in token_lists: for tok in toks: if tok.type == tokenize.COMMENT: yield tok def iter_mypy_comments( filename: Union[pathlib.Path, str], tokens: List[List[tokenize.TokenInfo]] ) -> Iterator[Message]: for tok in iter_comments(filename, tokens): try: yield Message.from_comment(filename, tok.start[0], tok.string) except ValueError: pass def generate_per_line_token_lists(source: str) -> Iterator[List[tokenize.TokenInfo]]: i = 0 for lineno, group in itertools.groupby( tokenize.generate_tokens(io.StringIO(source).readline), lambda tok: tok.start[0], ): assert 0 <= lineno <= 10000000 while i < lineno: yield [] i += 1 yield list(group) i += 1 def parse_file(filename: Union[os.PathLike, str, pathlib.Path], config) -> MypyTestFile: """Parse *filename* and return information about mypy test cases.""" filename = pathlib.Path(filename).resolve() with open(filename, "r", encoding="utf-8") as f: source_text = f.read() source_lines = source_text.splitlines() token_lists = list(generate_per_line_token_lists(source_text)) messages = list(iter_mypy_comments(filename, token_lists)) tree = ast.parse(source_text, filename=str(filename)) if sys.version_info < (3, 8): _add_end_lineno_if_missing(tree, len(source_lines)) items: List[MypyTestItem] = [] for node in ast.iter_child_nodes(tree): if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): continue marks = _find_marks(node) if "mypy_testing" in marks: items.append( MypyTestItem.from_ast_node( node, marks=marks, unfiltered_messages=messages ) ) return MypyTestFile( filename=str(filename), source_lines=source_lines, items=items, messages=messages, ) def _add_end_lineno_if_missing(tree, line_count: int): """Add end_lineno attribute to top-level nodes if missing""" prev_node: Optional[ast.AST] = None for node in ast.iter_child_nodes(tree): if prev_node is not None: setattr(prev_node, "end_lineno", node.lineno) # noqa: B010 prev_node = node if prev_node: setattr(prev_node, "end_lineno", line_count) # noqa: B010 def _find_marks(func_node: Union[ast.FunctionDef, ast.AsyncFunctionDef]) -> Set[str]: return { name.split(".", 2)[2] for name, _ in _iter_func_decorators(func_node) if name.startswith("pytest.mark.") } def _iter_func_decorators( func_node: Union[ast.FunctionDef, ast.AsyncFunctionDef] ) -> Iterator[Tuple[str, ast.AST]]: def dotted(*nodes): return ".".join(_get_node_name(node) for node in reversed(nodes)) for decorator_node in func_node.decorator_list: if isinstance(decorator_node, (ast.Name, ast.Attribute)): node, attrs = _unwrap_ast_attributes(decorator_node) if isinstance(node, ast.Name): yield dotted(*attrs, node), decorator_node elif isinstance(decorator_node, ast.Call): node, attrs = _unwrap_ast_attributes(decorator_node.func) if isinstance(node, ast.Name): yield dotted(*attrs, node), decorator_node def _get_node_name(node) -> str: if isinstance(node, ast.Attribute): return node.attr elif isinstance(node, ast.Name): return node.id else: raise RuntimeError(f"Unsupported node type: {type(node)}") # pragma: no cover def _unwrap_ast_attributes(node) -> Tuple[ast.AST, List[ast.Attribute]]: attrs: List[ast.Attribute] = [] while isinstance(node, ast.Attribute): attrs.append(node) node = node.value return node, attrs pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/plugin.py000066400000000000000000000170411457145253100243260ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT import os import pathlib import tempfile from typing import Iterable, Iterator, List, NamedTuple, Optional, Tuple, Union import mypy.api import pytest from _pytest._code.code import ReprEntry, ReprFileLocation from _pytest.config import Config from _pytest.python import path_matches_patterns from .message import Message, Severity from .output_processing import OutputMismatch, diff_message_sequences from .parser import MypyTestItem, parse_file PYTEST_VERSION = pytest.__version__ PYTEST_VERSION_INFO = tuple(int(part) for part in PYTEST_VERSION.split(".")[:3]) class MypyResult(NamedTuple): mypy_args: List[str] returncode: int output_lines: List[str] file_messages: List[Message] non_item_messages: List[Message] class MypyAssertionError(AssertionError): def __init__(self, item, errors: Iterable[OutputMismatch]): super().__init__(item, errors) self.item = item self.errors = errors class PytestMypyTestItem(pytest.Item): parent: "PytestMypyFile" def __init__( self, name: str, parent: "PytestMypyFile", *, mypy_item: MypyTestItem, config: Optional[Config] = None, **kwargs, ) -> None: if config is None: config = parent.config super().__init__(name, parent=parent, config=config, **kwargs) self.add_marker("mypy") self.mypy_item = mypy_item for mark in self.mypy_item.marks: self.add_marker(mark) @classmethod def from_parent(cls, parent, name, mypy_item): return super().from_parent(parent=parent, name=name, mypy_item=mypy_item) def runtest(self) -> None: returncode, actual_messages = self.parent.run_mypy(self.mypy_item) errors = diff_message_sequences( actual_messages, self.mypy_item.expected_messages ) if errors: raise MypyAssertionError(item=self, errors=errors) def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]: return self.parent.path, self.mypy_item.lineno, self.name def repr_failure(self, excinfo, style=None): if not excinfo.errisinstance(MypyAssertionError): return super().repr_failure(excinfo, style=style) # pragma: no cover reprfileloc_key = "reprfileloc" exception_repr = excinfo.getrepr(style="short") exception_repr.reprcrash.message = "" exception_repr.reprtraceback.reprentries = [ ReprEntry( lines=mismatch.lines, style="short", reprlocals=None, reprfuncargs=None, **{ reprfileloc_key: ReprFileLocation( path=str(self.parent.path), lineno=mismatch.lineno, message=mismatch.error_message, ) }, ) for mismatch in excinfo.value.errors ] return exception_repr class PytestMypyFile(pytest.File): def __init__( self, *, parent=None, config=None, session=None, nodeid=None, **kwargs, ) -> None: if config is None: config = getattr(parent, "config", None) super().__init__( parent=parent, config=config, session=session, nodeid=nodeid, **kwargs, ) self.add_marker("mypy") self.mypy_file = parse_file(self.path, config=config) self._mypy_result: Optional[MypyResult] = None @classmethod def from_parent(cls, parent, **kwargs): return super().from_parent(parent=parent, **kwargs) def collect(self) -> Iterator[PytestMypyTestItem]: for item in self.mypy_file.items: yield PytestMypyTestItem.from_parent( parent=self, name="[mypy]" + item.name, mypy_item=item ) def run_mypy(self, item: MypyTestItem) -> Tuple[int, List[Message]]: if self._mypy_result is None: self._mypy_result = self._run_mypy(self.path) return ( self._mypy_result.returncode, sorted( item.actual_messages + self._mypy_result.non_item_messages, key=lambda msg: msg.lineno, ), ) def _run_mypy(self, filename: Union[pathlib.Path, os.PathLike, str]) -> MypyResult: filename = pathlib.Path(filename) with tempfile.TemporaryDirectory(prefix="pytest-mypy-testing-") as tmp_dir_name: mypy_cache_dir = os.path.join(tmp_dir_name, "mypy_cache") os.makedirs(mypy_cache_dir) mypy_args = [ "--cache-dir={}".format(mypy_cache_dir), "--check-untyped-defs", "--hide-error-context", "--no-color-output", "--no-error-summary", "--no-pretty", "--soft-error-limit=-1", "--no-silence-site-packages", "--no-warn-unused-configs", "--show-column-numbers", "--show-error-codes", "--show-traceback", str(filename), ] out, err, returncode = mypy.api.run(mypy_args) lines = (out + err).splitlines() file_messages = [ msg for msg in map(Message.from_output, lines) if (msg.filename == self.mypy_file.filename) and not ( msg.severity is Severity.NOTE and msg.message == "See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports" ) ] non_item_messages = [] for msg in file_messages: for item in self.mypy_file.items: if item.lineno <= msg.lineno <= item.end_lineno: item.actual_messages.append(msg) break else: non_item_messages.append(msg) return MypyResult( mypy_args=mypy_args, returncode=returncode, output_lines=lines, file_messages=file_messages, non_item_messages=non_item_messages, ) def pytest_collect_file(file_path: pathlib.Path, parent): if file_path.suffix == ".mypy-testing" or _is_pytest_test_file(file_path, parent): file = PytestMypyFile.from_parent(parent=parent, path=file_path) if file.mypy_file.items: return file return None def _is_pytest_test_file(file_path: pathlib.Path, parent): """Return `True` if *path* is considered to be a pytest test file.""" # Based on _pytest/python.py::pytest_collect_file fn_patterns = parent.config.getini("python_files") + ["__init__.py"] return file_path.suffix == ".py" and ( parent.session.isinitpath(file_path) or path_matches_patterns(file_path, fn_patterns) ) def pytest_configure(config): """ Register a custom marker for MypyItems, and configure the plugin based on the CLI. """ _add_reveal_type_to_builtins() config.addinivalue_line( "markers", "mypy_testing: mark functions to be used for mypy testing." ) config.addinivalue_line( "markers", "mypy: mark mypy tests. Do not add this marker manually!" ) def _add_reveal_type_to_builtins(): # Add a reveal_type function to the builtins module import builtins if not hasattr(builtins, "reveal_type"): setattr(builtins, "reveal_type", lambda x: x) # noqa: B010 pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/py.typed000066400000000000000000000001151457145253100241470ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 pytest-mypy-testing-0.1.3/src/pytest_mypy_testing/strutil.py000066400000000000000000000006361457145253100245400ustar00rootroot00000000000000# SPDX-FileCopyrightText: 2020 David Fritzsche # SPDX-License-Identifier: Apache-2.0 OR MIT import textwrap def common_prefix(a: str, b: str) -> str: """Determine the common prefix of *a* and *b*.""" if len(a) > len(b): a, b = b, a for i in range(len(a)): if a[i] != b[i]: return a[:i] return a def dedent(a: str) -> str: return textwrap.dedent(a).lstrip("\n") pytest-mypy-testing-0.1.3/tasks.py000066400000000000000000000046671457145253100172350ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import os import sys from invoke import task MAYBE_PTY = sys.platform != "win32" @task def mkdir(ctx, dirname): os.makedirs(dirname, exist_ok=True) @task def pth(ctx): import sysconfig site_packages_dir = sysconfig.get_path("purelib") pth_filename = os.path.join(site_packages_dir, "subprojects.pth") with open(pth_filename, "w", encoding="utf-8") as f: print(os.path.abspath("src"), file=f) @task(pre=[pth]) def tox(ctx, parallel="auto", e="ALL"): import fnmatch import itertools env_patterns = list(filter(None, e.split(","))) result = ctx.run("tox --listenvs-all", hide=True, pty=False) all_envs = result.stdout.splitlines() if any(pat == "ALL" for pat in env_patterns): envs = set(all_envs) else: envs = set( itertools.chain.from_iterable( fnmatch.filter(all_envs, pat) for pat in env_patterns ) ) envlist = ",".join(sorted(envs)) ctx.run(f"tox --parallel={parallel} -e {envlist}", echo=True, pty=MAYBE_PTY) @task def mypy(ctx): ctx.run("mypy src tests", echo=True, pty=MAYBE_PTY) @task def flake8(ctx): ctx.run("flake8", echo=True, pty=MAYBE_PTY) @task(pre=[pth]) def pytest(ctx): cmd = [ "pytest", # "-s", # "--log-cli-level=DEBUG", "--cov=pytest_mypy_testing", "--cov-report=html:build/cov_html", "--cov-report=term:skip-covered", ] ctx.run(" ".join(cmd), echo=True, pty=MAYBE_PTY) @task def black(ctx): ctx.run("black --check --diff .", echo=True, pty=MAYBE_PTY) @task def reuse_lint(ctx): ctx.run("reuse lint", echo=True, pty=MAYBE_PTY) @task def black_reformat(ctx): ctx.run("black .", echo=True, pty=MAYBE_PTY) @task def lock_requirements(ctx, upgrade=False): cmd = "pip-compile --allow-unsafe --no-index" if upgrade: cmd += " --upgrade" ctx.run(cmd, env={"CUSTOM_COMPILE_COMMAND": cmd}, echo=True, pty=MAYBE_PTY) @task def build(ctx): result = ctx.run("git show -s --format=%ct HEAD") timestamp = result.stdout.strip() cmd = "flit build" ctx.run(cmd, env={"SOURCE_DATE_EPOCH": timestamp}, echo=True, pty=MAYBE_PTY) @task def publish(ctx, repository="testpypi"): cmd = "flit publish --repository=%s" % (repository,) ctx.run(cmd, echo=True, pty=MAYBE_PTY) @task(pre=[mypy, pytest, flake8]) def check(ctx): pass pytest-mypy-testing-0.1.3/tests/000077500000000000000000000000001457145253100166635ustar00rootroot00000000000000pytest-mypy-testing-0.1.3/tests/conftest.py000066400000000000000000000005051457145253100210620ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 def pytest_cmdline_main(config): """Load pytest_mypy_testing if not already present.""" if not config.pluginmanager.get_plugin("mypy-testing"): from pytest_mypy_testing import plugin config.pluginmanager.register(plugin) pytest-mypy-testing-0.1.3/tests/test___init__.py000066400000000000000000000003241457145253100220320ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import re from pytest_mypy_testing import __version__ def test_version(): assert re.match("^[0-9]*([.][0-9]*)*$", __version__) pytest-mypy-testing-0.1.3/tests/test_basics.mypy-testing000066400000000000000000000046631457145253100235720ustar00rootroot00000000000000# -*- mode: python; -*- # SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import pytest @pytest.mark.mypy_testing def mypy_test_invalid_assignment(): foo = "abc" foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") @pytest.mark.mypy_testing def mypy_test_invalid_assignment_with_error_code(): foo = "abc" foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment] @pytest.mark.xfail @pytest.mark.mypy_testing def mypy_test_invalid_assignment_with_error_code__message_does_not_match(): foo = "abc" foo = 123 # E: Invalid assignment [assignment] @pytest.mark.mypy_testing def mypy_test_invalid_assignment_only_error_code(): foo = "abc" foo = 123 # E: [assignment] @pytest.mark.xfail @pytest.mark.mypy_testing def mypy_test_invalid_assignment_only_error_code__error_code_does_not_match(): foo = "abc" foo = 123 # E: [baz] @pytest.mark.xfail @pytest.mark.mypy_testing def mypy_test_invalid_assignment_no_message_and_no_error_code(): foo = "abc" foo = 123 # E: @pytest.mark.mypy_testing def mypy_test_use_reveal_type(): reveal_type(123) # N: Revealed type is 'Literal[123]?' reveal_type(456) # R: Literal[456]? @pytest.mark.mypy_testing def mypy_test_use_reveal_type__float_var(): some_float = 123.03 reveal_type(some_float) # R: builtins.float @pytest.mark.mypy_testing def mypy_test_use_reveal_type__int_var(): some_int = 123 reveal_type(some_int) # R: builtins.int @pytest.mark.mypy_testing def mypy_test_use_reveal_type__int_list_var(): some_list = [123] reveal_type(some_list) # R: builtins.list[builtins.int] @pytest.mark.mypy_testing def mypy_test_use_reveal_type__int_list_var__with__inferred_asterisk(): some_list = [123] reveal_type(some_list) # R: builtins.list[builtins.int*] @pytest.mark.mypy_testing @pytest.mark.skip("foo") def mypy_test_use_skip_marker(): reveal_type(123) # N: Revealed type is 'Literal[123]?' reveal_type(456) # R: Literal[456]? @pytest.mark.mypy_testing @pytest.mark.xfail def mypy_test_xfail_wrong_reveal_type(): reveal_type(456) # R: float @pytest.mark.mypy_testing @pytest.mark.xfail def mypy_test_xfail_missing_note(): "nothing" # N: missing @pytest.mark.mypy_testing @pytest.mark.xfail def mypy_test_xfail_unexpected_note(): reveal_type([]) # unexpected message pytest-mypy-testing-0.1.3/tests/test_file_with_nonitem_messages.mypy-testing000066400000000000000000000005271457145253100277130ustar00rootroot00000000000000# -*- mode: python; -*- # SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import pytest a = 123 a = "abc" # mypy error not covered by the test case below @pytest.mark.mypy_testing @pytest.mark.xfail def mypy_test_xfail_unexpected_note(): """Test case that fails due to not covered non-item error above.""" pytest-mypy-testing-0.1.3/tests/test_message.py000066400000000000000000000057061457145253100217300ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 from typing import Optional import pytest from pytest_mypy_testing.message import Message, Severity @pytest.mark.parametrize( "string,expected", [("r", Severity.NOTE), ("N", Severity.NOTE)] ) def test_init_severity(string: str, expected: Severity): assert Severity.from_string(string) == expected @pytest.mark.parametrize( "filename,comment,severity,message,error_code", [ ("z.py", "# E: bar", Severity.ERROR, "bar", None), ("z.py", "# E: bar", Severity.ERROR, "bar", "foo"), ("z.py", "# E: bar [foo]", Severity.ERROR, "bar", "foo"), ("z.py", "# E: bar [foo]", Severity.ERROR, "bar", ""), ("z.py", "#type:ignore# W: bar", Severity.WARNING, "bar", None), ("z.py", "# type: ignore # W: bar", Severity.WARNING, "bar", None), ("z.py", "# R: bar", Severity.NOTE, "Revealed type is 'bar'", None), ], ) def test_message_from_comment( filename: str, comment: str, severity: Severity, message: str, error_code: Optional[str], ): lineno = 123 actual = Message.from_comment(filename, lineno, comment) expected = Message( filename=filename, lineno=lineno, colno=None, severity=severity, message=message, error_code=error_code, ) assert actual == expected def test_message_from_invalid_comment(): with pytest.raises(ValueError): Message.from_comment("foo.py", 1, "# fubar") @pytest.mark.parametrize( "line,severity,message", [ ("z.py:1: note: bar", Severity.NOTE, "bar"), ("z.py:1:2: note: bar", Severity.NOTE, "bar"), ("z.py:1:2: error: fubar", Severity.ERROR, "fubar"), ], ) def test_message_from_output(line: str, severity: Severity, message: str): msg = Message.from_output(line) assert msg.message == message assert msg.severity == severity @pytest.mark.parametrize( "output", [ "foo.py:a: fubar", "fubar", "foo.py:1: fubar", "foo.py:1:1: fubar", "foo.py:1:1: not: fubar", ], ) def test_message_from_invalid_output(output): with pytest.raises(ValueError): Message.from_output(output) MSG_WITHOUT_COL = Message("z.py", 13, None, Severity.NOTE, "foo") MSG_WITH_COL = Message("z.py", 13, 23, Severity.NOTE, "foo") @pytest.mark.parametrize( "a,b", [ (MSG_WITH_COL, MSG_WITH_COL), (MSG_WITH_COL, MSG_WITHOUT_COL), (MSG_WITHOUT_COL, MSG_WITHOUT_COL), (MSG_WITHOUT_COL, MSG_WITH_COL), ], ) def test_message_eq(a: Message, b: Message): assert a == b assert not (a != b) def test_message_neq_with_not_message(): assert MSG_WITH_COL != 23 assert MSG_WITH_COL != "abc" def test_message_hash(): assert hash(MSG_WITH_COL) == hash(MSG_WITHOUT_COL) def test_message_str(): assert str(MSG_WITHOUT_COL) == "z.py:13: note: foo" assert str(MSG_WITH_COL) == "z.py:13:23: note: foo" pytest-mypy-testing-0.1.3/tests/test_output_processing.py000066400000000000000000000105641457145253100240760ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 from typing import List, Tuple import pytest from pytest_mypy_testing.message import Message, Severity from pytest_mypy_testing.output_processing import ( OutputMismatch, diff_message_sequences, iter_msg_seq_diff_chunks, ) ERROR = Severity.ERROR NOTE = Severity.NOTE WARNING = Severity.WARNING MSGS_DIFF_A_SINGLE = [Message("z.py", 15, 1, NOTE, "diff-a")] MSGS_DIFF_B_SINGLE = [Message("z.py", 15, 1, NOTE, "diff-b")] MSGS_DIFF_A_MULTI = [ Message("z.py", 25, 1, NOTE, "diff-a"), Message("z.py", 25, 1, NOTE, "diff-a error"), ] MSGS_DIFF_B_MULTI = [Message("z.py", 25, 1, NOTE, "diff-b")] MSGS_UNEXPECTED_A_SINGLE = [ Message("z.py", 35, 1, NOTE, "unexpected"), ] MSGS_UNEXPECTED_A_MULTI = [ Message("z.py", 45, 1, NOTE, "unexpected"), Message("z.py", 45, 1, ERROR, "unexpected error"), ] MSGS_MISSING_B_SINGLE = [ Message("z.py", 55, 1, NOTE, "missing"), ] MSGS_MISSING_B_MULTI = [ Message("z.py", 65, 1, ERROR, "missing error"), Message("z.py", 65, 1, NOTE, "missing note"), ] A = [ Message("z.py", 10, 3, ERROR, "equal error"), Message("z.py", 10, 3, NOTE, "equal"), *MSGS_DIFF_A_SINGLE, Message("z.py", 20, 1, NOTE, "equal"), *MSGS_DIFF_A_MULTI, Message("z.py", 30, 1, NOTE, "equal"), *MSGS_UNEXPECTED_A_SINGLE, Message("z.py", 40, 1, NOTE, "equal"), *MSGS_UNEXPECTED_A_MULTI, Message("z.py", 50, 1, NOTE, "equal"), Message("z.py", 60, 1, NOTE, "equal"), Message("z.py", 70, 1, NOTE, "equal"), ] B = [ Message("z.py", 10, 3, ERROR, "equal error"), Message("z.py", 10, 3, NOTE, "equal"), *MSGS_DIFF_B_SINGLE, Message("z.py", 20, 1, NOTE, "equal"), *MSGS_DIFF_B_MULTI, Message("z.py", 30, 1, NOTE, "equal"), Message("z.py", 40, 1, NOTE, "equal"), Message("z.py", 50, 1, NOTE, "equal"), *MSGS_MISSING_B_SINGLE, Message("z.py", 60, 1, NOTE, "equal"), *MSGS_MISSING_B_MULTI, Message("z.py", 70, 1, NOTE, "equal"), ] EXPECTED_DIFF_SEQUENCE: List[Tuple[List[Message], List[Message]]] = [ (MSGS_DIFF_A_SINGLE, MSGS_DIFF_B_SINGLE), (MSGS_DIFF_A_MULTI, MSGS_DIFF_B_MULTI), (MSGS_UNEXPECTED_A_SINGLE, []), (MSGS_UNEXPECTED_A_MULTI, []), ([], MSGS_MISSING_B_SINGLE), ([], MSGS_MISSING_B_MULTI), ] def test_output_mismatch_neither_actual_nor_expected(): with pytest.raises(ValueError): OutputMismatch() def test_output_mismatch_actual_lineno_or_severity_without_actual(): msg = Message("z.py", 1, 0, NOTE, "foo") om = OutputMismatch(expected=[msg]) with pytest.raises(RuntimeError): om.actual_lineno with pytest.raises(RuntimeError): om.actual_severity def test_output_mismatch_expected_lineno_or_severity_without_expected(): msg = Message("z.py", 1, 0, NOTE, "foo") om = OutputMismatch(actual=[msg]) with pytest.raises(RuntimeError): om.expected_lineno with pytest.raises(RuntimeError): om.expected_severity def test_output_mismatch_line_number_mismatch(): msg_a = Message("z.py", 1, 0, NOTE, "foo") msg_b = Message("z.py", 2, 0, NOTE, "foo") with pytest.raises(ValueError): OutputMismatch([msg_a], [msg_b]) def test_output_mismatch_with_actual_and_expected(): actual = Message("z.py", 17, 3, NOTE, "bar") expected = Message("z.py", 17, 3, NOTE, "foo") om = OutputMismatch(actual=[actual], expected=[expected]) assert om.lineno == actual.lineno == expected.lineno assert "note" in om.error_message assert om.lines def test_output_mismatch_only_expected(): expected = Message("z.py", 17, 3, NOTE, "foo") om = OutputMismatch(expected=[expected]) assert om.lineno == expected.lineno assert "note" in om.error_message assert "missing" in om.error_message assert not om.lines def test_output_mismatch_only_actual(): actual = Message("z.py", 17, 3, NOTE, "foo") om = OutputMismatch(actual=[actual]) assert om.lineno == actual.lineno assert "note" in om.error_message assert "unexpected" in om.error_message assert not om.lines def test_iter_diff_sequences(): diff = list(iter_msg_seq_diff_chunks(A, B)) assert diff == EXPECTED_DIFF_SEQUENCE def test_diff_message_sequences(): expected = [OutputMismatch(a, e) for a, e in EXPECTED_DIFF_SEQUENCE] actual = diff_message_sequences(A, B) assert actual == expected pytest-mypy-testing-0.1.3/tests/test_parser.py000066400000000000000000000075511457145253100216000ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import ast import sys import typing as _typing from tokenize import COMMENT, ENDMARKER, NAME, NEWLINE, NL, TokenInfo from unittest.mock import Mock import pytest from _pytest.config import Config from pytest_mypy_testing.parser import ( MypyTestItem, generate_per_line_token_lists, parse_file, ) from pytest_mypy_testing.strutil import dedent @pytest.mark.parametrize("node", [None, 123, "abc"]) def test_cannot_create_mypy_test_case_from_ast_node_without_valid_node(node): with pytest.raises(ValueError): MypyTestItem.from_ast_node(node) def test_create_mypy_test_case(): func = dedent( r""" @pytest.mark.mypy_testing @pytest.mark.skip() @foo.bar def mypy_foo(): pass """ ) tree = ast.parse(func, "func.py") func_nodes = [ node for node in ast.iter_child_nodes(tree) if isinstance(node, ast.FunctionDef) ] assert func_nodes tc = MypyTestItem.from_ast_node(func_nodes[0]) assert tc.lineno == 1 def test_iter_comments(): source = "\n".join(["# foo", "assert True # E: bar"]) actual = list(generate_per_line_token_lists(source)) # fmt: off expected:_typing.List[_typing.List[TokenInfo]] = [ [], # line 0 [ TokenInfo(type=COMMENT, string="# foo", start=(1, 0), end=(1, 5), line="# foo\n",), TokenInfo(type=NL, string="\n", start=(1, 5), end=(1, 6), line="# foo\n"), ], [ TokenInfo(type=NAME, string="assert", start=(2, 0), end=(2, 6), line="assert True # E: bar",), TokenInfo(type=NAME, string="True", start=(2, 7), end=(2, 11), line="assert True # E: bar",), TokenInfo(type=COMMENT, string="# E: bar", start=(2, 12), end=(2, 20), line="assert True # E: bar",), TokenInfo(type=NEWLINE, string="", start=(2, 20), end=(2, 21), line=""), ], [ TokenInfo(type=ENDMARKER, string="", start=(3, 0), end=(3, 0), line="") ], ] # fmt: on # some patching due to differences between Python versions... for lineno, line_toks in enumerate(actual): for i, tok in enumerate(line_toks): if tok.type == NEWLINE: try: expected_tok = expected[lineno][i] if expected_tok.type == NEWLINE: expected[lineno][i] = TokenInfo( type=expected_tok.type, string=expected_tok.string, start=expected_tok.start, end=expected_tok.end, line=tok.line, ) except IndexError: pass assert actual == expected def test_parse_file_basic_call_works_with_py37(monkeypatch, tmp_path): path = tmp_path / "parse_file_test.py" path.write_text( dedent( r""" # foo def test_mypy_foo(): pass @pytest.mark.mypy_testing def test_mypy_bar(): pass """ ) ) monkeypatch.setattr(sys, "version_info", (3, 7, 5)) config = Mock(spec=Config) parse_file(str(path), config) def test_parse_async(tmp_path): path = tmp_path / "test_async.mypy-testing" path.write_text( dedent( r""" import pytest @pytest.mark.mypy_testing async def mypy_test_invalid_assginment(): foo = "abc" foo = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") """ ) ) config = Mock(spec=Config) result = parse_file(str(path), config) assert len(result.items) == 1 item = result.items[0] assert item.name == "mypy_test_invalid_assginment" pytest-mypy-testing-0.1.3/tests/test_plugin.py000066400000000000000000000044701457145253100215770ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import pathlib from types import SimpleNamespace from unittest.mock import Mock import pytest from _pytest.config import Config from pytest_mypy_testing.message import Severity from pytest_mypy_testing.parser import MypyTestFile from pytest_mypy_testing.plugin import ( MypyAssertionError, PytestMypyFile, pytest_collect_file, ) from pytest_mypy_testing.strutil import dedent PYTEST_VERSION = pytest.__version__ PYTEST_VERSION_INFO = tuple(int(part) for part in PYTEST_VERSION.split(".")[:3]) ERROR = Severity.ERROR NOTE = Severity.NOTE WARNING = Severity.WARNING def call_pytest_collect_file(file_path: pathlib.Path, parent): return pytest_collect_file(file_path, parent) def test_create_mypy_assertion_error(): MypyAssertionError(None, []) def mk_dummy_parent(tmp_path: pathlib.Path, filename, content=""): path = tmp_path / filename path.write_text(content) config = Mock(spec=Config) config.rootdir = str(tmp_path) config.rootpath = str(tmp_path) config.getini.return_value = ["test_*.py", "*_test.py"] session = SimpleNamespace( config=config, isinitpath=lambda p: True, _initialpaths=[] ) parent = SimpleNamespace( config=config, session=session, nodeid="dummy", path=path, ) return parent @pytest.mark.parametrize("filename", ["z.py", "test_z.mypy-testing"]) def test_pytest_collect_file_not_test_file_name(tmp_path, filename: str): parent = mk_dummy_parent(tmp_path, filename) file_path = parent.path actual = call_pytest_collect_file(file_path, parent) assert actual is None @pytest.mark.parametrize("filename", ["test_z.py", "test_z.mypy-testing"]) def test_pytest_collect_file(tmp_path, filename): content = dedent( """ @pytest.mark.mypy_testing def foo(): pass """ ) parent = mk_dummy_parent(tmp_path, filename, content) expected = MypyTestFile( filename=str(parent.path), source_lines=content.splitlines() ) file_path = parent.path actual = call_pytest_collect_file(file_path, parent) assert isinstance(actual, PytestMypyFile) assert len(actual.mypy_file.items) == 1 actual.mypy_file.items = [] assert actual.mypy_file == expected pytest-mypy-testing-0.1.3/tests/test_strutil.py000066400000000000000000000012031457145253100217760ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 import pytest from pytest_mypy_testing.strutil import common_prefix, dedent @pytest.mark.parametrize( "a,b,expected", [ ("", "", ""), ("a", "a", "a"), ("abc", "abcd", "abc"), ("abcd", "abc", "abc"), ("abc", "xyz", ""), ], ) def test_common_prefix(a: str, b: str, expected: str): actual = common_prefix(a, b) assert actual == expected def test_dedent(): input = """ foo bar baz """ expected = "foo\nbar\n baz\n" actual = dedent(input) assert actual == expected pytest-mypy-testing-0.1.3/tox.ini000066400000000000000000000045221457145253100170370ustar00rootroot00000000000000# SPDX-FileCopyrightText: David Fritzsche # SPDX-License-Identifier: CC0-1.0 [tox] isolated_build = True envlist = py37-pytest{70,74}-mypy{10,14} {py38,py39,py310,py311,py312}-pytest{70,81}-mypy{10,18} py-pytest{70,81}-mypy{10,18} linting minversion = 3.28 [testenv] deps = -c constraints.in coverage[toml] pytest70: pytest~=7.0.1 pytest71: pytest~=7.1.3 pytest72: pytest~=7.2.2 pytest74: pytest~=7.4.4 pytest80: pytest~=8.0.2 pytest80: pytest~=8.0.2 pytest81: pytest==8.1.0 # to test with the yanked version mypy10: mypy==1.0.1 mypy14: mypy==1.4.1 # last version to support Python 3.7 mypy17: mypy==1.7.1 mypy18: mypy==1.8.0 setenv = COVERAGE_FILE={toxinidir}/build/{envname}/coverage commands = python -m coverage run --context "{envname}" -m pytest {posargs} --junitxml={toxinidir}/build/{envname}/junit.xml [testenv:black] basepython = python3.10 skip_install = True deps = -c constraints.txt black commands = python -m black --check --fast --diff {posargs} . [testenv:flake8] basepython = python3.10 skip_install = True deps = -c constraints.txt flake8 flake8-bugbear flake8-comprehensions flake8-html flake8-mutable flake8-pyi flake8-logging-format commands = python -m flake8 {posargs} [testenv:isort] basepython = python3.10 skip_install = True deps = -c constraints.txt isort commands = python -m isort . --check {posargs} [testenv:mypy] basepython = python3.10 skip_install = True deps = -cconstraints.txt mypy pytest commands = mypy src tests [testenv:linting] basepython = python3.10 skip_install = True deps = -cconstraints.txt {[testenv:black]deps} {[testenv:flake8]deps} {[testenv:isort]deps} {[testenv:mypy]deps} commands = {[testenv:black]commands} {[testenv:flake8]commands} {[testenv:isort]commands} {[testenv:mypy]commands} [testenv:lock-requirements] basepython = python3.10 skip_install = True deps = -cconstraints.txt pip-tools allowlist_externals = sh commands = sh ./lock-requirements.sh {posargs} [tox:.package] # note tox will use the same python version as under what tox is # installed to package so unless this is python 3 you can require a # given python version for the packaging environment via the # basepython key basepython = python3