Skip to content

Commit

Permalink
Remove defunct numpy aliases (#1)
Browse files Browse the repository at this point in the history
* Remove defunct numpy aliases

* Relax numpy requirements

* Remove defunct numpy aliases from tests

Use pytest instead of nose as it is now unmaintained

* Update yield tests to use pytest.mark.parametrize

* Relax pyparsing requirement
  • Loading branch information
chowder authored Aug 10, 2023
1 parent 28f8c0a commit a3c3f99
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 66 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pyparsing==2.2.2
pyparsing
decorator==4.0.10
six==1.10.0
future
numpy<1.16
numpy
3 changes: 0 additions & 3 deletions src/contracts/library/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def parse(s, loc, tokens):


np_types = {
'np_int': np.int, # Platform integer (normally either int32 or int64)
'np_int8': np.int8, # Byte (-128 to 127)
'np_int16': np.int16, # Integer (-32768 to 32767)
'np_int32': np.int32, # Integer (-2147483648 to 2147483647)
Expand All @@ -231,11 +230,9 @@ def parse(s, loc, tokens):
'np_uint16': np.uint16, # Unsigned integer (0 to 65535)
'np_uint32': np.uint32, # Unsigned integer (0 to 4294967295)
'np_uint64': np.uint64, # Unsigned integer (0 to 18446744073709551615)
'np_float': np.float, # Shorthand for float64.
'np_float16': np.float16, # Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
'np_float32': np.float32, # Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
'np_float64': np.float64, # Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
'np_complex': np.complex, # Shorthand for complex128.
'np_complex64': np.complex64, # Complex number, represented by two 32-bit floats (real and imaginary components)
'np_complex128': np.complex128}

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/testing/library/array_tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
good('finite', 1)
good('finite', 0)
good('finite', -1)
good('finite', np.float(1))
good('finite', 1.0)
fail('finite', np.inf)
fail('finite', np.nan)

Expand Down
6 changes: 3 additions & 3 deletions src/contracts/testing/library/test_scoped_variables.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest
from contracts import ContractException, check, contract, decorate, fail, parse
from contracts.interface import (ContractNotRespected,
ExternalScopedVariableNotFound)
from contracts.library.simple_values import EqualTo
from contracts.library.types_misc import CheckType
from contracts.utils import check_isinstance
from nose.tools import raises


def test_raw_parse():
Expand Down Expand Up @@ -42,9 +42,9 @@ def test_algebra():
assert c.length_contract.rvalue.value == 2


@raises(ContractException)
def test_invalid():
parse('$not_found')
with pytest.raises(ContractException):
parse('$not_found')


def test_check():
Expand Down
19 changes: 4 additions & 15 deletions src/contracts/testing/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
from abc import abstractmethod
import functools
import nose
import pytest
import unittest

from contracts import ContractNotRespected, contract, ContractsMeta
from contracts import CannotDecorateClassmethods
from contracts import with_metaclass

def expected_failure(test):
@functools.wraps(test)
def inner(*args, **kwargs):
try:
test(*args, **kwargs)
except Exception:
raise nose.SkipTest
else:
raise AssertionError('Failure expected')
return inner


class TestMeta(unittest.TestCase):

Expand Down Expand Up @@ -81,7 +70,7 @@ def g(self, a):
self.assertRaises(ContractNotRespected, b.f, 0)
self.assertRaises(ContractNotRespected, b.g, 0)

@expected_failure
@pytest.mark.xfail(raises=Exception)
def test_static1(self):

class A(with_metaclass(ContractsMeta, object)):
Expand All @@ -101,7 +90,7 @@ def f(a):

self.assertRaises(ContractNotRespected, B.f, 0) # this doesn't work

@expected_failure
@pytest.mark.xfail(raises=Exception)
def test_classmethod1(self):

class A(with_metaclass(ContractsMeta, object)):
Expand All @@ -123,7 +112,7 @@ def f(cls, a):

self.assertRaises(ContractNotRespected, B.f, 0) # this doesn't work

@expected_failure
@pytest.mark.xfail(raises=Exception)
def test_classmethod1ns(self):

class A(with_metaclass(ContractsMeta, object)):
Expand Down
11 changes: 5 additions & 6 deletions src/contracts/testing/test_new_contract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import pytest

from contracts import new_contract, check, Contract, contract
from contracts.library.extensions import identifier_expression
Expand Down Expand Up @@ -341,9 +342,7 @@ def check_valid_identifier(e):
check(e, 42)


def test_valid_identifiers():

for e in examples_valid:
yield check_valid_identifier, e


class TestValidIdentifiers():
@pytest.mark.parametrize("identifier", examples_valid)
def test_example(self, identifier):
check_valid_identifier(identifier)
35 changes: 21 additions & 14 deletions src/contracts/testing/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from contracts.test_registrar import (semantic_fail_examples,
contract_fail_examples, good_examples)
import pickle
import pytest


def check_exception_pickable(contract, value):
Expand All @@ -20,12 +21,14 @@ def check_exception_pickable(contract, value):
# raise Exception(msg)


def test_exceptions_are_pickable():
for contract, value, exact in semantic_fail_examples: # @UnusedVariable
yield check_contracts_fail, contract, value, ContractNotRespected
#ContractSemanticError
for contract, value, exact in contract_fail_examples: # @UnusedVariable
yield check_contracts_fail, contract, value, ContractNotRespected
class TestExceptionsArePickable():
@pytest.mark.parametrize("contract,value,exact", semantic_fail_examples)
def test_semantic_fail_examples(self, contract, value, exact):
check_contracts_fail(contract, value, ContractNotRespected)

@pytest.mark.parametrize("contract,value,exact", contract_fail_examples)
def test_contract_fail_examples(self, contract, value, exact):
check_contracts_fail(contract, value, ContractNotRespected)


def check_contract_pickable(contract):
Expand All @@ -44,11 +47,15 @@ def check_contract_pickable(contract):
assert c == c2


def test_contracts_are_pickable():
allc = (good_examples + semantic_fail_examples + contract_fail_examples)
for contract, _, _ in allc:
if isinstance(contract, list):
for c in contract:
yield check_contract_pickable, c
else:
yield check_contract_pickable, contract
contracts = []
allc = (good_examples + semantic_fail_examples + contract_fail_examples)
for contract, _, _ in allc:
if isinstance(contract, list):
contracts.extend(contract)
else:
contracts.append(contract)


@pytest.mark.parametrize("contract", contracts)
def test_contract_is_pickable(contract):
check_contract_pickable(contract)
58 changes: 36 additions & 22 deletions src/contracts/testing/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traceback
import pytest

from ..main import parse_contract_string
from ..test_registrar import (good_examples, semantic_fail_examples,
Expand All @@ -23,47 +24,60 @@
#from ..test_registrar import fail, good, syntax_fail, semantic_fail


def test_good():
for contract, value, exact in good_examples: # @UnusedVariable
yield check_contracts_ok, contract, value
@pytest.mark.parametrize("contract,value,exact", good_examples)
def test_good(contract, value, exact):
check_contracts_ok(contract, value)


def test_syntax_fail():
for s in syntax_fail_examples:
yield check_syntax_fail, s
@pytest.mark.parametrize("s", syntax_fail_examples)
def test_syntax_fail(s):
check_syntax_fail(s)


def test_semantic_fail():
for contract, value, exact in semantic_fail_examples: # @UnusedVariable
yield check_contracts_fail, contract, value, ContractNotRespected
@pytest.mark.parametrize("contract,value,exact", semantic_fail_examples)
def test_semantic_fail(contract, value, exact):
check_contracts_fail(contract, value, ContractNotRespected)


def test_contract_fail():
for contract, value, exact in contract_fail_examples: # @UnusedVariable
yield check_contracts_fail, contract, value, ContractNotRespected
@pytest.mark.parametrize("contract,value,exact", contract_fail_examples)
def test_contract_fail(contract, value, exact):
check_contracts_fail(contract, value, ContractNotRespected)


# Checks that we can eval() the __repr__() value and
# we get an equivalent object.
def test_repr():
def get_repr_test_cases():
test_cases = []
allc = (good_examples + semantic_fail_examples + contract_fail_examples)
for contract, value, exact in allc: # @UnusedVariable
if isinstance(contract, list):
for c in contract:
yield check_good_repr, c
test_cases.extend(contract)
else:
yield check_good_repr, contract
test_cases.append(contract)
return test_cases


# Checks that we can reconvert the __str__() value and we get the same.
def test_reconversion():
# Checks that we can eval() the __repr__() value and
# we get an equivalent object.
@pytest.mark.parametrize("contract", get_repr_test_cases())
def test_repr(contract):
check_good_repr(contract)


def get_reconversion_test_cases():
test_cases = []
allc = (good_examples + semantic_fail_examples + contract_fail_examples)
for contract, _, exact in allc:
if isinstance(contract, list):
for c in contract:
yield check_recoversion, c, exact
test_cases.append((c, exact))
else:
yield check_recoversion, contract, exact
test_cases.append((contract, exact))
return test_cases


# Checks that we can reconvert the __str__() value and we get the same.
@pytest.mark.parametrize("contract,exact", get_reconversion_test_cases())
def test_reconversion(contract, exact):
check_recoversion(contract, exact)


def check_good_repr(c):
Expand Down

0 comments on commit a3c3f99

Please sign in to comment.