Skip to content

Commit f4505db

Browse files
authored
Add support for calling Python functions and methods with keyword arguments (#531)
* Add support for calling Python functions and methods with keyword arguments * Test invocation with kwargs * Finishing touches * Docstring warning * Ahhhhhhhhh
1 parent e476aba commit f4505db

File tree

9 files changed

+294
-72
lines changed

9 files changed

+294
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
* Added support for `future`s (#441)
10+
* Added support for calling Python functions and methods with keyword arguments (#531)
1011

1112
### Fixed
1213
* Fixed a bug where the Basilisp AST nodes for return values of `deftype` members could be marked as _statements_ rather than _expressions_, resulting in an incorrect `nil` return (#523)

Pipfile.lock

Lines changed: 50 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/basilisp/core.lpy

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,10 @@
12661266
(cond
12671267
(uuid? x) true
12681268
(string? x) (do (uuid/UUID x) true)
1269-
(int? x) (do (apply-kw uuid/UUID {:int x}) true)
1270-
(byte-string? x) (do (apply-kw uuid/UUID {:bytes x}) true)
1269+
(int? x) (do (uuid/UUID ** :int x) true)
1270+
(byte-string? x) (do (uuid/UUID ** :bytes x) true)
12711271
(or (py-tuple? x)
1272-
(vector? x)) (do (apply-kw uuid/UUID {:fields x}) true)
1272+
(vector? x)) (do (uuid/UUID ** :fields x) true)
12731273
:else false)
12741274
(catch python/AttributeError _ false)
12751275
(catch python/TypeError _ false)
@@ -2175,8 +2175,28 @@
21752175

21762176
(defn partial
21772177
"Return a function which is the partial application of f with args."
2178-
[f & args]
2179-
(apply basilisp.lang.runtime/partial f args))
2178+
([f] f)
2179+
([f & args]
2180+
(apply basilisp.lang.runtime/partial f args)))
2181+
2182+
(defn partial-kw
2183+
"Return a function which is the partial application of f with keyword arguments.
2184+
2185+
If a single argument is provided, it will be interpreted as a map of keyword arguments.
2186+
2187+
If multiple arguments are given, they are interpreted as key/value pairs and will
2188+
be converted into a hash-map before being partially applied to the function.
2189+
2190+
This function applies keyword arguments via `apply-kw`. As a consequence, Lisp keywords
2191+
will be converted to munged Python strings (via `name`), meaning namespaces will be lost
2192+
and identifiers which are not valid Python syntax will be converted to safe Python
2193+
identifiers."
2194+
([f] f)
2195+
([f m]
2196+
(apply-kw basilisp.lang.runtime/partial f m))
2197+
([f arg & args]
2198+
(let [m (apply hash-map (cons arg args))]
2199+
(partial-kw f m))))
21802200

21812201
(defn every?
21822202
"Return true if every element in coll satisfies pred."

0 commit comments

Comments
 (0)