changeset 34947:ff2110eadbfa stable

configitems: relax warning about unwanted default value The original condition was a bit harsh for extension authors since third-party extensions need to preserve compatibility with older Mercurial versions, where no defaults would be loaded from the configtable. So let's silence the warning if the given default value matches, which should be harmless.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 31 Oct 2017 22:37:30 +0900
parents 5caae380ff90
children ff178743e59b
files mercurial/ui.py tests/test-devel-warnings.t
diffstat 2 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ui.py	Thu Oct 26 11:07:06 2017 -0700
+++ b/mercurial/ui.py	Tue Oct 31 22:37:30 2017 +0900
@@ -471,12 +471,16 @@
         return value
 
     def _config(self, section, name, default=_unset, untrusted=False):
-        value = default
+        value = itemdefault = default
         item = self._knownconfig.get(section, {}).get(name)
         alternates = [(section, name)]
 
         if item is not None:
             alternates.extend(item.alias)
+            if callable(item.default):
+                itemdefault = item.default()
+            else:
+                itemdefault = item.default
         else:
             msg = ("accessing unregistered config item: '%s.%s'")
             msg %= (section, name)
@@ -490,13 +494,12 @@
                 msg = "config item requires an explicit default value: '%s.%s'"
                 msg %= (section, name)
                 self.develwarn(msg, 2, 'warn-config-default')
-            elif callable(item.default):
-                    value = item.default()
             else:
-                value = item.default
+                value = itemdefault
         elif (item is not None
-              and item.default is not configitems.dynamicdefault):
-            msg = ("specifying a default value for a registered "
+              and item.default is not configitems.dynamicdefault
+              and default != itemdefault):
+            msg = ("specifying a mismatched default value for a registered "
                    "config item: '%s.%s' '%s'")
             msg %= (section, name, default)
             self.develwarn(msg, 2, 'warn-config-default')
--- a/tests/test-devel-warnings.t	Thu Oct 26 11:07:06 2017 -0700
+++ b/tests/test-devel-warnings.t	Tue Oct 31 22:37:30 2017 +0900
@@ -352,17 +352,21 @@
   > 
   > configitem('test', 'some', default='foo')
   > configitem('test', 'dynamic', default=configitems.dynamicdefault)
+  > configitem('test', 'callable', default=list)
   > # overwrite a core config
   > configitem('ui', 'quiet', default=False)
   > configitem('ui', 'interactive', default=None)
   > 
   > @command(b'buggyconfig')
   > def cmdbuggyconfig(ui, repo):
-  >     repo.ui.config('ui', 'quiet', False)
-  >     repo.ui.config('ui', 'interactive', None)
+  >     repo.ui.config('ui', 'quiet', True)
+  >     repo.ui.config('ui', 'interactive', False)
+  >     repo.ui.config('test', 'some', 'bar')
   >     repo.ui.config('test', 'some', 'foo')
   >     repo.ui.config('test', 'dynamic', 'some-required-default')
   >     repo.ui.config('test', 'dynamic')
+  >     repo.ui.config('test', 'callable', [])
+  >     repo.ui.config('test', 'callable', 'foo')
   >     repo.ui.config('test', 'unregistered')
   >     repo.ui.config('unregistered', 'unregistered')
   > EOF
@@ -370,10 +374,11 @@
   $ hg --config "extensions.buggyconfig=${TESTTMP}/buggyconfig.py" buggyconfig
   devel-warn: extension 'buggyconfig' overwrite config item 'ui.interactive' at: */mercurial/extensions.py:* (_loadextra) (glob)
   devel-warn: extension 'buggyconfig' overwrite config item 'ui.quiet' at: */mercurial/extensions.py:* (_loadextra) (glob)
-  devel-warn: specifying a default value for a registered config item: 'ui.quiet' 'False' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
-  devel-warn: specifying a default value for a registered config item: 'ui.interactive' 'None' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
-  devel-warn: specifying a default value for a registered config item: 'test.some' 'foo' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
+  devel-warn: specifying a mismatched default value for a registered config item: 'ui.quiet' 'True' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
+  devel-warn: specifying a mismatched default value for a registered config item: 'ui.interactive' 'False' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
+  devel-warn: specifying a mismatched default value for a registered config item: 'test.some' 'bar' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
   devel-warn: config item requires an explicit default value: 'test.dynamic' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
+  devel-warn: specifying a mismatched default value for a registered config item: 'test.callable' 'foo' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
   devel-warn: accessing unregistered config item: 'test.unregistered' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
   devel-warn: accessing unregistered config item: 'unregistered.unregistered' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)