From 8ab47fdc25294aef5746ef229f97cd8fc444dd7a Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Sat, 3 Dec 2022 22:17:48 +0100 Subject: [PATCH 1/2] FIX make sure we can execute code with python -OO --- doc/whats_new/v0.10.rst | 7 +++++++ imblearn/utils/_docstring.py | 3 ++- imblearn/utils/tests/test_docstring.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/whats_new/v0.10.rst b/doc/whats_new/v0.10.rst index 3e75e0142..5dc47c93f 100644 --- a/doc/whats_new/v0.10.rst +++ b/doc/whats_new/v0.10.rst @@ -6,6 +6,13 @@ Version 0.10.0 (ongoing) Changelog --------- +Bug fixes +......... + +- Make sure that :class:`~imblearn.utils._docstring.Substitution` is + working with `python -OO` that replace `__doc__` by `None`. + :pr:`xxx` bu :user:`Guillaume Lemaitre `. + Compatibility ............. diff --git a/imblearn/utils/_docstring.py b/imblearn/utils/_docstring.py index d03be3740..61921c3f3 100644 --- a/imblearn/utils/_docstring.py +++ b/imblearn/utils/_docstring.py @@ -19,7 +19,8 @@ def __init__(self, *args, **kwargs): self.params = args or kwargs def __call__(self, obj): - obj.__doc__ = obj.__doc__.format(**self.params) + if obj.__doc__: + obj.__doc__ = obj.__doc__.format(**self.params) return obj diff --git a/imblearn/utils/tests/test_docstring.py b/imblearn/utils/tests/test_docstring.py index acfa1e162..0109fdb31 100644 --- a/imblearn/utils/tests/test_docstring.py +++ b/imblearn/utils/tests/test_docstring.py @@ -66,3 +66,17 @@ def test_docstring_inject(obj, obj_docstring): def test_docstring_template(): assert "random_state" in _random_state_docstring assert "n_jobs" in _n_jobs_docstring + + +def test_docstring_with_python_OO(): + """Check that we don't raise a warning if the code is executed with -OO. + + Non-regression test for: + https://github.com/scikit-learn-contrib/imbalanced-learn/issues/945 + """ + instance = cls(param_1="xxx", param_2="yyy") + instance.__doc__ = None # simulate -OO + + instance = Substitution(param_1="xxx", param_2="yyy")(instance) + + assert instance.__doc__ is None From c5f87a898f183d4db736fa4a8e3c83a90277fb37 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Sat, 3 Dec 2022 22:18:42 +0100 Subject: [PATCH 2/2] update pr number --- doc/whats_new/v0.10.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats_new/v0.10.rst b/doc/whats_new/v0.10.rst index 5dc47c93f..8a6f647f8 100644 --- a/doc/whats_new/v0.10.rst +++ b/doc/whats_new/v0.10.rst @@ -11,7 +11,7 @@ Bug fixes - Make sure that :class:`~imblearn.utils._docstring.Substitution` is working with `python -OO` that replace `__doc__` by `None`. - :pr:`xxx` bu :user:`Guillaume Lemaitre `. + :pr:`953` bu :user:`Guillaume Lemaitre `. Compatibility .............