diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index ce0866c8f..75730264d 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -49,6 +49,7 @@ dedupe_keys, find_spatialite, hash_record, + np, progressbar, sqlite3, suggest_column_types, @@ -174,12 +175,6 @@ def resolve_casing(name: str, candidates: Iterable[str]) -> str: except ImportError: pd = None -np: Any = None -try: - np = importlib.import_module("numpy") -except ImportError: - np = None - Column = namedtuple( "Column", ("cid", "name", "type", "notnull", "default_value", "is_pk") ) @@ -411,6 +406,7 @@ def _iter_complete_sql_statements(sql: str) -> Generator[str, None, None]: try: COLUMN_TYPE_MAPPING.update( { + np.bool_: "INTEGER", np.int8: "INTEGER", np.int16: "INTEGER", np.int32: "INTEGER", @@ -5695,7 +5691,7 @@ def drop(self, ignore: bool = False) -> None: def jsonify_if_needed(value: object) -> object: if isinstance(value, decimal.Decimal): return float(value) - if np is not None and isinstance(value, (np.integer, np.floating)): + if np is not None and isinstance(value, (np.integer, np.floating, np.bool_)): # sqlite3 would otherwise store the raw buffer as a BLOB return cast(Any, value).item() if isinstance(value, (dict, list, tuple)): diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index 49c218750..6d56ff222 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -41,6 +41,15 @@ OperationalError = dbapi2.OperationalError +np: Any = None +try: + np = importlib.import_module("numpy") +except ImportError: + np = None + +_NUMPY_BOOL_TYPE: type | None = getattr(np, "bool_", None) + + SPATIALITE_PATHS = ( "/usr/lib/x86_64-linux-gnu/mod_spatialite.so", "/usr/lib/aarch64-linux-gnu/mod_spatialite.so", @@ -142,6 +151,9 @@ def types_for_column_types( ) -> dict[str, type]: column_types: dict[str, type] = {} for key, types in all_column_types.items(): + if _NUMPY_BOOL_TYPE is not None and _NUMPY_BOOL_TYPE in types: + types.remove(_NUMPY_BOOL_TYPE) + types.add(bool) # Ignore null values if at least one other type present: if len(types) > 1: types.discard(None.__class__) diff --git a/tests/test_create.py b/tests/test_create.py index fcd9eee34..35f01a234 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1822,3 +1822,21 @@ def test_insert_raw_numpy_scalars(fresh_db): ("integer", "real"), ("integer", "real"), ] + + +@pytest.mark.parametrize("schema", ["inferred", "python_bool", "numpy_bool", "mixed"]) +def test_insert_numpy_booleans(fresh_db, schema): + np = pytest.importorskip("numpy") + table = fresh_db.table("t") + if schema == "python_bool": + table.create({"enabled": bool}) + elif schema == "numpy_bool": + table.create({"enabled": np.bool_}) + + values = [True if schema == "mixed" else np.bool_(True), np.bool_(False)] + table.insert_all([{"enabled": value} for value in values]) + + assert table.columns_dict == {"enabled": int} + assert fresh_db.execute( + "select enabled, typeof(enabled) from t order by rowid" + ).fetchall() == [(1, "integer"), (0, "integer")]