Skip to content

Commit 55e52dd

Browse files
authored
Merge pull request #394 from bugsnag/utcnow-deprecated
replace utcnow calls
2 parents 9fd6a13 + ed8ad0e commit 55e52dd

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
## TBD
5+
6+
### Enhancements
7+
8+
* Remove depricated `datetime.utcnow()` method call from utils class
9+
[#394](https://github.com/bugsnag/bugsnag-python/pull/394).
10+
411
## v4.7.1 (2024-05-22)
512

613
### Bug fixes

bugsnag/utils.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from threading import local as threadlocal
55
from typing import AnyStr, Tuple, Optional
66
import warnings
7+
import sys
78
import copy
89
import logging
910
from datetime import datetime, timedelta
@@ -422,14 +423,13 @@ def remove_query_from_url(url: AnyStr) -> Optional[AnyStr]:
422423
# milliseconds precision
423424
# Python can do this natively from version 3.6, but we need to include a
424425
# fallback implementation for Python 3.5
425-
try:
426-
# this will raise if 'timespec' isn't supported
427-
datetime.utcnow().isoformat(timespec='milliseconds') # type: ignore
428-
426+
if sys.version_info >= (3, 6):
427+
# Python 3.6+ has a built-in method for this
429428
def to_rfc3339(dt: datetime) -> str:
430429
return dt.isoformat(timespec='milliseconds') # type: ignore
431430

432-
except Exception:
431+
else:
432+
# Python 3.5 fallback implementation
433433
def _get_timezone_offset(dt: datetime) -> str:
434434
if dt.tzinfo is None:
435435
return ''
@@ -464,17 +464,16 @@ def to_rfc3339(dt: datetime) -> str:
464464

465465

466466
def get_package_version(package_name: str) -> Optional[str]:
467-
try:
468-
from importlib import metadata
469-
470-
return metadata.version(package_name) # type: ignore
471-
except ImportError:
467+
if sys.version_info >= (3, 8):
472468
try:
473-
import pkg_resources
474-
except ImportError:
469+
from importlib import metadata
470+
return metadata.version(package_name) # type: ignore
471+
except metadata.PackageNotFoundError:
475472
return None
476-
473+
else:
477474
try:
478-
return pkg_resources.get_distribution(package_name).version
479-
except pkg_resources.DistributionNotFound:
475+
import pkg_resources # type: ignore
476+
return pkg_resources.get_distribution(
477+
package_name).version # type: ignore
478+
except (ImportError, pkg_resources.DistributionNotFound):
480479
return None

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ deps=
6565
exceptiongroup: exceptiongroup
6666
lint: flake8
6767
lint: mypy
68+
lint: types-pkg_resources; python_version < '3.12'
6869
lint: types-setuptools
6970
lint: types-requests
7071
lint: types-Flask

0 commit comments

Comments
 (0)