Detect subclasses of dict/tuple/list, fixes #87

This commit is contained in:
Simon Willison 2020-02-15 18:20:39 -08:00
commit 685e6a1bb3
4 changed files with 9 additions and 5 deletions

View file

@ -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

View file

@ -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):