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

Logging future warning #65

Merged
13 changes: 13 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Describe your changes

## Issue number

Closes #X

## Checklist before requesting a review

- [ ] Performed a self-review of my code
- [ ] Formatted my code with [`pkgmt format`](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#linting-formatting)
- [ ] Added [tests](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#testing) (when necessary).
- [ ] Added [docstring](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#documenting-changes-and-new-features) documentation and update the [changelog](https://ploomber-contributing.readthedocs.io/en/latest/contributing/pr.html#changelog) (when needed)

28 changes: 27 additions & 1 deletion src/ploomber_core/warnings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from warnings import warn
from ploomber_core.telemetry import telemetry


class PloomberDeprecationWarning(FutureWarning):
"""
Notes
Expand All @@ -6,4 +10,26 @@ class PloomberDeprecationWarning(FutureWarning):
Changed superclass from UserWarning to FutureWarning
"""

pass
# @
def __init__(self, message):
print("message: ", message)
self.message = message


def deprecation_warning(message, module_telemetry=None):
"""Raise deprecation warning message, also log to posthug if telemetry instance
is provided

Parameters
----------
message : str
The warning message displayed to the user
module_telemetry : Telemetry
The telemetry instance defined in ploomber_core.telemetry.Telemetry,
by default None
"""
warn(message, FutureWarning)
if module_telemetry and isinstance(module_telemetry, telemetry.Telemetry):
module_telemetry.log_api(
action="deprecation-warning-shown", metadata={"message": message}
)
25 changes: 25 additions & 0 deletions tests/test_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# import posthog

from unittest.mock import Mock
from ploomber_core.warnings import deprecation_warning
from ploomber_core.telemetry import telemetry as telemetry_module

import pytest


def test_deprecation_warning_w_posthug(monkeypatch):
# Initiate telemetry instance with mock_log_api
somepackage_telemetry = telemetry_module.Telemetry(
api_key="KEY", package_name="somepackage", version="0.1"
)
mock_log_api = Mock()
monkeypatch.setattr(somepackage_telemetry, "log_api", mock_log_api)

# To test if warning is shown
with pytest.warns(FutureWarning):
deprecation_warning("Test", somepackage_telemetry)

# To test if log_api is called
mock_log_api.assert_called_once_with(
action="deprecation-warning-shown", metadata={"message": "Test"}
)