[PATCH 1 of 2] patchbomb: prompt: always return a str, even when ui is non-interactive

Greg Ward greg-hg at gerg.ca
Wed Jul 15 08:18:07 CDT 2009


On Tue, Jul 14, 2009 at 11:03 PM, Nicolas Dumazet<nicdumz at gmail.com> wrote:
> # HG changeset patch
> # User Nicolas Dumazet <nicdumz.commits at gmail.com>
> # Date 1247624826 -32400
> # Node ID 25c6fc40b705222cc9b7f88bd04e7c0638b7ad29
> # Parent  78b81646a2e4ae31e5c814b00be7cedf427a9c63
> patchbomb: prompt: always return a str, even when ui is non-interactive
>
> 'foo %s bar' % prompt(ui, ...) always work, even if prompt returns None.
> However the result of prompt() cannot be used directly in concatenations
> if it can be None. Let's be explicit.
>
> diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
> --- a/hgext/patchbomb.py
> +++ b/hgext/patchbomb.py
> @@ -76,7 +76,7 @@
>
>  def prompt(ui, prompt, default=None, rest=': ', empty_ok=False):
>     if not ui.interactive():
> -        return default
> +        return str(default)

But then you'll return the string "None", which is hardly ever useful.
 IMHO better to expect callers to check for None if they want to
concatenate.  (Interpolating None into a %s is rarely appropriate
either.  Any time I see a Python app say "blah blah: None blah blah",
I know I've just seen a minor bug.)

Greg



More information about the Mercurial-devel mailing list