Skip to content
Myr

upgrading.rst

This file

Path

docs/upgrading.rst

131 lines · 4.6 KiB

Last changed by

Merge branch '7.x'David Lordbefore adoption8b971f7

History of this file

Take it

1Upgrading To Newer Releases2===========================34Click attempts the highest level of backwards compatibility but sometimes5this is not entirely possible.  In case we need to break backwards6compatibility this document gives you information about how to upgrade or7handle backwards compatibility properly.89.. _upgrade-to-7.0:1011Upgrading to 7.012----------------1314Commands that take their name from the decorated function now replace15underscores with dashes. For example, the Python function ``run_server``16will get the command name ``run-server`` now. There are a few options17to address this:1819-   To continue with the new behavior, pin your dependency to20    ``Click>=7`` and update any documentation to use dashes.21-   To keep existing behavior, add an explicit command name with22    underscores, like ``@click.command("run_server")``.23-   To try a name with dashes if the name with underscores was not24    found, pass a ``token_normalize_func`` to the context:2526    .. code-block:: python2728        def normalize(name):29            return name.replace("_", "-")3031        @click.group(context_settings={"token_normalize_func": normalize})32        def group():33            ...3435        @group.command()36        def run_server():37            ...383940.. _upgrade-to-3.2:4142Upgrading to 3.243----------------4445Click 3.2 had to perform two changes to multi commands which were46triggered by a change between Click 2 and Click 3 that had bigger47consequences than anticipated.4849Context Invokes50```````````````5152Click 3.2 contains a fix for the :meth:`Context.invoke` function when used53with other commands.  The original intention of this function was to54invoke the other command as as if it came from the command line when it55was passed a context object instead of a function.  This use was only56documented in a single place in the documentation before and there was no57proper explanation for the method in the API documentation.5859The core issue is that before 3.2 this call worked against intentions::6061    ctx.invoke(other_command, 'arg1', 'arg2')6263This was never intended to work as it does not allow Click to operate on64the parameters.  Given that this pattern was never documented and ill65intended the decision was made to change this behavior in a bugfix release66before it spreads by accident and developers depend on it.6768The correct invocation for the above command is the following::6970    ctx.invoke(other_command, name_of_arg1='arg1', name_of_arg2='arg2')7172This also allowed us to fix the issue that defaults were not handled73properly by this function.7475Multicommand Chaining API76`````````````````````````7778Click 3 introduced multicommand chaining.  This required a change in how79Click internally dispatches.  Unfortunately this change was not correctly80implemented and it appeared that it was possible to provide an API that81can inform the super command about all the subcommands that will be82invoked.8384This assumption however does not work with one of the API guarantees that85have been given in the past.  As such this functionality has been removed86in 3.2 as it was already broken.  Instead the accidentally broken87functionality of the :attr:`Context.invoked_subcommand` attribute was88restored.8990If you do require the know which exact commands will be invoked there are91different ways to cope with this.  The first one is to let the subcommands92all return functions and then to invoke the functions in a93:meth:`Context.resultcallback`.949596.. _upgrade-to-2.0:9798Upgrading to 2.099----------------100101Click 2.0 has one breaking change which is the signature for parameter102callbacks.  Before 2.0, the callback was invoked with ``(ctx, value)``103whereas now it's ``(ctx, param, value)``.  This change was necessary as it104otherwise made reusing callbacks too complicated.105106To ease the transition Click will still accept old callbacks.  Starting107with Click 3.0 it will start to issue a warning to stderr to encourage you108to upgrade.109110In case you want to support both Click 1.0 and Click 2.0, you can make a111simple decorator that adjusts the signatures::112113    import click114    from functools import update_wrapper115116    def compatcallback(f):117        # Click 1.0 does not have a version string stored, so we need to118        # use getattr here to be safe.119        if getattr(click, '__version__', '0.0') >= '2.0':120            return f121        return update_wrapper(lambda ctx, value: f(ctx, None, value), f)122123With that helper you can then write something like this::124125    @compatcallback126    def callback(ctx, param, value):127        return value.upper()128129Note that because Click 1.0 did not pass a parameter, the `param` argument130here would be `None`, so a compatibility callback could not use that131argument.