Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add a new BeartypeConf.strategy_O1_check_sequence_index option enabling callers to deterministically type-check sequences #385

Open
leycec opened this issue May 24, 2024 Discussed in #383 · 1 comment

Comments

@leycec
Copy link
Member

leycec commented May 24, 2024

Discussed in #383

Originally posted by avolchek May 23, 2024
Hi!

The documentation states that for the O1 strategy, only one element of the container is selected, and the selection is random. Is it possible to replace random selection with a more deterministic approach (e.g., selecting the first element)?

The problem with random selection is that it makes tests in CI less robust. For example, I enabled type-checking in some tests in my codebase, and they passed successfully. However, this was only because the random selection chose a "good" element from the container. The next day, my colleague changed an unrelated piece of code, altering the RNG state. As a result, a "bad" element was selected, causing Beartype to complain, and the test failed, leading to my colleague's frustration.

My codebase is large, with many people working on it. It would be great to be able to disable randomness via configuration to avoid situations like the one mentioned above :)

@leycec
Copy link
Member Author

leycec commented May 24, 2024

Superb request! Yes. So much yes. You're right, of course. This is an excellent feature request. Deterministic CI is a lofty goal. @beartype should support you in that goal, @avolchek. You literally already resolved super-nasty issue #382 for us. Quid pro quo and my genteel nature demands I do this for you and your justifiably frustrated colleague. When colleagues are frustrated with @beartype, only one of us can be right – and it's probably not @beartype. Bad bear. 🐻 😠 🐻

This Plan: It Is a Good Plan

Let's add a new BeartypeConf(strategy_O1_check_sequence_index: Optional[int] = None, ...) option to @beartype's existing configuration API. Assuming you also use beartype.claw import hooks, you'd probably set this option like so:

# In your "{your_package}.__init__" submodule:
from beartype import BeartypeConf
from beartype.claw import beartype_this_package
beartype_this_package(conf=BeartypeConf(strategy_O1_check_sequence_index=0))

The idea here is pretty simple. strategy_O1_check_sequence_index:

  • Defaults to None, which then instructs @beartype to non-deterministically type-check one item of each sequence with a pseudo-random index. Understandably, you don't want that.
  • Can be set to an integer, in which case @beartype will then deterministically type-check one item of each sequence with that index. You want that. Really, the only two sensible hard-coded indices are:
    • strategy_O1_check_sequence_index=0, type-checking the first item.
    • strategy_O1_check_sequence_index=-1, type-checking the last item.

This is (...probably) trivial stuff. So, this will (...probably) make it into our upcoming @beartype 0.19.0 release. Let's see if @leycec does anything at all! So sleepy. 🥱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant