Arrow: Implement PEP 495

Created on 9 Nov 2017  路  5Comments  路  Source: arrow-py/arrow

Since arrow provides its own datetime-like, it should really implement PEP 495, e.g. the fold attribute, with functionality backported to Python pre-3.6 Python versions.

Issues that this will or could fix:

  • #424
  • #476
  • #368 (I think)

There may be more, I'm not sure. Either way, it's pretty important.

enhancement help wanted

All 5 comments

I will take a crack at this a bit later. I think it's not terribly difficult, but from an implementation perspective, the one major question about a backport is whether you want the compatibility code at runtime (makes the code itself cleaner) or at import time (code may run faster).

So, for example, say I implement a fold property in arrow:

@property
def fold(self):
    return getattr(self._datetime, 'fold', self._fold)

@fold.setter
def fold(self, val):
    if getattr(self._datetime, 'fold', None) is None:
        if val not in {0, 1}:
            raise ValueError('fold attribute must be either 0 or 1')
        self._fold = fold
    else:
        self._datetime = self._datetime.replace(fold, val)

Alternatively, it can be done this way:

if getattr(datetime, 'fold', None):
    @property
    def fold(self):
        return self._fold

    @fold.setter
    def fold(self, val):
        if self._fold not in {0, 1}:
            raise ValueError('Fold must be 0 or 1')
        self._fold = val
else:
    @property
    def fold(self):
        return self._datetime.fold

    @fold.setter
    def fold(self, val):
        self._datetime = self._datetime.replace(fold=val)

The third, and I think best option would be to use dateutil.tz.enfold, which already backports almost all the functionality of PEP 495 (with the exception of dateutil/dateutil#344, which is hard for dateutil to fix but easy for arrow). The only problem with this approach is that tz.enfold was introduced in dateutil version 2.6.0, meaning you'd need to pin to >=2.6.0. If that's a problem, it might be best to backport the enfold function into arrow.

@pganssle would you still be interested in implementing this? dateutil is pinned at 2.6.0 right now if that helps.

@systemcatch Why is dateutil pinned at all in this library? I don't think that's a good idea at all.

Anyway, I don't really have time for this, so someone else will have to take it up.

Oh, I just realized you might have meant pinned to have a minimum of 2.6.0, which would probably make things easier.

That said, doesn't seem like it's actually pinned in setup.py

Done in #802 :partying_face:

Was this page helpful?
0 / 5 - 0 ratings