clojure-spec

cfleming 2023-11-08T03:16:04.763369Z

I have a case where conform returns :clojure.spec.alpha/invalid, but explain returns “Success!“. Am I missing something?

(def test-data (rest '(defn F "s||C_)C^?" {} ([[& {:keys [], :strs [a2p*1.+lNz3+9.-fc? !2_!T2*8_.aw7.H14*7 B_-T*E5+3lKy-gE Ca++-F*_G58!7+? VM.3-E**S*R+09eW f-F?J-D_?r*xdjb.D]} :as kQ.zi] & yeok5+8+3_O_.mXJZ] "MyN-Rmu[Y" nil :Wd--IU9M*! ":*ULe,h" :p-:?6+__S- J91*) "EcO")))
=> #'cursive.extensions.specs/test-data
(s/conform :clojure.core.specs.alpha/defn-args test-data)
=> :clojure.spec.alpha/invalid
(s/explain :clojure.core.specs.alpha/defn-args test-data)
Success!

2023-11-08T03:22:18.256729Z

https://ask.clojure.org/index.php/13390/surprising-behaviour similar recent ask

cfleming 2023-11-08T03:23:36.792689Z

Yeah that looks similar.

cfleming 2023-11-08T03:26:38.703989Z

I’ve also found that generating code samples using the core specs doesn’t work well - it does work sometimes but not others, and I’m not sure why:

(gen/sample (s/gen ::core/bindings))
Error printing return value (ExceptionInfo) at clojure.test.check.generators/fn (generators.cljc:435).
Couldn't satisfy such-that predicate after 100 tries.

cfleming 2023-11-08T03:49:45.208889Z

I’ve commented on the ask with some cases. Looks to me like the cases should be valid, so perhaps the bug is in conform.

2023-11-08T03:58:29.213679Z

The way s/and composes for generators is unlikely to get you a working generator for more complex specs, basically it takes the generator first spec, and then uses the rest of the specs in the and as a predicate to filter out generated values that don't match the entire s/and

2023-11-08T03:59:02.454379Z

It is limited to 100 tries

cfleming 2023-11-08T03:59:48.214969Z

Hmm, I see. Thanks for the explanation.

2023-11-08T04:00:30.271059Z

I think there might be something you can bind to increase such-that's limit, but in general if you hit it you usually need to write a custom generator instead of relying on s/and's composed one

cfleming 2023-11-08T04:01:05.089579Z

Is there a good way to figure out which spec is problematic? And can I override generators for the built-in specs?

cfleming 2023-11-08T04:01:18.257179Z

The stacktrace didn’t seem especially useful.

2023-11-08T04:03:03.690289Z

spec has a way to define specs with custom generators, which is helpful for your own specs, for existing specs I sort of recall some mechanism for overriding but I am not sure

2023-11-08T04:03:18.236929Z

as far as figuring out which spec, I don't know

cfleming 2023-11-08T04:03:56.834109Z

Ok, thanks. I’m currently generating these forms with my own code, and then just checking the result against the spec.