AndreaCensi/contracts — Issue #4: Errors cause nosetests to be unable to continue, due to contract redefinition
Author: wickedgrey
Created: 2011-11-04 04:15:04+00:00
State: open

Consider a file foo.py (note, this is condensed from the real test case; apologies for typos, etc.):
```python
from contracts import contract, new_contract

@new_contract
def myContract(inst):
return isinstance(inst, object)

class Foo(object):
@contract
def __init__(self, a):
"""
:type a: number,$$%^^&
"""
pass
```
If foo gets imported in multiple test cases (we're running them via python setup.py nosetests), then the following occurs:

```
======================================================================
ERROR: Failure: ContractSyntaxError (Expected "(" (at char 8), (line:1, col:9)

line 1 >number,$$%^^&
^
|
here or nearby)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/nose/loader.py", line 390, in loadTestsFromName
addr.filename, addr.module)
File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 39, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 86, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
...
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 159, in contract_decorator
return contracts_decorate(function, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 224, in contracts_decorate
for x in accepts_dict])
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 306, in parse_flexible_spec
return parse_contract_string(spec)
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 80, in parse_contract_string
raise ContractSyntaxError(msg, where=where)
ContractSyntaxError: Expected "(" (at char 8), (line:1, col:9)

line 1 >number,$$%^^&
^
|
here or nearby

======================================================================
ERROR: Failure: ValueError (Tried to redefine 'myContract' with a definition that looks different to me.
- old: CheckCallable()
- new: CheckCallable()
)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/nose/loader.py", line 390, in loadTestsFromName
addr.filename, addr.module)
File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 39, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 86, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
...
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 493, in new_contract
new_contract_impl(identifier, function)
File "/usr/local/lib/python2.7/dist-packages/contracts/main.py", line 577, in new_contract_impl
raise ValueError(msg)
ValueError: Tried to redefine 'myContract' with a definition that looks different to me.
- old: CheckCallable()
- new: CheckCallable()
```

The ValueError is repeated for every subsequent test case that imports foo, even if the class with the broken definition is not used. This can make it difficult to find the real error when there are a large number of test cases.