#542 Fix datetime.utcnow deprecation warning
Merged 21 days ago by onosek. Opened 3 months ago by dherrera.
dherrera/fedpkg datetime-deprecation  into  master

file modified
+3 -3
@@ -15,7 +15,7 @@ 

  import re

  import six

  

- from datetime import datetime, timedelta

+ from datetime import datetime, timedelta, timezone

  

  # doc/fedpkg_man_page.py uses the 'cli' import

  from . import cli  # noqa
@@ -363,7 +363,7 @@ 

              override = result['overrides'][0]

              expiration_date = datetime.strptime(override['expiration_date'],

                                                  '%Y-%m-%d %H:%M:%S')

-             if expiration_date < datetime.utcnow():

+             if expiration_date < datetime.now(timezone.utc).replace(tzinfo=None):

                  self.log.info(

                      'Buildroot override for %s exists and is expired. Consider'

                      ' using command `override extend` to extend duration.',
@@ -384,7 +384,7 @@ 

          override = result['overrides'][0]

          expiration_date = datetime.strptime(override['expiration_date'],

                                              '%Y-%m-%d %H:%M:%S')

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc).replace(tzinfo=None)

  

          # bodhi-client binding API save_override calculates expiration

          # date by adding duration to datetime.utcnow

file modified
+2 -2
@@ -13,7 +13,7 @@ 

  # So that we import the bugzilla package and not fedpkg.bugzilla

  from __future__ import absolute_import

  

- from datetime import datetime

+ from datetime import datetime, timezone

  

  import bugzilla

  
@@ -95,7 +95,7 @@ 

                  if update_dt:

                      dt = datetime.strptime(

                          update_dt.value, '%Y%m%dT%H:%M:%S')

-                     delta = datetime.utcnow().date() - dt.date()

+                     delta = datetime.now(timezone.utc).replace(tzinfo=None).date() - dt.date()

                      if delta.days > 60:

                          raise rpkgError('The Bugzilla bug\'s review was '

                                          'approved over 60 days ago')

file modified
+2 -2
@@ -12,7 +12,7 @@ 

  

  import json

  import re

- from datetime import datetime

+ from datetime import datetime, timezone

  

  import git

  import requests
@@ -292,7 +292,7 @@ 

      for sl, eol in sl_dict.items():

          if re.match(eol_date_regex, eol):

              eol_date = datetime.strptime(eol, '%Y-%m-%d').date()

-             today = datetime.utcnow().date()

+             today = datetime.now(timezone.utc).replace(tzinfo=None).date()

              if eol_date < today:

                  raise rpkgError(

                      'The SL "{0}" is already expired'.format(eol))

file modified
+8 -8
@@ -16,7 +16,7 @@ 

  import os

  import re

  import sys

- from datetime import datetime, timedelta

+ from datetime import datetime, timedelta, timezone

  from os import rmdir

  from tempfile import mkdtemp, mkstemp

  
@@ -472,7 +472,7 @@ 

          self.mock_bug.assigned_to = 'Tom Brady'

          self.mock_bug.setter = 'Tom Brady'

          mod_date = Mock()

-         mod_date.value = datetime.utcnow().strftime('%Y%m%dT%H:%M:%S')

+         mod_date.value = datetime.now(timezone.utc).replace(tzinfo=None).strftime('%Y%m%dT%H:%M:%S')

          self.mock_bug.flags = [{

              'status': '+',

              'name': 'fedora-review',
@@ -804,7 +804,7 @@ 

          """Tests request-repo errors when the bug was approved over 60 days ago

          """

          self.mock_bug.flags[0]['modification_date'].value = \

-             (datetime.utcnow() - timedelta(days=75)).strftime(

+             (datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=75)).strftime(

                  '%Y%m%dT%H:%M:%S')

          mock_bz.getbug.return_value = self.mock_bug

          cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path,
@@ -1968,7 +1968,7 @@ 

      @patch('fedpkg.BodhiClient.send_request')

      def test_extend_override_by_days(

              self, send_request, csrf, list_overrides):

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc).replace(tzinfo=None)

          override_expiration_date = utcnow + timedelta(days=7)

          build_nvr = 'somepkg-1.54-2.fc28'

          build_override = {
@@ -2021,7 +2021,7 @@ 

      @patch('fedpkg.BodhiClient.send_request')

      def test_extend_override_by_specific_date(

              self, send_request, csrf, list_overrides):

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc)

          override_expiration_date = utcnow + timedelta(days=7)

          build_nvr = 'somepkg-1.54-2.fc28'

          build_override = {
@@ -2078,7 +2078,7 @@ 

      @freeze_time('2018-06-08 16:17:30')

      def test_extend_for_expired_override(

              self, send_request, csrf, list_overrides):

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc).replace(tzinfo=None)

          # Make override expired

          override_expiration_date = utcnow - timedelta(days=7)

          build_nvr = 'somepkg-1.54-2.fc28'
@@ -2136,7 +2136,7 @@ 

              self, extend_override, csrf, list_overrides):

          extend_override.side_effect = Exception

  

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc).replace(tzinfo=None)

          override_expiration_date = utcnow + timedelta(days=7)

          build_nvr = 'somepkg-1.54-2.fc28'

  
@@ -2171,7 +2171,7 @@ 

      @patch('fedpkg.BodhiClient.send_request')

      def test_retry_to_extend_override_by_days(

              self, send_request, csrf, list_overrides):

-         utcnow = datetime.utcnow()

+         utcnow = datetime.now(timezone.utc).replace(tzinfo=None)

          override_expiration_date = utcnow + timedelta(days=7)

  

          from fedora.client import AuthError

Replaces datetime.utcnow() with datetime.now(timezone.utc).replace(tzinfo=None) to replace the deprecated function[0] and keep the current code behaviour.

I decided to use .replace(tzinfo=None) and leave the date variables "timezone naive" to maintain compatibility with the rest of the variables. Another option was to change other variables to be "timezone aware", but external communication already uses datetime objects without timezone information. changing this would mean changing some tests too.

This reduces the number of warnings on py312 from 47 to 5.

[0] https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

@onosek would you mind taking a look at this PR and providing feedback?

Is there anyone available to review this pull request?

rebased onto a5eb5fb

21 days ago

So far I can see the issue, that importing timezone fails on ancient python2.7, which was still used by fedpkg on epel7.

Based on the discussion here https://pagure.io/epel/issue/238
I could drop python2.7 support soon/now.

rebased onto 9cb735a

21 days ago

With merged https://pagure.io/fedpkg/pull-request/547
tests pass and I can accept it. Thanks.

Pull-Request has been merged by onosek

21 days ago