Skip to content

Commit

Permalink
Allow casting non-strings to booleans as well
Browse files Browse the repository at this point in the history
The spec is a little vague w.r.t this, but I see no harm in enabling it.
In fact, when working with Excel files where booleans are represented as ints, this is quite useful.
  • Loading branch information
akariv committed Sep 18, 2018
1 parent b404c58 commit f36c385
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 2 additions & 3 deletions tableschema/types/boolean.py
Expand Up @@ -12,9 +12,8 @@

def cast_boolean(format, value, **options):
if not isinstance(value, bool):
if not isinstance(value, six.string_types):
return ERROR
value = value.strip()
if isinstance(value, six.string_types):
value = value.strip()
if value in options.get('trueValues', _TRUE_VALUES):
value = True
elif value in options.get('falseValues', _FALSE_VALUES):
Expand Down
2 changes: 2 additions & 0 deletions tests/types/test_boolean.py
Expand Up @@ -32,6 +32,8 @@
('default', 'No', ERROR, {}),
('default', 0, ERROR, {}),
('default', 1, ERROR, {}),
('default', 0, False, {'falseValues': [0], 'trueValues': [1]}),
('default', 1, True, {'falseValues': [0], 'trueValues': [1]}),
('default', '3.14', ERROR, {}),
('default', '', ERROR, {}),
('default', 'Yes', ERROR, {'trueValues': ['yes']}),
Expand Down

0 comments on commit f36c385

Please sign in to comment.