Skip to content

Commit

Permalink
Avoid using deprecated flask.__version__
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Dec 4, 2023
1 parent 563063f commit 7bfc823
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 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

* Avoid using deprecated `flask.__version__` attribute
[#365](https://github.com/bugsnag/bugsnag-python/pull/365)

## v4.6.0 (2023-09-05)

### Enhancements
Expand Down
13 changes: 10 additions & 3 deletions bugsnag/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bugsnag.wsgi import request_path
from bugsnag.legacy import _auto_leave_breadcrumb
from bugsnag.breadcrumbs import BreadcrumbType
from bugsnag.utils import remove_query_from_url
from bugsnag.utils import remove_query_from_url, get_package_version


__all__ = ('handle_exceptions',)
Expand Down Expand Up @@ -36,9 +36,16 @@ def add_flask_request_to_notification(event: bugsnag.Event):


def handle_exceptions(app):
middleware = bugsnag.configure().internal_middleware
bugsnag.configure().runtime_versions['flask'] = flask.__version__
configuration = bugsnag.configure()

version = get_package_version("flask")

if version is not None:
configuration.runtime_versions["flask"] = version

middleware = configuration.internal_middleware
middleware.before_notify(add_flask_request_to_notification)

flask.got_request_exception.connect(__log_exception, app)
flask.request_started.connect(_on_request_started, app)

Expand Down
17 changes: 17 additions & 0 deletions bugsnag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,20 @@ def to_rfc3339(dt: datetime) -> str:
int(dt.microsecond / 1000),
_get_timezone_offset(dt)
)


def get_package_version(package_name: str) -> Optional[str]:
try:
from importlib import metadata

return metadata.version(package_name) # type: ignore
except ImportError:
try:
import pkg_resources
except ImportError:
return None

try:
return pkg_resources.get_distribution(package_name).version
except pkg_resources.DistributionNotFound:
return None
12 changes: 5 additions & 7 deletions tests/integrations/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,14 @@ def hello():
handle_exceptions(app)
app.test_client().get('/hello')

self.assertEqual(len(self.server.received), 1)
assert self.server.sent_report_count == 1

payload = self.server.received[0]['json_body']
device_data = payload['events'][0]['device']
versions = payload['events'][0]['device']['runtimeVersions']

self.assertEquals(len(device_data['runtimeVersions']), 2)
self.assertTrue(re.match(r'\d+\.\d+\.\d+',
device_data['runtimeVersions']['python']))
self.assertTrue(re.match(r'\d+\.\d+\.\d+',
device_data['runtimeVersions']['flask']))
assert re.match(r'^\d+\.\d+\.\d+$', versions['python'])
assert re.match(r'^\d+\.\d+\.\d+$', versions['flask'])
assert len(versions) == 2

def test_read_request_in_callback(self):
def callback(event):
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ deps=
exceptiongroup: exceptiongroup
lint: flake8
lint: mypy
lint: types-pkg_resources
lint: types-requests
lint: types-Flask
lint: types-contextvars
Expand Down

0 comments on commit 7bfc823

Please sign in to comment.