Clojure

[spec] Generator overrides for spec aliases are not respected

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.9
  • Fix Version/s: Release 1.11
  • Component/s: None
  • Labels:
  • Approval:
    Vetted

Description

Generator overrides for spec aliases are not respected.

(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])
(s/def ::original number?)
(s/def ::alias ::original)

(every? double? (gen/sample (s/gen ::alias {::alias gen/double})))
;; => false

Providing a generator override for the original spec works as expected:

(every? double? (gen/sample (s/gen ::alias {::original gen/double})))
;; => true

Activity

Hide
Alex Miller added a comment -

Probably a missing delay in the alias case - there's another ticket that has the same cause.

Show
Alex Miller added a comment - Probably a missing delay in the alias case - there's another ticket that has the same cause.
Hide
Nate Smith added a comment -

Looks like it might be because gensub looks for matching overrides by calling spec-name, which returns the wrong value for spec aliases.

(require '[clojure.spec :as s])
(s/def ::original number?)
(s/def ::alias ::original)
(@#'clojure.spec/spec-name (s/get-spec ::alias))
;; => :user/original
Show
Nate Smith added a comment - Looks like it might be because gensub looks for matching overrides by calling spec-name, which returns the wrong value for spec aliases.
(require '[clojure.spec :as s])
(s/def ::original number?)
(s/def ::alias ::original)
(@#'clojure.spec/spec-name (s/get-spec ::alias))
;; => :user/original
Hide
Charles Despointes added a comment -

I've a somewhat similar issue. I think it is related.
I'm trying to do something like :

(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.gen.alpha :as gen])
(s/def ::bar any?)
(s/def ::foo (s/with-gen any? (fn [] (s/gen ::bar))))
(gen/generate (s/gen ::foo {::bar (fn [] (s/gen int?))}))

I'm somewhat expecting it generates me an integer like it would have with a direct aliasing to ::bar in ::foo definition. But it doesn't and keep the with-gen binded generator.
Is that the same issue or is that an expected behaviour or should i fill a new issue ?

Show
Charles Despointes added a comment - I've a somewhat similar issue. I think it is related. I'm trying to do something like :
(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.gen.alpha :as gen])
(s/def ::bar any?)
(s/def ::foo (s/with-gen any? (fn [] (s/gen ::bar))))
(gen/generate (s/gen ::foo {::bar (fn [] (s/gen int?))}))
I'm somewhat expecting it generates me an integer like it would have with a direct aliasing to ::bar in ::foo definition. But it doesn't and keep the with-gen binded generator. Is that the same issue or is that an expected behaviour or should i fill a new issue ?
Hide
James Gatannah added a comment -

I think I'm probably running into this, except that there seems to be some non-determinism involved.

It seems to behave differently, depending on whether I run it from within a deftest or from the REPL. (Running it from the REPL seems to fail every time. Running it inside deftest seems much more reliable).

My really long-winded description is at https://groups.google.com/forum/#!topic/clojure/zPWPmQGm94w

The sample where I tried to document exactly what I'm seeing is at https://gist.github.com/jimrthy/21851c52a8cd6b04a31ed08b1d0a7f04 (when I wrote that, running inside deftest seemed to work every time. That is not the case).

If nothing else, it would be nice to have better error messages that include the name of the spec it's failing to generate. Actually, that would be generally helpful and possibly worth its own ticket. Please let me know if you'd like me to create one.

Show
James Gatannah added a comment - I think I'm probably running into this, except that there seems to be some non-determinism involved. It seems to behave differently, depending on whether I run it from within a deftest or from the REPL. (Running it from the REPL seems to fail every time. Running it inside deftest seems much more reliable). My really long-winded description is at https://groups.google.com/forum/#!topic/clojure/zPWPmQGm94w The sample where I tried to document exactly what I'm seeing is at https://gist.github.com/jimrthy/21851c52a8cd6b04a31ed08b1d0a7f04 (when I wrote that, running inside deftest seemed to work every time. That is not the case). If nothing else, it would be nice to have better error messages that include the name of the spec it's failing to generate. Actually, that would be generally helpful and possibly worth its own ticket. Please let me know if you'd like me to create one.

People

Vote (8)
Watch (8)

Dates

  • Created:
    Updated: