Skip to content

types.py - compatibility for PyPy3 5.10 and later #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions fluent.runtime/fluent/runtime/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ class DateFormatOptions(object):


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

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


class FluentDate(FluentDateType, date):
def __new__(cls,
dt_obj,
**kwargs):
self = super(FluentDate, cls).__new__(
cls,
dt_obj.year, dt_obj.month, dt_obj.day)
self._init(dt_obj, kwargs)
return self
@classmethod
def from_date(cls, dt_obj, **kwargs):
obj = cls(dt_obj.year, dt_obj.month, dt_obj.day)
obj._init_options(dt_obj, kwargs)
return obj


class FluentDateTime(FluentDateType, datetime):
def __new__(cls,
dt_obj,
**kwargs):
self = super(FluentDateTime, cls).__new__(
cls,
dt_obj.year, dt_obj.month, dt_obj.day,
dt_obj.hour, dt_obj.minute, dt_obj.second,
dt_obj.microsecond, tzinfo=dt_obj.tzinfo)

self._init(dt_obj, kwargs)
return self
@classmethod
def from_date_time(cls, dt_obj, **kwargs):
obj = cls(dt_obj.year, dt_obj.month, dt_obj.day,
dt_obj.hour, dt_obj.minute, dt_obj.second,
dt_obj.microsecond, tzinfo=dt_obj.tzinfo)
obj._init_options(dt_obj, kwargs)
return obj


def fluent_date(dt, **kwargs):
if isinstance(dt, FluentDateType) and not kwargs:
return dt
if isinstance(dt, datetime):
return FluentDateTime(dt, **kwargs)
return FluentDateTime.from_date_time(dt, **kwargs)
elif isinstance(dt, date):
return FluentDate(dt, **kwargs)
return FluentDate.from_date(dt, **kwargs)
elif isinstance(dt, FluentNone):
return dt
else:
Expand Down