Skip to content

Commit 5bf4df5

Browse files
authored
Raise ArgumentError in Keyword.from_keys/2 for non-atom keys (#15197)
1 parent a7b2d6c commit 5bf4df5

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

lib/elixir/lib/keyword.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,23 @@ defmodule Keyword do
119119
120120
iex> Keyword.from_keys([:foo, :bar, :baz], :atom)
121121
[foo: :atom, bar: :atom, baz: :atom]
122+
122123
iex> Keyword.from_keys([], :atom)
123124
[]
124125
126+
iex> Keyword.from_keys(["foo"], :bar)
127+
** (ArgumentError) expected a list of atoms as keys, got: "foo"
125128
"""
126129
@doc since: "1.14.0"
127130
@spec from_keys([key], value) :: t(value)
128131
def from_keys(keys, value) when is_list(keys) do
129-
:lists.map(&{&1, value}, keys)
132+
:lists.map(
133+
fn
134+
key when is_atom(key) -> {key, value}
135+
other -> raise ArgumentError, "expected a list of atoms as keys, got: #{inspect(other)}"
136+
end,
137+
keys
138+
)
130139
end
131140

132141
@doc """

0 commit comments

Comments
 (0)