CrossHair icon indicating copy to clipboard operation
CrossHair copied to clipboard

Register contracts for nondeterministic functions in the standard library

Open pschanely opened this issue 3 years ago • 2 comments

We now have some effective strategies for dealing with non-deterministic behavior, by adding contracts to existing functions. Let's apply them to the standard library. Some high-priority ones off the top of my head:

  • [x] time.time
  • [x] time.uniform
  • [ ] random.choice
  • [x] random.randint / randrange / uniform / random / randbits
  • [x] functools.lru_cache / cache
  • [ ] sys.stdin
  • [ ] uuid.uuid*

pschanely avatar May 17 '22 20:05 pschanely

The remaining ones may require even more capabilities. I'm not immediately prioritizing them.

For instance, I'd like to simply disable lru_cache and friends, but it seems like we'd need to intercept it at import time rather than at run time. For stdin, I'd like to intercept only the read() bound to stdin, not to any TextIOWrapper.

pschanely avatar May 19 '22 18:05 pschanely

I figured out a workaround for disabling the caching behaviors of lru_cache and cache: we dynamically detect the wrapper functions at runtime and skip the caching bits. Implemented in 2ab5105aba83e8b26a40e9a1362a946fc8b13644

Now, you won't get non-deterministic errors when analyzing behavior that involves these caches!

pschanely avatar Jul 01 '22 12:07 pschanely