Skip to content

Commit 3389c1b

Browse files
authored
Merge pull request #91 from django-ftl/fix_pypy3_datetime
types.py - compatibility for PyPy3 5.10 and later
2 parents d9f42df + fcfacda commit 3389c1b

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

fluent.runtime/fluent/runtime/types.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ class DateFormatOptions(object):
277277

278278

279279
class FluentDateType(object):
280-
def _init(self, dt_obj, kwargs):
280+
# We need to match signature of `__init__` and `__new__` due to the way
281+
# some Python implementation (e.g. PyPy) implement some methods.
282+
# So we leave those alone, and implement another `_init_options`
283+
# which is called from other constructors.
284+
def _init_options(self, dt_obj, kwargs):
281285
if 'timeStyle' in kwargs and not isinstance(self, datetime):
282286
raise TypeError("timeStyle option can only be specified for datetime instances, not date instance")
283287

@@ -328,37 +332,30 @@ def _ensure_datetime_tzinfo(dt, tzinfo=None):
328332

329333

330334
class FluentDate(FluentDateType, date):
331-
def __new__(cls,
332-
dt_obj,
333-
**kwargs):
334-
self = super(FluentDate, cls).__new__(
335-
cls,
336-
dt_obj.year, dt_obj.month, dt_obj.day)
337-
self._init(dt_obj, kwargs)
338-
return self
335+
@classmethod
336+
def from_date(cls, dt_obj, **kwargs):
337+
obj = cls(dt_obj.year, dt_obj.month, dt_obj.day)
338+
obj._init_options(dt_obj, kwargs)
339+
return obj
339340

340341

341342
class FluentDateTime(FluentDateType, datetime):
342-
def __new__(cls,
343-
dt_obj,
344-
**kwargs):
345-
self = super(FluentDateTime, cls).__new__(
346-
cls,
347-
dt_obj.year, dt_obj.month, dt_obj.day,
348-
dt_obj.hour, dt_obj.minute, dt_obj.second,
349-
dt_obj.microsecond, tzinfo=dt_obj.tzinfo)
350-
351-
self._init(dt_obj, kwargs)
352-
return self
343+
@classmethod
344+
def from_date_time(cls, dt_obj, **kwargs):
345+
obj = cls(dt_obj.year, dt_obj.month, dt_obj.day,
346+
dt_obj.hour, dt_obj.minute, dt_obj.second,
347+
dt_obj.microsecond, tzinfo=dt_obj.tzinfo)
348+
obj._init_options(dt_obj, kwargs)
349+
return obj
353350

354351

355352
def fluent_date(dt, **kwargs):
356353
if isinstance(dt, FluentDateType) and not kwargs:
357354
return dt
358355
if isinstance(dt, datetime):
359-
return FluentDateTime(dt, **kwargs)
356+
return FluentDateTime.from_date_time(dt, **kwargs)
360357
elif isinstance(dt, date):
361-
return FluentDate(dt, **kwargs)
358+
return FluentDate.from_date(dt, **kwargs)
362359
elif isinstance(dt, FluentNone):
363360
return dt
364361
else:

0 commit comments

Comments
 (0)