-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 10ecfd1
Showing
13 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Tox | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] | ||
name: Python ${{ matrix.python-version }} tests | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Setup python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
architecture: x64 | ||
- name: Install dependencies | ||
run: pip install -r requirements-dev.txt | ||
- name: Run tests and type checking | ||
run: tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.vscode | ||
venv* | ||
*.pyc | ||
*.egg-info | ||
build | ||
dist | ||
.tox | ||
.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Tushar Sadhwani | ||
|
||
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 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# pytest-beartype | ||
|
||
Pytest plugin to run your tests with beartype checking enabled. | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install pytest_beartype | ||
``` | ||
|
||
## Usage | ||
|
||
```bash | ||
pytest --beartype-packages='your_package_name,other_package_name' | ||
``` | ||
|
||
This will ensure that any type annotations in those packages are checked at | ||
runtime, while your tests are running. | ||
|
||
## Local Development / Testing | ||
|
||
- Create and activate a virtual environment | ||
- Run `pip install -r requirements-dev.txt` to do an editable install | ||
- Run `pytest` to run tests | ||
|
||
## Type Checking | ||
|
||
Run `mypy .` | ||
|
||
## Create and upload a package to PyPI | ||
|
||
Make sure to bump the version in `setup.cfg`. | ||
|
||
Then run the following commands: | ||
|
||
```bash | ||
rm -rf build dist | ||
python setup.py sdist bdist_wheel | ||
``` | ||
|
||
Then upload it to PyPI using [twine](https://twine.readthedocs.io/en/latest/#installation): | ||
|
||
```bash | ||
twine upload dist/* | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[mypy] | ||
strict = True | ||
exclude = setup.py|venv|build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-e .[dev] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
[metadata] | ||
name = pytest-beartype | ||
version = 0.0.1 | ||
description = Pytest plugin to run your tests with beartype checking enabled. | ||
long_description = file: README.md | ||
long_description_content_type = text/markdown | ||
url = https://github.com/tusharsadhwani/pytest-beartype | ||
author = Tushar Sadhwani | ||
author_email = [email protected] | ||
license = MIT | ||
license_file = LICENSE | ||
classifiers = | ||
Framework :: Pytest | ||
License :: OSI Approved :: MIT License | ||
Operating System :: OS Independent | ||
Programming Language :: Python :: 3 | ||
Programming Language :: Python :: 3 :: Only | ||
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 | ||
Programming Language :: Python :: Implementation :: CPython | ||
Typing :: Typed | ||
|
||
[options] | ||
packages = find: | ||
install_requires = | ||
pytest | ||
beartype | ||
python_requires = >=3.8 | ||
package_dir = =src | ||
|
||
[options.packages.find] | ||
where = ./src | ||
|
||
[options.entry_points] | ||
pytest11 = | ||
pytest_beartype = pytest_beartype | ||
|
||
[options.extras_require] | ||
dev = | ||
black | ||
mypy | ||
pytest-cov | ||
tox | ||
|
||
[options.package_data] | ||
pytest-beartype = | ||
py.typed | ||
|
||
[tool:pytest] | ||
addopts = --cov --cov-report=term-missing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from setuptools import setup | ||
|
||
setup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""pytest-beartype - Pytest plugin to run your tests with beartype checking enabled.""" | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
|
||
def pytest_addoption(parser: "pytest.Parser") -> None: | ||
# Add beartype-specific "pytest" options exposed by this plugin. | ||
group = parser.getgroup("beartype") | ||
group.addoption( | ||
"--beartype-packages", | ||
action="store", | ||
help=( | ||
"comma-delimited list of the fully-qualified names of " | ||
"all packages and modules to type-check with beartype" | ||
), | ||
) | ||
|
||
|
||
def pytest_configure(config: "pytest.Config") -> None: | ||
# Comma-delimited string listing the fully-qualified names of *ALL* packages | ||
# and modules to type-check with beartype, corresponding to the | ||
# "--beartype-packages" option defined above by the pytest_addoption() hook. | ||
package_names_str = config.getoption("beartype_packages") | ||
|
||
# If the user passed this option... | ||
if package_names_str: | ||
# Defer hook-specific imports. To improve "pytest" startup performance, | ||
# avoid performing *ANY* imports unless the user actually passed the | ||
# "--beartype-packages" option declared by this plugin. | ||
from beartype.roar import BeartypeWarning | ||
from beartype.claw import beartype_packages | ||
from beartype._util.text.utiltextjoin import join_delimited | ||
from sys import modules | ||
from warnings import warn | ||
|
||
class BeartypePytestWarning(BeartypeWarning): | ||
""" | ||
Beartype :mod:`pytest` warning. | ||
This warning is emitted at :mod:`pytest` configuration time when one or | ||
more packages or modules to be type-checked have already been imported | ||
under the active Python interpreter. | ||
""" | ||
|
||
# Tuple of the fully-qualified names of these packages and modules. | ||
package_names = tuple( | ||
package_name_str.strip() | ||
for package_name_str in package_names_str.split(",") | ||
) | ||
|
||
# Tuple of the subset of these names corresponding to previously | ||
# imported packages and modules under the active Python interpreter. | ||
package_imported_names = tuple( | ||
package_name for package_name in package_names if package_name in modules | ||
) | ||
|
||
# If at least one of these packages or modules has already been | ||
# imported... | ||
if package_imported_names: | ||
# Comma-delimited double-quoted string listing these packages. Yeah! | ||
package_imported_names_str = join_delimited( | ||
strs=package_imported_names, | ||
delimiter_if_two=" and ", | ||
delimiter_if_three_or_more_nonlast=", ", | ||
delimiter_if_three_or_more_last=", and ", | ||
is_double_quoted=True, | ||
) | ||
|
||
# Emit a non-fatal warning informing the user. | ||
warn( | ||
( | ||
f"Previously imported packages " | ||
f"{package_imported_names_str} not checkable by beartype." | ||
), | ||
BeartypePytestWarning, | ||
stacklevel=1, # <-- dark magic glistens dangerously | ||
) | ||
|
||
# Install an import hook type-checking these packages and modules. | ||
beartype_packages(package_names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Marker file for PEP 561. This package uses inline types. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from unittest import mock | ||
import pytest | ||
|
||
import pytest_beartype | ||
|
||
|
||
def test_pytest_addoption() -> None: | ||
""" | ||
Assert that the "pytest_addoption" hook declared by this plugin adds the | ||
"--beartype-packages" option to the "beartype" group. | ||
""" | ||
with mock.patch.object(pytest.OptionGroup, "addoption") as mock_addoption: | ||
pytest_beartype.pytest_addoption(pytest.Parser()) | ||
|
||
mock_addoption.assert_called_once_with( | ||
"--beartype-packages", | ||
action="store", | ||
help=mock.ANY, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[tox] | ||
envlist = py38,py39,py310,py311,py312,py312-type | ||
skip_missing_interpreters = true | ||
|
||
[testenv] | ||
deps = -rrequirements-dev.txt | ||
commands = pytest | ||
|
||
[testenv:py312-type] | ||
description = Type check with mypy | ||
commands = | ||
mypy . |