asserts
Advanced tools
+48
-20
@@ -23,2 +23,4 @@ """ | ||
| from __future__ import annotations | ||
| import re | ||
@@ -28,6 +30,8 @@ import sys | ||
| from json import loads as json_loads | ||
| from typing import Set | ||
| from warnings import catch_warnings | ||
| from typing import Any, Callable, Set | ||
| from warnings import WarningMessage, catch_warnings | ||
| from typing_extensions import deprecated | ||
| def fail(msg=None): | ||
@@ -869,4 +873,3 @@ """Raise an AssertionError with the given message. | ||
| class AssertRaisesContext(object): | ||
| class AssertRaisesContext: | ||
| """A context manager to test for exceptions with certain properties. | ||
@@ -913,3 +916,3 @@ | ||
| self._exception_name = getattr(exception, "__name__", str(exception)) | ||
| self._tests = [] | ||
| self._tests: list[Callable[[Any], object]] = [] | ||
@@ -937,3 +940,3 @@ def __enter__(self): | ||
| def add_test(self, cb): | ||
| def add_test(self, cb: Callable[[Any], object]) -> None: | ||
| """Add a test callback. | ||
@@ -956,3 +959,2 @@ | ||
| class AssertRaisesRegexContext(AssertRaisesContext): | ||
| """A context manager to test for exceptions and their messages.""" | ||
@@ -975,3 +977,2 @@ | ||
| class AssertRaisesErrnoContext(AssertRaisesContext): | ||
| """A context manager to test for exceptions with errnos.""" | ||
@@ -1171,3 +1172,2 @@ | ||
| class AssertWarnsContext(object): | ||
| """A context manager to test for warnings with certain properties. | ||
@@ -1203,5 +1203,7 @@ | ||
| self._msg_fmt = msg_fmt | ||
| self._warning_context = None | ||
| self._warning_context: catch_warnings[list[WarningMessage]] | None = ( | ||
| None | ||
| ) | ||
| self._warnings = [] | ||
| self._tests = [] | ||
| self._tests: list[Callable[[Warning], bool]] = [] | ||
@@ -1214,2 +1216,3 @@ def __enter__(self): | ||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| assert self._warning_context is not None | ||
| self._warning_context.__exit__(exc_type, exc_val, exc_tb) | ||
@@ -1227,3 +1230,3 @@ if not any(self._is_expected_warning(w) for w in self._warnings): | ||
| def _is_expected_warning(self, warning): | ||
| def _is_expected_warning(self, warning) -> bool: | ||
| if not issubclass(warning.category, self._warning_class): | ||
@@ -1233,3 +1236,3 @@ return False | ||
| def add_test(self, cb): | ||
| def add_test(self, cb: Callable[[Warning], bool]) -> None: | ||
| """Add a test callback. | ||
@@ -1246,3 +1249,2 @@ | ||
| class AssertWarnsRegexContext(AssertWarnsContext): | ||
| """A context manager to test for warnings and their messages.""" | ||
@@ -1370,4 +1372,4 @@ | ||
| In objects, the special name `Exists` can be used to check for the | ||
| existence or non-existence of a specific key: | ||
| In objects, the special classes `Present` and `Absent` can be used to | ||
| check for the presence or absence of a specific key: | ||
@@ -1418,2 +1420,4 @@ >>> assert_json_subset({Exists("foo"): True}, '{"foo": "bar"}') | ||
| self._assert_arrays_equal() | ||
| elif _is_present(self._expected): | ||
| pass | ||
| else: | ||
@@ -1431,2 +1435,4 @@ self._assert_fundamental_values_equal() | ||
| return not isinstance(self._actual, (int, float)) | ||
| elif _is_present(self._expected): | ||
| return False | ||
| for type_ in [bool, str, _Str, list, dict]: | ||
@@ -1455,7 +1461,11 @@ if isinstance(self._expected, type_): | ||
| for name in self._expected: | ||
| if isinstance(name, str) and _is_absent(self._expected[name]): | ||
| if name in self._actual: | ||
| self._raise_assertion_error( | ||
| f"spurious member '{name}' in object {{path}}" | ||
| ) | ||
| if isinstance(name, Exists) and not self._expected[name]: | ||
| if name.member_name in self._actual: | ||
| self._raise_assertion_error( | ||
| f"spurious member '{name.member_name}' in " | ||
| f"object {{path}}" | ||
| f"spurious member '{name.member_name}' in object {{path}}" | ||
| ) | ||
@@ -1465,3 +1475,3 @@ | ||
| for name in self._expected: | ||
| if isinstance(name, str): | ||
| if isinstance(name, str) and not _is_absent(self._expected[name]): | ||
| self._assert_json_value_equals_with_item(name) | ||
@@ -1474,3 +1484,4 @@ | ||
| if isinstance(k, str): | ||
| keys.add(k) | ||
| if not _is_absent(self._expected[k]): | ||
| keys.add(k) | ||
| elif isinstance(k, Exists) and self._expected[k]: | ||
@@ -1545,2 +1556,19 @@ keys.add(k.member_name) | ||
| class Present: | ||
| """Helper class for presence checks in assert_json_subset().""" | ||
| def _is_present(o: object) -> bool: | ||
| return o is Present or isinstance(o, Present) | ||
| class Absent: | ||
| """Helper class for absence checks in assert_json_subset().""" | ||
| def _is_absent(o: object) -> bool: | ||
| return o is Absent or isinstance(o, Absent) | ||
| @deprecated("Use Present and Absent instead.") | ||
| class Exists: | ||
@@ -1547,0 +1575,0 @@ """Helper class for existence checks in assert_json_subset().""" |
@@ -8,2 +8,4 @@ import datetime | ||
| from typing_extensions import deprecated | ||
| _E = TypeVar("_E", bound=BaseException) | ||
@@ -167,4 +169,8 @@ _S = TypeVar("_S") | ||
| class Present: ... | ||
| class Absent: ... | ||
| @deprecated("Use Present and Absent instead.") | ||
| class Exists: | ||
| member_name: str | ||
| def __init__(self, member_name: str) -> None: ... |
+19
-16
| Metadata-Version: 2.1 | ||
| Name: asserts | ||
| Version: 0.12.0 | ||
| Version: 0.13.0 | ||
| Summary: Stand-alone Assertions | ||
@@ -9,3 +9,3 @@ Home-page: https://github.com/srittau/python-asserts | ||
| Author-email: srittau@rittau.biz | ||
| Requires-Python: >=3.7 | ||
| Requires-Python: >=3.8.1 | ||
| Classifier: Development Status :: 3 - Alpha | ||
@@ -15,13 +15,16 @@ Classifier: Intended Audience :: Developers | ||
| Classifier: Programming Language :: Python :: 3 | ||
| Classifier: Programming Language :: Python :: 3.7 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Classifier: Programming Language :: Python :: 3.12 | ||
| Classifier: Topic :: Software Development :: Quality Assurance | ||
| Classifier: Topic :: Software Development :: Testing | ||
| Requires-Dist: typing-extensions (>=4.10.0,<5.0.0) | ||
| Project-URL: Bug Tracker, https://github.com/srittau/python-asserts/issues | ||
| Project-URL: Changes, https://github.com/srittau/python-asserts/blob/main/CHANGELOG.md | ||
| Project-URL: GitHub, https://github.com/srittau/python-asserts | ||
| Project-URL: Repository, https://github.com/srittau/python-asserts | ||
| Description-Content-Type: text/markdown | ||
| Python Asserts | ||
| ============== | ||
| # Python Asserts | ||
@@ -32,3 +35,3 @@ [](https://pypi.python.org/pypi/asserts/) | ||
| [](https://pypi.python.org/pypi/asserts/) | ||
| [](https://travis-ci.org/srittau/python-asserts) | ||
| [](https://github.com/srittau/python-asserts/actions/workflows/test-and-lint.yml) | ||
@@ -40,9 +43,9 @@ Stand-alone Assertions for Python | ||
| * Can be used stand-alone, for example: | ||
| * In test cases, not derived from TestCase. | ||
| * In fake and mock classes. | ||
| * In implementations as rich alternative to the assert statement. | ||
| * PEP 8 compliance. | ||
| * Custom stand-alone assertions can be written easily. | ||
| * Arguably a better separation of concerns, since TestCase is responsible | ||
| - Can be used stand-alone, for example: | ||
| - In test cases, not derived from TestCase. | ||
| - In fake and mock classes. | ||
| - In implementations as rich alternative to the assert statement. | ||
| - PEP 8 compliance. | ||
| - Custom stand-alone assertions can be written easily. | ||
| - Arguably a better separation of concerns, since TestCase is responsible | ||
| for test running only, if assertion functions are used exclusively. | ||
@@ -52,5 +55,5 @@ | ||
| * The default assertion class (`AssertionError`) can not be overwritten. This | ||
| - The default assertion class (`AssertionError`) can not be overwritten. This | ||
| is rarely a problem in practice. | ||
| * asserts does not support the `addTypeEqualityFunc()` functionality. | ||
| - asserts does not support the `addTypeEqualityFunc()` functionality. | ||
@@ -57,0 +60,0 @@ Usage: |
+19
-6
| [tool.poetry] | ||
| name = "asserts" | ||
| version = "0.12.0" | ||
| version = "0.13.0" | ||
| description = "Stand-alone Assertions" | ||
@@ -19,15 +19,28 @@ readme = "README.md" | ||
| [tool.poetry.urls] | ||
| "GitHub" = "https://github.com/srittau/python-asserts" | ||
| "Bug Tracker" = "https://github.com/srittau/python-asserts/issues" | ||
| "Changes" = "https://github.com/srittau/python-asserts/blob/main/CHANGELOG.md" | ||
| [tool.poetry.dependencies] | ||
| python = ">=3.7" | ||
| python = ">=3.8.1" | ||
| typing-extensions = "^4.10.0" | ||
| [tool.poetry.group.dev.dependencies] | ||
| black = "^22.10.0" | ||
| flake8 = "~5.0.4" | ||
| mypy = "0.982" | ||
| mypy = "~1.9.0" | ||
| poethepoet = "^0.25.0" | ||
| ruff = "^0.3.2" | ||
| [tool.black] | ||
| [tool.ruff] | ||
| line-length = 79 | ||
| target-version = "py38" | ||
| [tool.poe.tasks] | ||
| test = "python3 -Wall -m unittest test_asserts" | ||
| doctest = "python3 -m doctest asserts/__init__.py" | ||
| lint = "ruff check asserts test_asserts.py" | ||
| typecheck = "mypy asserts test_asserts.py" | ||
| [build-system] | ||
| requires = ["poetry-core>=1.0.0"] | ||
| build-backend = "poetry.core.masonry.api" |
+11
-12
@@ -1,3 +0,2 @@ | ||
| Python Asserts | ||
| ============== | ||
| # Python Asserts | ||
@@ -8,3 +7,3 @@ [](https://pypi.python.org/pypi/asserts/) | ||
| [](https://pypi.python.org/pypi/asserts/) | ||
| [](https://travis-ci.org/srittau/python-asserts) | ||
| [](https://github.com/srittau/python-asserts/actions/workflows/test-and-lint.yml) | ||
@@ -16,9 +15,9 @@ Stand-alone Assertions for Python | ||
| * Can be used stand-alone, for example: | ||
| * In test cases, not derived from TestCase. | ||
| * In fake and mock classes. | ||
| * In implementations as rich alternative to the assert statement. | ||
| * PEP 8 compliance. | ||
| * Custom stand-alone assertions can be written easily. | ||
| * Arguably a better separation of concerns, since TestCase is responsible | ||
| - Can be used stand-alone, for example: | ||
| - In test cases, not derived from TestCase. | ||
| - In fake and mock classes. | ||
| - In implementations as rich alternative to the assert statement. | ||
| - PEP 8 compliance. | ||
| - Custom stand-alone assertions can be written easily. | ||
| - Arguably a better separation of concerns, since TestCase is responsible | ||
| for test running only, if assertion functions are used exclusively. | ||
@@ -28,5 +27,5 @@ | ||
| * The default assertion class (`AssertionError`) can not be overwritten. This | ||
| - The default assertion class (`AssertionError`) can not be overwritten. This | ||
| is rarely a problem in practice. | ||
| * asserts does not support the `addTypeEqualityFunc()` functionality. | ||
| - asserts does not support the `addTypeEqualityFunc()` functionality. | ||
@@ -33,0 +32,0 @@ Usage: |
-26
| # -*- coding: utf-8 -*- | ||
| from setuptools import setup | ||
| packages = \ | ||
| ['asserts'] | ||
| package_data = \ | ||
| {'': ['*']} | ||
| setup_kwargs = { | ||
| 'name': 'asserts', | ||
| 'version': '0.12.0', | ||
| 'description': 'Stand-alone Assertions', | ||
| 'long_description': 'Python Asserts\n==============\n\n[](https://pypi.python.org/pypi/asserts/)\n[](https://pypi.python.org/pypi/asserts/)\n[](https://github.com/srittau/python-asserts/releases/)\n[](https://pypi.python.org/pypi/asserts/)\n[](https://travis-ci.org/srittau/python-asserts)\n\nStand-alone Assertions for Python\n\nThis package provides a few advantages over the assertions provided by\nunittest.TestCase:\n\n* Can be used stand-alone, for example:\n * In test cases, not derived from TestCase.\n * In fake and mock classes.\n * In implementations as rich alternative to the assert statement.\n* PEP 8 compliance.\n* Custom stand-alone assertions can be written easily.\n* Arguably a better separation of concerns, since TestCase is responsible\n for test running only, if assertion functions are used exclusively.\n\nThere are a few regressions compared to assertions from TestCase:\n\n* The default assertion class (`AssertionError`) can not be overwritten. This\n is rarely a problem in practice.\n* asserts does not support the `addTypeEqualityFunc()` functionality.\n\nUsage:\n\n```python\n>>> from asserts import assert_true, assert_equal, assert_raises\n>>> my_var = 13\n>>> assert_equal(13, my_var)\n>>> assert_true(True, msg="custom failure message")\n>>> with assert_raises(KeyError):\n... raise KeyError()\n```\n\nFailure messages can be customized:\n\n```python\n>>> assert_equal(13, 14, msg_fmt="{got} is wrong, expected {expected}")\nTraceback (most recent call last):\n ...\nAssertionError: 14 is wrong, expected 13\n```\n', | ||
| 'author': 'Sebastian Rittau', | ||
| 'author_email': 'srittau@rittau.biz', | ||
| 'maintainer': 'None', | ||
| 'maintainer_email': 'None', | ||
| 'url': 'https://github.com/srittau/python-asserts', | ||
| 'packages': packages, | ||
| 'package_data': package_data, | ||
| 'python_requires': '>=3.7', | ||
| } | ||
| setup(**setup_kwargs) |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
63740
-0.39%7
-12.5%