Skip to content

Commit

Permalink
[config] coerce more to string when 'type: str' (ansible#72172)
Browse files Browse the repository at this point in the history
Change:
- When a plugin defines `type: str` on a parameter, treat more kinds of
  input as a string instead of whatever it is parsed as.

Test Plan:
- New unit tests
- CI

Signed-off-by: Rick Elrod <[email protected]>
  • Loading branch information
relrod authored Oct 14, 2020
1 parent 6e7a40d commit 3b40c6f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/more-types-to-string-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "config - more types are now automatically coerced to string when ``type: str`` is used and the value is parsed as a different type"
2 changes: 1 addition & 1 deletion lib/ansible/config/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def ensure_type(value, value_type, origin=None):
errmsg = 'dictionary'

elif value_type in ('str', 'string'):
if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode)):
if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode, bool, int, float, complex)):
value = unquote(to_text(value, errors='surrogate_or_strict'))
else:
errmsg = 'string'
Expand Down
Empty file added test/units/config/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions test/units/config/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
('a', 'string', string_types),
('Café', 'string', string_types),
('', 'string', string_types),
('29', 'str', string_types),
('13.37', 'str', string_types),
('123j', 'string', string_types),
('0x123', 'string', string_types),
('true', 'string', string_types),
('True', 'string', string_types),
(0, 'str', string_types),
(29, 'str', string_types),
(13.37, 'str', string_types),
(123j, 'string', string_types),
(0x123, 'string', string_types),
(True, 'string', string_types),
('None', 'none', type(None))
]

Expand Down

0 comments on commit 3b40c6f

Please sign in to comment.