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

Request: support the concept of "closedness" for Maps #2237

Open
fosskers opened this issue Dec 8, 2023 · 4 comments
Open

Request: support the concept of "closedness" for Maps #2237

fosskers opened this issue Dec 8, 2023 · 4 comments

Comments

@fosskers
Copy link

fosskers commented Dec 8, 2023

Is your feature request related to a problem? Please describe.

The "typing" library https://github.com/metosin/malli supports the concept of "closedness" for Maps:

(m/validate
  [:map {:closed true} [:x :int]]
  {:x 1, :extra "key"})
; => false

In other words, when we expect there to be only the exact keys specified and no more. Using the integration provided by malli, clj-kondo warns when required keys are missing, but not when closedness is being violated.

Describe the solution you'd like

For the following Clojure:

(def Pkg [:map {:closed true}
          [:Description :string]])

(defn fooz
  "zii"
  {:malli/schema [:=> [:cat Pkg] :string]}
  [pkg]
  (:Name pkg))

(comment
  (fooz {:Name "hi"}))

the following clj-kondo config is generated by Malli:

{:linters {:unresolved-symbol {:exclude [(malli.core/=>)]},
           :type-mismatch {:namespaces {packages {fooz {:arities {1 {:args [{:op :keys, :req {:Description :string}}],
                                                                     :ret :string}}}}}}}}

If clj-kondo could be altered to understand some marker for closedness here, then malli could be updated to emit that. Then, the illegal usage of :Name would be flagged at both call sites.

Please and thank you!

Describe alternatives you've considered
N/A

Additional context
Original discussion on the Clojure Slack: https://clojurians.slack.com/archives/CHY97NXE2/p1701764317891619

@borkdude borkdude added this to Needs triage in clj-kondo via automation Dec 8, 2023
@borkdude borkdude moved this from Needs triage to Medium priority (new / enhance) in clj-kondo Dec 8, 2023
@jkrasnay
Copy link

jkrasnay commented Jan 4, 2024

It would be super cool if clj-kondo could actually use Malli itself for the type checking.
I have a function that uses keyword varargs that looks like this:

(defn quux
  {:malli/schema [:=> [:cat
                       [:altn
                        [:map [:map {:closed true}
                               [:foo {:optional true} boolean?]
                               [:bar {:optional true} boolean?]]]
                        [:args [:*
                                [:alt
                                 [:cat [:= :foo] boolean?]
                                 [:cat [:= :bar] boolean?]]]]]]
                  :any]}
  [& {:keys [foo bar]}]
  ,,,)

(quux :foo true)

But the Malli instrumentation generates just the following :type-mismatch config:

quux {:arities {:varargs {:args [:any],
                          :ret :any,
                          :min-arity 0}}}

@fosskers
Copy link
Author

fosskers commented Jan 6, 2024

I wonder if that's a Malli issue, since type checking does occur for "normally" shaped maps, as shown above in Pkg (i.e. passing :Description an int would report an error).

@borkdude
Copy link
Member

borkdude commented Jan 6, 2024

It would be super cool if clj-kondo could actually use Malli itself for the type checking.

This is probably not feasible in the short term but we could extend the type checking mechanism with a closed check.

@jkrasnay
Copy link

jkrasnay commented Jan 6, 2024

@fosskers The problem is that the arguments can be a single map or a sequence of specific keyword/value pairs.
I'd be pretty happy with just closed maps, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
clj-kondo
  
Medium priority (new / enhance)
Development

No branches or pull requests

3 participants