@@ -277,7 +277,11 @@ class DateFormatOptions(object):
277
277
278
278
279
279
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 ):
281
285
if 'timeStyle' in kwargs and not isinstance (self , datetime ):
282
286
raise TypeError ("timeStyle option can only be specified for datetime instances, not date instance" )
283
287
@@ -328,37 +332,30 @@ def _ensure_datetime_tzinfo(dt, tzinfo=None):
328
332
329
333
330
334
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
339
340
340
341
341
342
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
353
350
354
351
355
352
def fluent_date (dt , ** kwargs ):
356
353
if isinstance (dt , FluentDateType ) and not kwargs :
357
354
return dt
358
355
if isinstance (dt , datetime ):
359
- return FluentDateTime (dt , ** kwargs )
356
+ return FluentDateTime . from_date_time (dt , ** kwargs )
360
357
elif isinstance (dt , date ):
361
- return FluentDate (dt , ** kwargs )
358
+ return FluentDate . from_date (dt , ** kwargs )
362
359
elif isinstance (dt , FluentNone ):
363
360
return dt
364
361
else :
0 commit comments