Skip to content

Commit

Permalink
Implement distutils.strtobool in class fields.StringBoolean (#204)
Browse files Browse the repository at this point in the history
* Implement distutils.strtobool in class fields.StringBoolean

To remove dependency on distutils as suggested in #203.

* Implement distutils.strtobool in class fields.StringBoolean

To remove dependency on distutils as suggested in #203.

* increase test coverage for strtobool
  • Loading branch information
DemiBSel authored Mar 17, 2022
1 parent f135da5 commit 97197bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Contributors
* Abdul muhaimin <[email protected]>
* Marek Suppa <[email protected]>
* Bob Callaway <[email protected]>
* Elias Sebbar <[email protected]>
5 changes: 2 additions & 3 deletions src/shillelagh/adapters/api/gsheets/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Custom fields for the GSheets adapter.
"""
import datetime
from distutils.util import strtobool
from typing import Any, List, Optional, Type

from shillelagh.adapters.api.gsheets.parsing.date import (
Expand All @@ -13,7 +12,7 @@
format_number_pattern,
parse_number_pattern,
)
from shillelagh.fields import External, Field, Internal, Order
from shillelagh.fields import External, Field, Internal, Order, StringBoolean
from shillelagh.filters import Filter

# timestamp format used in SQL queries
Expand Down Expand Up @@ -224,7 +223,7 @@ def parse(self, value: Optional[str]) -> Optional[bool]:
if value is None or value == "":
return None

return bool(strtobool(value))
return StringBoolean.strtobool(value)

def format(self, value: Optional[bool]) -> str:
if value is None:
Expand Down
19 changes: 17 additions & 2 deletions src/shillelagh/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Fields representing columns of different types and capabilities.
"""
import datetime
from distutils.util import strtobool
from enum import Enum
from typing import Any, Generic, List, Optional, Type, TypeVar, cast

Expand Down Expand Up @@ -575,7 +574,7 @@ class StringBoolean(Field[str, bool]):
def parse(self, value: Optional[str]) -> Optional[bool]:
if value is None:
return None
return bool(strtobool(value))
return StringBoolean.strtobool(value)

def format(self, value: Optional[bool]) -> Optional[str]:
if value is None:
Expand All @@ -587,6 +586,22 @@ def quote(self, value: Optional[str]) -> str:
return "NULL"
return value

@staticmethod
def strtobool(val: str) -> bool:
"""
Convert a string representation of truth to a boolean.
True values are 'y', 'yes', 't', 'true', 'on', and '1'.
False values are 'n', 'no', 'f', 'false', 'off', and '0'.
Raises ValueError if 'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError("invalid truth value %s" % val)


class IntBoolean(Field[int, bool]):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
import datetime

import pytest

from shillelagh.backends.apsw.db import connect
from shillelagh.fields import (
Blob,
Expand Down Expand Up @@ -263,6 +265,9 @@ def test_string_boolean() -> None:
"""
assert StringBoolean().parse("TRUE") is True
assert StringBoolean().parse("FALSE") is False
with pytest.raises(ValueError) as expected_err:
StringBoolean().parse("B")
assert str(expected_err.value) == "invalid truth value b"
assert StringBoolean().parse(None) is None
assert StringBoolean().format(True) == "TRUE"
assert StringBoolean().format(False) == "FALSE"
Expand Down

0 comments on commit 97197bd

Please sign in to comment.