@@ -277,7 +277,11 @@ class DateFormatOptions(object):
277277
278278
279279class 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
330334class 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
341342class 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
355352def 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