pytest-check icon indicating copy to clipboard operation
pytest-check copied to clipboard

Make `_Comparable*` protocols compatible with builtins

Open SebastianGrans opened this issue 8 months ago • 2 comments

Hey!

Thanks for making this plugin! It's great!

There's one issue in the library that I found a fix for. It's not breaking anything, it just makes my static analyzer complain (pyright).

In particular, it's the comparison functions lessequal, etc.

Currently, the following statement

check.less_equal(1, 2)

results in the following pyright error:

file.py
  file.py:4:18 - error: Argument of type "Literal[1]" cannot be assigned to parameter "a" of type "_ComparableLessThanOrEqual" in function "less_equal"
    "Literal[1]" is incompatible with protocol "_ComparableLessThanOrEqual"
      "__le__" is an incompatible type
        Type "(value: int, /) -> bool" is not assignable to type "(other: Any) -> bool"
          Missing keyword parameter "other"
            Position-only parameter mismatch; parameter "other" is not position-only
            Position-only parameter mismatch; expected 1 but received 0 (reportArgumentType)
1 error, 0 warnings, 0 informations

The reason is that the comparison functions of the built-ins have the function prototype:

def __<op>__(self, value: <Type>, /) -> bool: ...

Meaning that the the built-ins don't match the protocol definition. If we adjust the protocol definitions to also include a backslash, E.g.:

def __gt__(self, other: Any, /) -> bool: ...

We no longer get the error.


Test results
py39: skipped because could not find python interpreter with spec(s): py39
py39: SKIP ⚠ in 0.01 seconds
py310: skipped because could not find python interpreter with spec(s): py310
py310: SKIP ⚠ in 0.01 seconds
py311: skipped because could not find python interpreter with spec(s): py311
py311: SKIP ⚠ in 0.01 seconds
.pkg: _optional_hooks> python /home/grans/.local/share/uv/tools/tox/lib/python3.12/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: get_requires_for_build_wheel> python /home/grans/.local/share/uv/tools/tox/lib/python3.12/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: build_wheel> python /home/grans/.local/share/uv/tools/tox/lib/python3.12/site-packages/pyproject_api/_backend.py True hatchling.build
py312: install_package> python -I -m pip install --force-reinstall --no-deps /home/grans/Projects/pytest-check/.tox/.tmp/package/10/pytest_check-2.5.3-py3-none-any.whl
py312: commands[0]> pytest
======================================================================================================================= test session starts =======================================================================================================================
platform linux -- Python 3.12.7, pytest-8.3.5, pluggy-1.5.0
cachedir: .tox/py312/.pytest_cache
rootdir: /home/grans/Projects/pytest-check
configfile: tox.ini
testpaths: tests
plugins: check-2.5.3
collected 69 items                                                                                                                                                                                                                                                

tests/test_alt_names.py .                                                                                                                                                                                                                                   [  1%]
tests/test_any_failures.py .....                                                                                                                                                                                                                            [  8%]
tests/test_check_and_assert.py .                                                                                                                                                                                                                            [ 10%]
tests/test_check_check.py .                                                                                                                                                                                                                                 [ 11%]
tests/test_check_context_manager.py ...                                                                                                                                                                                                                     [ 15%]
tests/test_check_fixture.py .                                                                                                                                                                                                                               [ 17%]
tests/test_check_func_decorator.py ..                                                                                                                                                                                                                       [ 20%]
tests/test_fail_func.py .                                                                                                                                                                                                                                   [ 21%]
tests/test_fail_in_fixture.py ..                                                                                                                                                                                                                            [ 24%]
tests/test_fail_in_teardown_with_skip.py .                                                                                                                                                                                                                  [ 26%]
tests/test_functions.py ..                                                                                                                                                                                                                                  [ 28%]
tests/test_helpers.py .                                                                                                                                                                                                                                     [ 30%]
tests/test_locals.py ..                                                                                                                                                                                                                                     [ 33%]
tests/test_logging.py ..                                                                                                                                                                                                                                    [ 36%]
tests/test_maxfail.py ...                                                                                                                                                                                                                                   [ 40%]
tests/test_message.py ..                                                                                                                                                                                                                                    [ 43%]
tests/test_not_in_test.py .                                                                                                                                                                                                                                 [ 44%]
tests/test_raises.py .........                                                                                                                                                                                                                              [ 57%]
tests/test_red.py ..                                                                                                                                                                                                                                        [ 60%]
tests/test_speedup_flags.py .....                                                                                                                                                                                                                           [ 68%]
tests/test_speedup_functions.py ......                                                                                                                                                                                                                      [ 76%]
tests/test_stop_on_fail.py ...                                                                                                                                                                                                                              [ 81%]
tests/test_summary.py ..                                                                                                                                                                                                                                    [ 84%]
tests/test_tb_style.py ..                                                                                                                                                                                                                                   [ 86%]
tests/test_thread.py ..                                                                                                                                                                                                                                     [ 89%]
tests/test_tracebackhide.py ..                                                                                                                                                                                                                              [ 92%]
tests/test_xfail.py .....                                                                                                                                                                                                                                   [100%]

======================================================================================================================= 69 passed in 1.13s ========================================================================================================================
py312: OK ✔ in 1.57 seconds
py313: install_package> python -I -m pip install --force-reinstall --no-deps /home/grans/Projects/pytest-check/.tox/.tmp/package/11/pytest_check-2.5.3-py3-none-any.whl
py313: commands[0]> pytest
======================================================================================================================= test session starts =======================================================================================================================
platform linux -- Python 3.13.0, pytest-8.3.5, pluggy-1.5.0
cachedir: .tox/py313/.pytest_cache
rootdir: /home/grans/Projects/pytest-check
configfile: tox.ini
testpaths: tests
plugins: check-2.5.3
collected 69 items                                                                                                                                                                                                                                                

tests/test_alt_names.py .                                                                                                                                                                                                                                   [  1%]
tests/test_any_failures.py .....                                                                                                                                                                                                                            [  8%]
tests/test_check_and_assert.py .                                                                                                                                                                                                                            [ 10%]
tests/test_check_check.py .                                                                                                                                                                                                                                 [ 11%]
tests/test_check_context_manager.py ...                                                                                                                                                                                                                     [ 15%]
tests/test_check_fixture.py .                                                                                                                                                                                                                               [ 17%]
tests/test_check_func_decorator.py ..                                                                                                                                                                                                                       [ 20%]
tests/test_fail_func.py .                                                                                                                                                                                                                                   [ 21%]
tests/test_fail_in_fixture.py ..                                                                                                                                                                                                                            [ 24%]
tests/test_fail_in_teardown_with_skip.py .                                                                                                                                                                                                                  [ 26%]
tests/test_functions.py ..                                                                                                                                                                                                                                  [ 28%]
tests/test_helpers.py .                                                                                                                                                                                                                                     [ 30%]
tests/test_locals.py ..                                                                                                                                                                                                                                     [ 33%]
tests/test_logging.py ..                                                                                                                                                                                                                                    [ 36%]
tests/test_maxfail.py ...                                                                                                                                                                                                                                   [ 40%]
tests/test_message.py ..                                                                                                                                                                                                                                    [ 43%]
tests/test_not_in_test.py .                                                                                                                                                                                                                                 [ 44%]
tests/test_raises.py .........                                                                                                                                                                                                                              [ 57%]
tests/test_red.py ..                                                                                                                                                                                                                                        [ 60%]
tests/test_speedup_flags.py .....                                                                                                                                                                                                                           [ 68%]
tests/test_speedup_functions.py ......                                                                                                                                                                                                                      [ 76%]
tests/test_stop_on_fail.py ...                                                                                                                                                                                                                              [ 81%]
tests/test_summary.py ..                                                                                                                                                                                                                                    [ 84%]
tests/test_tb_style.py ..                                                                                                                                                                                                                                   [ 86%]
tests/test_thread.py ..                                                                                                                                                                                                                                     [ 89%]
tests/test_tracebackhide.py ..                                                                                                                                                                                                                              [ 92%]
tests/test_xfail.py .....                                                                                                                                                                                                                                   [100%]

======================================================================================================================= 69 passed in 1.10s ========================================================================================================================
py313: OK ✔ in 1.45 seconds
py314: skipped because could not find python interpreter with spec(s): py314
py314: SKIP ⚠ in 0.01 seconds
pytest_earliest: skipped because could not find python interpreter with spec(s): python3.11
pytest_earliest: SKIP ⚠ in 0.01 seconds
coverage: install_package> python -I -m pip install --force-reinstall --no-deps /home/grans/Projects/pytest-check/.tox/.tmp/package/12/pytest_check-2.5.3-py3-none-any.whl
coverage: commands[0]> coverage run --source=/home/grans/Projects/pytest-check/.tox/coverage/lib/python3.13/site-packages/pytest_check,tests -m pytest
======================================================================================================================= test session starts =======================================================================================================================
platform linux -- Python 3.13.0, pytest-8.3.5, pluggy-1.5.0
cachedir: .tox/coverage/.pytest_cache
rootdir: /home/grans/Projects/pytest-check
configfile: tox.ini
testpaths: tests
plugins: check-2.5.3
collected 69 items                                                                                                                                                                                                                                                

tests/test_alt_names.py .                                                                                                                                                                                                                                   [  1%]
tests/test_any_failures.py .....                                                                                                                                                                                                                            [  8%]
tests/test_check_and_assert.py .                                                                                                                                                                                                                            [ 10%]
tests/test_check_check.py .                                                                                                                                                                                                                                 [ 11%]
tests/test_check_context_manager.py ...                                                                                                                                                                                                                     [ 15%]
tests/test_check_fixture.py .                                                                                                                                                                                                                               [ 17%]
tests/test_check_func_decorator.py ..                                                                                                                                                                                                                       [ 20%]
tests/test_fail_func.py .                                                                                                                                                                                                                                   [ 21%]
tests/test_fail_in_fixture.py ..                                                                                                                                                                                                                            [ 24%]
tests/test_fail_in_teardown_with_skip.py .                                                                                                                                                                                                                  [ 26%]
tests/test_functions.py ..                                                                                                                                                                                                                                  [ 28%]
tests/test_helpers.py .                                                                                                                                                                                                                                     [ 30%]
tests/test_locals.py ..                                                                                                                                                                                                                                     [ 33%]
tests/test_logging.py ..                                                                                                                                                                                                                                    [ 36%]
tests/test_maxfail.py ...                                                                                                                                                                                                                                   [ 40%]
tests/test_message.py ..                                                                                                                                                                                                                                    [ 43%]
tests/test_not_in_test.py .                                                                                                                                                                                                                                 [ 44%]
tests/test_raises.py .........                                                                                                                                                                                                                              [ 57%]
tests/test_red.py ..                                                                                                                                                                                                                                        [ 60%]
tests/test_speedup_flags.py .....                                                                                                                                                                                                                           [ 68%]
tests/test_speedup_functions.py ......                                                                                                                                                                                                                      [ 76%]
tests/test_stop_on_fail.py ...                                                                                                                                                                                                                              [ 81%]
tests/test_summary.py ..                                                                                                                                                                                                                                    [ 84%]
tests/test_tb_style.py ..                                                                                                                                                                                                                                   [ 86%]
tests/test_thread.py ..                                                                                                                                                                                                                                     [ 89%]
tests/test_tracebackhide.py ..                                                                                                                                                                                                                              [ 92%]
tests/test_xfail.py .....                                                                                                                                                                                                                                   [100%]

======================================================================================================================= 69 passed in 2.28s ========================================================================================================================
coverage: commands[1]> coverage report --fail-under=100 --show-missing
Name                                                                          Stmts   Miss Branch BrPart  Cover   Missing
-------------------------------------------------------------------------------------------------------------------------
.tox/coverage/lib/python3.13/site-packages/pytest_check/__init__.py              12      0      2      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/check_functions.py      174      0     54      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/check_log.py             49      0     14      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/check_raises.py          32      0      8      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/context_manager.py       42      0      6      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/plugin.py                65      0      6      0   100%
.tox/coverage/lib/python3.13/site-packages/pytest_check/pseudo_traceback.py      66      0     30      0   100%
tests/__init__.py                                                                 0      0      0      0   100%
tests/conftest.py                                                                 1      0      0      0   100%
tests/test_alt_names.py                                                           7      0      0      0   100%
tests/test_any_failures.py                                                       17      0      0      0   100%
tests/test_check_and_assert.py                                                    5      0      0      0   100%
tests/test_check_check.py                                                         4      0      0      0   100%
tests/test_check_context_manager.py                                              14      0      0      0   100%
tests/test_check_fixture.py                                                       2      0      0      0   100%
tests/test_check_func_decorator.py                                                9      0      0      0   100%
tests/test_fail_func.py                                                           5      0      0      0   100%
tests/test_fail_in_fixture.py                                                    10      0      0      0   100%
tests/test_fail_in_teardown_with_skip.py                                          4      0      0      0   100%
tests/test_functions.py                                                           8      0      0      0   100%
tests/test_helpers.py                                                             8      0      0      0   100%
tests/test_locals.py                                                             10      0      0      0   100%
tests/test_logging.py                                                             9      0      0      0   100%
tests/test_maxfail.py                                                            14      0      0      0   100%
tests/test_message.py                                                             9      0      0      0   100%
tests/test_not_in_test.py                                                         8      0      0      0   100%
tests/test_raises.py                                                             63      0      0      0   100%
tests/test_red.py                                                                12      0      0      0   100%
tests/test_speedup_flags.py                                                      29      0      0      0   100%
tests/test_speedup_functions.py                                                  34      0      0      0   100%
tests/test_stop_on_fail.py                                                       17      0      0      0   100%
tests/test_summary.py                                                            12      0      0      0   100%
tests/test_tb_style.py                                                           15      0      0      0   100%
tests/test_thread.py                                                             10      0      0      0   100%
tests/test_tracebackhide.py                                                      14      0      0      0   100%
tests/test_xfail.py                                                              25      0      0      0   100%
-------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                           815      0    120      0   100%
coverage: OK ✔ in 2.8 seconds
lint: commands[0]> ruff check src tests examples
All checks passed!
lint: OK ✔ in 0.01 seconds
mypy: install_package> python -I -m pip install --force-reinstall --no-deps /home/grans/Projects/pytest-check/.tox/.tmp/package/13/pytest_check-2.5.3-py3-none-any.whl
mypy: commands[0]> mypy --strict --pretty src
Success: no issues found in 7 source files
mypy: commands[1]> mypy --pretty tests
Success: no issues found in 29 source files
mypy: OK ✔ in 3.9 seconds
mypy_earliest: skipped because could not find python interpreter with spec(s): python3.9
.pkg: _exit> python /home/grans/.local/share/uv/tools/tox/lib/python3.12/site-packages/pyproject_api/_backend.py True hatchling.build
  py39: SKIP (0.01 seconds)
  py310: SKIP (0.01 seconds)
  py311: SKIP (0.01 seconds)
  py312: OK (1.57=setup[0.34]+cmd[1.23] seconds)
  py313: OK (1.45=setup[0.24]+cmd[1.21] seconds)
  py314: SKIP (0.01 seconds)
  pytest_earliest: SKIP (0.01 seconds)
  coverage: OK (2.80=setup[0.24]+cmd[2.46,0.10] seconds)
  lint: OK (0.01=setup[0.00]+cmd[0.01] seconds)
  mypy: OK (3.90=setup[0.24]+cmd[1.82,1.84] seconds)
  mypy_earliest: SKIP (0.02 seconds)
  congratulations :) (9.84 seconds)

SebastianGrans avatar Apr 14 '25 15:04 SebastianGrans

@okken Friendly ping! Not sure if you receive any notifications from this :)

SebastianGrans avatar Apr 28 '25 14:04 SebastianGrans

Thanks for the ping. Will bump this up in priority

okken avatar Apr 29 '25 15:04 okken