Skip to content

Commit

Permalink
Make @deprecate log a warning (mlflow#1569)
Browse files Browse the repository at this point in the history
* Make deprecations actually log a warning

* Updating stack level

* Warn only once and on function usage

* Setting warning filter

* Add in-line comment
  • Loading branch information
apurva-koti committed Jul 16, 2019
1 parent c17f22a commit f49d253
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
7 changes: 4 additions & 3 deletions mlflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
For a lower level API, see the :py:mod:`mlflow.tracking` module.
"""

from mlflow.version import VERSION as __version__
from mlflow.utils.logging_utils import _configure_mlflow_loggers
import mlflow.tracking.fluent

# Filter annoying Cython warnings that serve no good purpose, and so before
# importing other modules.
# See: https://github.com/numpy/numpy/pull/432/commits/170ed4e33d6196d7
import warnings
warnings.filterwarnings("ignore", message="numpy.dtype size changed") # noqa: E402
warnings.filterwarnings("ignore", message="numpy.ufunc size changed") # noqa: E402
# log a deprecated warning only once per function per module
warnings.filterwarnings("module", category=DeprecationWarning)

# pylint: disable=wrong-import-position
import mlflow.projects as projects # noqa
import mlflow.tracking as tracking # noqa
import mlflow.tracking.fluent
from mlflow.utils.logging_utils import _configure_mlflow_loggers

_configure_mlflow_loggers(root_module_name=__name__)

Expand Down
2 changes: 1 addition & 1 deletion mlflow/pyfunc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def load_model(model_uri, suppress_warnings=False):
return load_pyfunc(model_uri, suppress_warnings)


@deprecated("pyfunc.load_model", 1.0)
@deprecated("mlflow.pyfunc.load_model", 1.0)
def load_pyfunc(model_uri, suppress_warnings=False):
"""
Load a model stored in Python function format.
Expand Down
27 changes: 21 additions & 6 deletions mlflow/utils/annotations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from functools import wraps


Expand All @@ -21,15 +22,29 @@ def deprecated(alternative=None, since=None):
:param func: A function to mark
:returns Decorated function.
"""
def deprecated_func(func):

def deprecated_decorator(func):
since_str = " since %s" % since if since else ""
notice = ".. Warning:: Deprecated%s: This method will be removed in " % since_str + \
"a near future release."
notice = (
".. Warning:: ``{function_name}`` is deprecated{since_string}. This method will be"
" removed in a near future release".format(
function_name='.'.join([func.__module__, func.__name__]),
since_string=since_str)
)
if alternative is not None and alternative.strip():
notice += " Use ``%s`` instead." % alternative
func.__doc__ = notice + "\n" + func.__doc__
return func
return deprecated_func

@wraps(func)
def deprecated_func(*args, **kwargs):
warnings.warn(notice, category=DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)

if func.__doc__ is not None:
deprecated_func.__doc__ = notice + "\n" + func.__doc__

return deprecated_func

return deprecated_decorator


def keyword_only(func):
Expand Down

0 comments on commit f49d253

Please sign in to comment.