Skip to content

Commit

Permalink
Merge pull request #394 from bugsnag/utcnow-deprecated
Browse files Browse the repository at this point in the history
replace utcnow calls
  • Loading branch information
clr182 authored Jan 13, 2025
2 parents 9fd6a13 + ed8ad0e commit 55e52dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 14 additions & 15 deletions bugsnag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ''
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ deps=
exceptiongroup: exceptiongroup
lint: flake8
lint: mypy
lint: types-pkg_resources; python_version < '3.12'
lint: types-setuptools
lint: types-requests
lint: types-Flask
Expand Down

0 comments on commit 55e52dd

Please sign in to comment.