diff --git a/src/tribler/core/sentry_reporter/sentry_reporter.py b/src/tribler/core/sentry_reporter/sentry_reporter.py index f6bb6fe086..599baca99f 100644 --- a/src/tribler/core/sentry_reporter/sentry_reporter.py +++ b/src/tribler/core/sentry_reporter/sentry_reporter.py @@ -349,7 +349,7 @@ def _format_breadcrumbs(self, event: Optional[Dict]): if breadcrumbs := event.get(BREADCRUMBS): breadcrumbs_values = breadcrumbs[VALUES] breadcrumbs_values = distinct_by(breadcrumbs_values) - breadcrumbs_values = order_by_utc_time(breadcrumbs_values, key='timestamp') + breadcrumbs_values = order_by_utc_time(breadcrumbs_values) event[BREADCRUMBS][VALUES] = breadcrumbs_values except Exception as e: diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index bb02055e52..fe7a4ad535 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -61,8 +61,7 @@ def distinct_by(list_of_dict: Optional[List[Dict]], """This function removes all duplicates from a list of dictionaries. A duplicate here is a dictionary that have the same value of the given key. - If no key field is presented in the dictionary, then the dictionary will not be considered - as a duplicate. + If no key field is presented in the dictionary, then the exception will be raised. Args: list_of_dict: list of dictionaries @@ -75,22 +74,8 @@ def distinct_by(list_of_dict: Optional[List[Dict]], if not list_of_dict: return list_of_dict - seen = set() - result = [] - - for d in list_of_dict: - try: - value = getter(d) - except (KeyError, TypeError): - result.append(d) - continue - - if value not in seen: - result.append(d) - - seen.add(value) - - return result + distinct = dict((getter(d), d) for d in list_of_dict) + return [value for _, value in distinct.items()] def format_version(version: Optional[str]) -> Optional[str]: diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py index 81fc40c37b..6a90b7d44b 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_tools.py @@ -85,21 +85,12 @@ def test_distinct_default(): def test_distinct_key_error(): - # Test distinct_by with missing key, it should keep the items + # Test distinct_by with missing key in getter values = [ {'message': 'message 1', }, - {'message': 'message 1', }, - {'timestamp': 'timestamp 1', }, - {'timestamp': 'timestamp 1', }, ] - - expected = [ - {'message': 'message 1', }, - {'message': 'message 1', }, - {'timestamp': 'timestamp 1', }, - {'timestamp': 'timestamp 1', }, - ] - assert distinct_by(values) == expected + with pytest.raises(KeyError): + distinct_by(values) def test_distinct_not_default(): @@ -112,8 +103,8 @@ def test_distinct_not_default(): ] expected = [ - {'message': 'message 1', 'timestamp': 'timestamp 1'}, - {'message': 'message 2', 'timestamp': 'timestamp 2'}, + {'message': 'message 3', 'timestamp': 'timestamp 1'}, + {'message': 'message 4', 'timestamp': 'timestamp 2'} ] assert distinct_by(values, lambda b: b['timestamp']) == expected @@ -121,8 +112,8 @@ def test_distinct_not_default(): def test_distinct_none_in_list(): # Test distinct_by with None in list values = [None] - expected = [None] - assert distinct_by(values) == expected + with pytest.raises(TypeError): + distinct_by(values) FORMATTED_VERSIONS = [