[PATCH 2 of 4 V2] ui: add a 'deprecwarn' helper to issue deprecation warnings

Pierre-Yves David pierre-yves.david at ens-lyon.org
Mon Dec 7 13:33:50 CST 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1449385549 28800
#      Sat Dec 05 23:05:49 2015 -0800
# Node ID abc6aeab55afb3158a6b34b9ba1f8917835a3843
# Parent  37c8432ae7a8e7ac7c213c5dea472d5060e325e9
# EXP-Topic deprecationwarning
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r abc6aeab55af
ui: add a 'deprecwarn' helper to issue deprecation warnings

As discussed on the list, we are adding an official way to keep old API around
for a short time in order to help third party developer to catch up. The
deprecated API will issue developer warning (issued by default during test runs)
to warn extensions authors that they need to upgrade their code without
instantaneously breaking tool chains and normal users.

The version is passed as an explicit argument so that developer think about it
and a potential future script can automatically check for it.

This is not build as a decorator because accessing the 'ui' instance will likely
be different each time. The message is also free form because deprecated API are
replaced in a variety of ways. I'm not super happy about the final rendering of
that message, but this is a developer oriented warning and I would like to move
forward.

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1058,10 +1058,20 @@ class ui(object):
             curframe = inspect.currentframe()
             calframe = inspect.getouterframes(curframe, 2)
             self.write_err('%s at: %s:%s (%s)\n'
                            % ((msg,) + calframe[stacklevel][1:4]))
 
+    def deprecwarn(self, msg, version):
+        """issue a deprecation warning
+
+        - msg: message explaining what is deprecated and how to upgrade,
+        - version: last version where the API will be supported,
+        """
+        msg += ("\n(compatibility will be dropped after Mercurial-%s,"
+                " update your code.)") % version
+        self.develwarn(msg, stacklevel=2)
+
 class paths(dict):
     """Represents a collection of paths and their configs.
 
     Data is initially derived from ui instances and the config files they have
     loaded.
diff --git a/tests/test-devel-warnings.t b/tests/test-devel-warnings.t
--- a/tests/test-devel-warnings.t
+++ b/tests/test-devel-warnings.t
@@ -45,10 +45,16 @@
   >     tr = repo.transaction('foobar')
   >     try:
   >         repair.strip(repo.ui, repo, [repo['.'].node()])
   >     finally:
   >         lo.release()
+  > @command('oldanddeprecated', [], '')
+  > def oldanddeprecated(ui, repo):
+  >     """test deprecation warning API"""
+  >     def foobar(ui):
+  >         ui.deprecwarn('foorbar is deprecated, go shopping', '42.1337')
+  >     foobar(ui)
   > 
   > def oldstylerevset(repo, subset, x):
   >     return list(subset)
   > 
   > revset.symbols['oldstyle'] = oldstylerevset
@@ -112,7 +118,24 @@
   [255]
 
   $ hg log -r "oldstyle()" -T '{rev}\n'
   devel-warn: revset "oldstyle" use list instead of smartset, (upgrade your code) at: */mercurial/revset.py:* (mfunc) (glob)
   0
+  $ hg oldanddeprecated
+  devel-warn: foorbar is deprecated, go shopping
+  (compatibility will be dropped after Mercurial-42.1337, update your code.) at: $TESTTMP/buggylocking.py:53 (oldanddeprecated)
 
+  $ hg oldanddeprecated --traceback
+  devel-warn: foorbar is deprecated, go shopping
+  (compatibility will be dropped after Mercurial-42.1337, update your code.) at:
+   */hg:* in <module> (glob)
+   */mercurial/dispatch.py:* in run (glob)
+   */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _runcatch (glob)
+   */mercurial/dispatch.py:* in _dispatch (glob)
+   */mercurial/dispatch.py:* in runcommand (glob)
+   */mercurial/dispatch.py:* in _runcommand (glob)
+   */mercurial/dispatch.py:* in checkargs (glob)
+   */mercurial/dispatch.py:* in <lambda> (glob)
+   */mercurial/util.py:* in check (glob)
+   $TESTTMP/buggylocking.py:* in oldanddeprecated (glob)
   $ cd ..


More information about the Mercurial-devel mailing list