3 ms·
> When decorated by typeguard, every call to that function checks every integer nested in that list. > When decorated by beartype, every call to the same funct
by a-dub 5y ago
> When decorated by typeguard, every call to that function checks every integer nested in that list.
> When decorated by beartype, every call to the same function checks only a single random integer contained in a single random nested list contained in a single random nested list contained in that parent list. This is what we mean by the quaint phrase "one-way random walk over the expected data structure."
so it's not actually faster, it just doesn't actually check the types or even the complete shape of the data structure. seems like it would catch obvious problems, but make more subtle ones way harder to find as it creates a sense that "oh, the types are checked" when they aren't really.
- eru 5y agoYes, though as eg QuickCheck shows, randomised testing is unreasonably effective in practice.
- a-dub 5y agoassuming you're sampling the same distribution. every call to a function can have different arguments. this seems to make the assumption that the types and shape don't change so you can amortize the cost of type checking over multiple function calls... but they can, and do, sometimes with low probability and those are the hard bugs to solve. especially when you think types are being checked.
- eru 5y agoI suspect you are reading different things into my comment than I wanted to convey. Your argument is perfectly valid, if you try to steelman my argument into something like a mathematical argument. But it wasn't meant that way. I meant exactly what I said: empirically, in practice, randomised checking is unreasonably effective. You illustrated the 'unreasonable' part.
- a-dub 5y agoi'd never seen this quickcheck thing before... it's basically fuzzing built into unit tests, neat... it is, however, quite different from a type checker though. where a fuzz tester generates random inputs to try and break a function... a type checker enforces assertions on data that is actually flowing through the system. i think my big beef with this library is that it does so probabilistically, while marketing itself as something that does complete checks and has been highly optimized. it's not a fast assertion checker, it is one that employs random sampling... that's fine, but it should be extremely upfront about that as this misunderstanding has the potential to cause a lot of pain on top of already painful situations.
- eru 5y agoYes, calling this a typechecker is perhaps a bit euphemistic. Btw, if you want to try something like QuickCheck, Python's Hypothesis is perhaps the most accessible implementation of the idea.
- maweki 5y agoQuickCheck generates its own test cases and possible shapes. More importantly, it tries all possible constructors of a type (or a reasonable number). So you're reasonably confident that you tried a lot of "shape-cases".
- eru 5y ago> it tries all possible constructors of a type It tries a 'reasonable' number. If you want to try everything, have a look at SmallCheck. I would imagine you would want to combine Beartype with tests, too. Perhaps written manually, or with test data generated via Hypothesis.
- More-nitors 5y agobut not all cases are generated.. for example, if I have an external api response that I'd like to validate, beartype fails to do so.
- chriswarbo 5y ago> If you want to try everything, have a look at SmallCheck. FYI my favourite approach is LazySmallCheck, which runs tests with an unevaluated input, and only generates data if the input gets forced; at which point it runs the test for each constructor. The original version lacked some useful generators, like functions; the "LazySmallCheck2012" fork added these (e.g. generating functions in terms of an input/output lookup table), but overall these libraries seem to be unmaintained :( IIRC there are also similar alternatives based around Prolog-like search.
- maweki 5y agoI'm pretty sure that SmallCheck also doesn't try out every constructor for Integer.
- eru 5y agoYes, and I carefully formulated my sentence not to say that. :)