From 8508c242c9172c467865f10adab10da9db98d5a3 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 5 Nov 2024 16:29:53 +0000 Subject: [PATCH 1/3] replace utcnow calls --- bugsnag/utils.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/bugsnag/utils.py b/bugsnag/utils.py index c2e1a857..5a18be3e 100644 --- a/bugsnag/utils.py +++ b/bugsnag/utils.py @@ -4,6 +4,7 @@ from threading import local as threadlocal from typing import AnyStr, Tuple, Optional import warnings +import sys import copy import logging from datetime import datetime, timedelta @@ -422,14 +423,13 @@ def remove_query_from_url(url: AnyStr) -> Optional[AnyStr]: # milliseconds precision # Python can do this natively from version 3.6, but we need to include a # fallback implementation for Python 3.5 -try: - # this will raise if 'timespec' isn't supported - datetime.utcnow().isoformat(timespec='milliseconds') # type: ignore - +if sys.version_info >= (3, 6): + # Python 3.6+ has a built-in method for this def to_rfc3339(dt: datetime) -> str: return dt.isoformat(timespec='milliseconds') # type: ignore -except Exception: +else: + # Python 3.5 fallback implementation def _get_timezone_offset(dt: datetime) -> str: if dt.tzinfo is None: return '' @@ -464,17 +464,16 @@ def to_rfc3339(dt: datetime) -> str: def get_package_version(package_name: str) -> Optional[str]: - try: - from importlib import metadata - - return metadata.version(package_name) # type: ignore - except ImportError: + if sys.version_info >= (3, 8): try: - import pkg_resources - except ImportError: + from importlib import metadata + return metadata.version(package_name) # type: ignore + except metadata.PackageNotFoundError: return None - + else: try: - return pkg_resources.get_distribution(package_name).version - except pkg_resources.DistributionNotFound: + import pkg_resources # type: ignore + return pkg_resources.get_distribution( + package_name).version # type: ignore + except (ImportError, pkg_resources.DistributionNotFound): return None From fd9b285580267d89656b63124dd794088e748d5c Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 11 Nov 2024 16:25:42 +0000 Subject: [PATCH 2/3] conditionally add pkg_resources --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 325d6b3d..38e2519d 100644 --- a/tox.ini +++ b/tox.ini @@ -64,7 +64,7 @@ deps= exceptiongroup: exceptiongroup lint: flake8 lint: mypy - lint: types-pkg_resources + lint: types-pkg_resources; python_version < '3.12' lint: types-requests lint: types-Flask lint: types-contextvars From 09c0f411bd72fc209c8fed5612ad7146c50b7695 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 11 Nov 2024 16:44:36 +0000 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a856b6d3..de416556 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog ========= +## TBD + +### Enhancements + +* Remove depricated `datetime.utcnow()` method call from utils class + [#394](https://github.com/bugsnag/bugsnag-python/pull/394). + ## v4.7.1 (2024-05-22) ### Bug fixes