mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Detect subclasses of dict/tuple/list, fixes #87
This commit is contained in:
parent
5e0000609f
commit
685e6a1bb3
4 changed files with 9 additions and 5 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from .utils import sqlite3, OperationalError, suggest_column_types
|
||||
from collections import namedtuple
|
||||
from collections import namedtuple, OrderedDict
|
||||
import datetime
|
||||
import hashlib
|
||||
import itertools
|
||||
|
|
|
|||
|
|
@ -18,10 +18,11 @@ def suggest_column_types(records):
|
|||
for key, types in all_column_types.items():
|
||||
if len(types) == 1:
|
||||
t = list(types)[0]
|
||||
# But if it's list / tuple / dict, use str instead as we
|
||||
# will be storing it as JSON in the table
|
||||
if t in (list, tuple, dict):
|
||||
t = str
|
||||
# But if it's a subclass of list / tuple / dict, use str
|
||||
# instead as we will be storing it as JSON in the table
|
||||
for superclass in (list, tuple, dict):
|
||||
if issubclass(t, superclass):
|
||||
t = str
|
||||
elif {int, bool}.issuperset(types):
|
||||
t = int
|
||||
elif {int, float, bool}.issuperset(types):
|
||||
|
|
|
|||
|
|
@ -621,6 +621,7 @@ def test_create_index_if_not_exists(fresh_db):
|
|||
["list with", "two items"],
|
||||
{"dictionary": "simple"},
|
||||
{"dictionary": {"nested": "complex"}},
|
||||
collections.OrderedDict([("key1", {"nested": "complex"}), ("key2", "foo"),]),
|
||||
[{"list": "of"}, {"two": "dicts"}],
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import pytest
|
||||
from collections import OrderedDict
|
||||
from sqlite_utils.utils import suggest_column_types
|
||||
|
||||
|
||||
|
|
@ -11,6 +12,7 @@ from sqlite_utils.utils import suggest_column_types
|
|||
([{"a": [1]}], {"a": str}),
|
||||
([{"a": (1,)}], {"a": str}),
|
||||
([{"a": {"b": 1}}], {"a": str}),
|
||||
([{"a": OrderedDict({"b": 1})}], {"a": str}),
|
||||
([{"a": 1}, {"a": 1.1}], {"a": float}),
|
||||
([{"a": b"b"}], {"a": bytes}),
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue