pschanely / CrossHair

An analysis tool for Python that blurs the line between testing and type systems.
Other
1.04k stars 49 forks source link

AssertionError Raised when Running Example #171

Closed wellsonah2019 closed 2 years ago

wellsonah2019 commented 2 years ago

I ran into an assertion error when running the examples (in particular, the average example) in the documentation (https://crosshair.readthedocs.io/en/latest/cover.html).

$ cat crosshair-test.py
from typing import List, Optional
def average(nums: List[float], default:Optional[float] = None) -> float:
    if len(nums) == 0:
        if default is None:
            raise ValueError
        return default
    return sum(nums) / len(nums)

$ crosshair cover crosshair-test.average
average([0.0], None)
average([], 0.0)
Traceback (most recent call last):
  File "/home/wellsonah2019/.local/bin/crosshair", line 8, in <module>
    sys.exit(main())
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/main.py", line 714, in main
    sys.exit(unwalled_main(cmd_args))
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/main.py", line 678, in unwalled_main
    return cover(args, defaults.overlay(options), sys.stdout, sys.stderr)
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/main.py", line 592, in cover
    return output_eval_exression_paths(fn, paths, stdout, stderr)
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/path_cover.py", line 146, in output_eval_exression_paths
    stdout.write(fn.__name__ + "(" + repr_boundargs(path.args) + ")\n")
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/path_cover.py", line 128, in repr_boundargs
    pieces = list(map(repr, boundargs.args))
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/simplestructs.py", line 663, in __repr__
    return repr(list(self.__iter__()))
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/libimpl/builtinslib.py", line 1746, in __iter__
    while SymbolicBool(idx < len_var).__bool__():
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/libimpl/builtinslib.py", line 925, in __init__
    SymbolicValue.__init__(self, smtvar, typ)
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/libimpl/builtinslib.py", line 239, in __init__
    self.statespace = context_statespace()
  File "/home/wellsonah2019/.local/lib/python3.8/site-packages/crosshair/statespace.py", line 214, in context_statespace
    assert space is not None
AssertionError

In the documentation, it is supposed to show the final input (which would then raise a ValueError), however as you can see, it gave an error instead. I am running this on Ubuntu 20.04 using Python 3.8. May I know if this is intended or if I am missing something?

Note: removing the "raise ValueError" line allowed me to get the same output as the documentation.

Thanks!

pschanely commented 2 years ago

Thank you for the bug report! Indeed, I can reproduce this issue - the exception handling I implemented for the cover command did not properly realize the inputs, causing this failure. The fix is straightforward, however. I expect to be able to cut a release later today - I'll update this issue then.

The cover command doesn't have as many users as the contract-related logic; I hope you'll continue to let me know how it goes and file issues, etc. Thank you!

wellsonah2019 commented 2 years ago

That's great to know! I'll try to run it again once the fix is out. Thanks a lot!

pschanely commented 2 years ago

Ok! Fix and new regression test was committed in e782601859860fa25d2113cb56db2e023f9ec27e, and released in v0.0.26. Thanks in advance for trying it again and circling back!

wellsonah2019 commented 2 years ago

I installed the new version and tested it again. It works just like the documentation now.

Thanks!