mirror of
https://github.com/carrotquest/django-clickhouse.git
synced 2024-11-25 10:33:47 +03:00
Багфиксы совместимости с python 3.7
This commit is contained in:
parent
10fae9220c
commit
ae253e0587
|
@ -67,7 +67,7 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
|
|||
field_names = field_names or cls.fields(writable=False).keys()
|
||||
|
||||
# Strange, but sometimes the columns are in different order...
|
||||
field_names = tuple(sorted(field_names))
|
||||
field_names = sorted(field_names)
|
||||
|
||||
if defaults:
|
||||
defaults_new = deepcopy(cls._defaults)
|
||||
|
@ -75,7 +75,12 @@ class ClickHouseModel(with_metaclass(ClickHouseModelMeta, InfiModel)):
|
|||
else:
|
||||
defaults_new = cls._defaults
|
||||
|
||||
return namedtuple("%sTuple" % cls.__name__, field_names, defaults=defaults_new)
|
||||
# defaults should be rightmost arguments
|
||||
required_field_names = tuple(name for name in field_names if name not in defaults_new)
|
||||
|
||||
default_field_names, default_values = zip(*defaults_new.items())
|
||||
|
||||
return namedtuple("%sTuple" % cls.__name__, required_field_names + default_field_names, defaults=default_values)
|
||||
|
||||
@classmethod
|
||||
def objects_in(cls, database): # type: (Database) -> QuerySet
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import sys
|
||||
from collections import namedtuple as basenamedtuple, Mapping
|
||||
from functools import lru_cache
|
||||
|
||||
from copy import deepcopy
|
||||
from collections import namedtuple as basenamedtuple
|
||||
|
||||
|
||||
def namedtuple(*args, **kwargs):
|
||||
|
@ -16,9 +13,6 @@ def namedtuple(*args, **kwargs):
|
|||
defaults = kwargs.pop('defaults', {})
|
||||
TupleClass = basenamedtuple(*args, **kwargs)
|
||||
TupleClass.__new__.__defaults__ = (None,) * len(TupleClass._fields)
|
||||
if isinstance(defaults, Mapping):
|
||||
prototype = TupleClass(**defaults)
|
||||
else:
|
||||
prototype = TupleClass(*defaults)
|
||||
TupleClass.__new__.__defaults__ = tuple(prototype)
|
||||
return TupleClass
|
||||
|
|
|
@ -159,7 +159,7 @@ def int_ranges(items: Iterable[int]) -> Iterator[Tuple[int, int]]:
|
|||
yield interval
|
||||
|
||||
if interval_start is None:
|
||||
raise StopIteration()
|
||||
return
|
||||
else:
|
||||
yield interval_start, prev_item
|
||||
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
from unittest import TestCase
|
||||
from django.test import TestCase
|
||||
|
||||
from django_clickhouse.compatibility import namedtuple
|
||||
|
||||
|
||||
class NamedTupleTest(TestCase):
|
||||
def test_defaults(self):
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults={'c': 3})
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
||||
self.assertTupleEqual((1, 2, 3), tuple(TestTuple(1, b=2)))
|
||||
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(1, 2, 4)))
|
||||
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(a=1, b=2, c=4)))
|
||||
|
||||
def test_exceptions(self):
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults={'c': 3})
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
||||
|
||||
# BUG On python < 3.7 this error is not raised, as not given defaults are filled by None
|
||||
# with self.assertRaises(TypeError):
|
||||
|
@ -22,8 +22,8 @@ class NamedTupleTest(TestCase):
|
|||
|
||||
def test_different_defaults(self):
|
||||
# Test that 2 tuple type defaults don't affect each other
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults={'c': 3})
|
||||
OtherTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults={'c': 4})
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
||||
OtherTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[4])
|
||||
t1 = TestTuple(a=1, b=2)
|
||||
t2 = OtherTuple(a=3, b=4)
|
||||
self.assertTupleEqual((1, 2, 3), tuple(t1))
|
||||
|
@ -31,7 +31,7 @@ class NamedTupleTest(TestCase):
|
|||
|
||||
def test_defaults_cache(self):
|
||||
# Test that 2 tuple instances don't affect each other's defaults
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults={'c': 3})
|
||||
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
||||
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(a=1, b=2, c=4)))
|
||||
self.assertTupleEqual((1, 2, 3), tuple(TestTuple(a=1, b=2)))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user