Skip to content

Commit

Permalink
Support pypy (#36)
Browse files Browse the repository at this point in the history
* add basic support for pypy

* cleanup and remove non-existant pypy version

* tweak ci caching

* add warning to docs
  • Loading branch information
samuelcolvin authored Apr 28, 2022
1 parent f6e0d13 commit 2d3c0e5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ on:

jobs:
test:
name: test py${{ matrix.python-version }} on ${{ matrix.os }}
name: test py-${{ matrix.python-version }} on ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos]
python-version: ['3.7', '3.8', '3.9', '3.10']
python-version: ['3.7', '3.8', '3.9', '3.10', 'pypy-3.7', 'pypy-3.8', 'pypy-3.9']

runs-on: ${{ matrix.os }}-latest

Expand All @@ -39,10 +39,10 @@ jobs:
key: ${{ runner.os }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('tests/requirements.txt') }}

- run: pip install -r tests/requirements.txt
if: steps.cache.outputs.cache-hit != 'true'
# if: steps.cache.outputs.cache-hit != 'true' # breaks pypy tests

- run: poetry install
if: steps.cache.outputs.cache-hit != 'true'
# if: steps.cache.outputs.cache-hit != 'true' # breaks pypy tests

- run: make test

Expand Down
3 changes: 2 additions & 1 deletion dirty_equals/_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def _filter_dict(self, d: Dict[Any, Any]) -> Dict[Any, Any]:
return {k: v for k, v in d.items() if not self._ignore_value(v)}

def _ignore_value(self, v: Any) -> bool:
if isinstance(v, (DirtyEquals, DirtyEqualsMeta)):
# `isinstance(v, (DirtyEquals, DirtyEqualsMeta))` seems to always return `True` on pypy, no idea why
if type(v) in (DirtyEquals, DirtyEqualsMeta):
return False
elif callable(self.ignore):
return self.ignore(v)
Expand Down
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ assert ['a', 'b', 'c'] == ~Contains('z')

## Initialised vs. Class comparison

!!! warning

This does not work with PyPy.

*dirty-equals* allows comparison with types regardless of whether they've been initialised.

This saves users adding `()` in lots of places.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import platform

import pytest

from dirty_equals import Contains, IsApprox, IsInt, IsNegative, IsOneOf, IsPositive, IsStr
Expand Down Expand Up @@ -58,6 +60,7 @@ def test_dict_compare():
assert v == {'foo': IsInt() & IsApprox(1), 'bar': IsPositive() | IsNegative(), 'spam': ~IsStr()}


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not metaclass dunder methods')
def test_not_repr():
v = ~IsInt
assert str(v) == '~IsInt'
Expand All @@ -68,6 +71,16 @@ def test_not_repr():
assert str(v) == '~IsInt'


def test_not_repr_instance():
v = ~IsInt()
assert str(v) == '~IsInt()'

with pytest.raises(AssertionError):
assert 1 == v

assert str(v) == '~IsInt()'


def test_repr():
v = ~IsInt
assert str(v) == '~IsInt'
Expand Down
8 changes: 8 additions & 0 deletions tests/test_boolean.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import platform

import pytest

from dirty_equals import IsFalseLike, IsTrueLike
Expand Down Expand Up @@ -43,11 +45,17 @@ def test_is_false_like_repr():
assert repr(IsFalseLike(allow_strings=True)) == 'IsFalseLike(allow_strings=True)'


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not metaclass dunder methods')
def test_dirty_not_equals():
with pytest.raises(AssertionError):
assert 0 != IsFalseLike


def test_dirty_not_equals_instance():
with pytest.raises(AssertionError):
assert 0 != IsFalseLike()


def test_invalid_initialization():
with pytest.raises(TypeError, match='takes 1 positional argument but 2 were given'):
IsFalseLike(True)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib.util
import platform
import re
from pathlib import Path
from textwrap import dedent
Expand Down Expand Up @@ -65,5 +66,6 @@ def pytest_generate_tests(metafunc):
metafunc.parametrize('module_name,source_code', generate_code_chunks('dirty_equals', 'docs'))


@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not metaclass dunder methods')
def test_docs_examples(module_name, source_code, import_execute):
import_execute(module_name, source_code, True)
4 changes: 2 additions & 2 deletions tests/test_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def test_has_name(value, dirty):
'value,dirty',
[
(Foo(1, 2), HasAttributes(a=1, b=2)),
(Foo(1, 's'), HasAttributes(a=IsInt, b=IsStr)),
(Foo(1, 2), ~HasAttributes(a=IsInt, b=IsStr)),
(Foo(1, 's'), HasAttributes(a=IsInt(), b=IsStr())),
(Foo(1, 2), ~HasAttributes(a=IsInt(), b=IsStr())),
(Foo(1, 2), ~HasAttributes(a=1, b=2, c=3)),
(Foo(1, 2), HasAttributes(a=1, b=2, spam=AnyThing)),
(Foo(1, 2), ~HasAttributes(a=1, b=2, missing=AnyThing)),
Expand Down

0 comments on commit 2d3c0e5

Please sign in to comment.