Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace utcnow calls #394

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading