Skip to content
Myr

upgrading.rst

This file

Path

docs/upgrading.rst

100 lines · 3.7 KiB

Last changed by

Not known.

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-3.2:1011Upgrading to 3.212----------------1314Click 3.2 had to perform two changes to multi commands which were15triggered by a change between Click 2 and Click 3 that had bigger16consequences than anticipated.1718Context Invokes19```````````````2021Click 3.2 contains a fix for the :meth:`Context.invoke` function when used22with other commands.  The original intention of this function was to23invoke the other command as as if it came from the command line when it24was passed a context object instead of a function.  This use was only25documented in a single place in the documentation before and there was no26proper explanation for the method in the API documentation.2728The core issue is that before 3.2 this call worked against intentions::2930    ctx.invoke(other_command, 'arg1', 'arg2')3132This was never intended to work as it does not allow Click to operate on33the parameters.  Given that this pattern was never documented and ill34intended the decision was made to change this behavior in a bugfix release35before it spreads by accident and developers depend on it.3637The correct invocation for the above command is the following::3839    ctx.invoke(other_command, name_of_arg1='arg1', name_of_arg2='arg2')4041This also allowed us to fix the issue that defaults were not handled42properly by this function.4344Multicommand Chaining API45`````````````````````````4647Click 3 introduced multicommand chaining.  This required a change in how48Click internally dispatches.  Unfortunately this change was not correctly49implemented and it appeared that it was possible to provide an API that50can inform the super command about all the subcommands that will be51invoked.5253This assumption however does not work with one of the API guarantees that54have been given in the past.  As such this functionality has been removed55in 3.2 as it was already broken.  Instead the accidentally broken56functionality of the :attr:`Context.invoked_subcommand` attribute was57restored.5859If you do require the know which exact commands will be invoked there are60different ways to cope with this.  The first one is to let the subcommands61all return functions and then to invoke the functions in a62:meth:`Context.resultcallback`.636465.. _upgrade-to-2.0:6667Upgrading to 2.068----------------6970Click 2.0 has one breaking change which is the signature for parameter71callbacks.  Before 2.0, the callback was invoked with ``(ctx, value)``72whereas now it's ``(ctx, param, value)``.  This change was necessary as it73otherwise made reusing callbacks too complicated.7475To ease the transition Click will still accept old callbacks.  Starting76with Click 3.0 it will start to issue a warning to stderr to encourage you77to upgrade.7879In case you want to support both Click 1.0 and Click 2.0, you can make a80simple decorator that adjusts the signatures::8182    import click83    from functools import update_wrapper8485    def compatcallback(f):86        # Click 1.0 does not have a version string stored, so we need to87        # use getattr here to be safe.88        if getattr(click, '__version__', '0.0') >= '2.0':89            return f90        return update_wrapper(lambda ctx, value: f(ctx, None, value), f)9192With that helper you can then write something like this::9394    @compatcallback95    def callback(ctx, param, value):96        return value.upper()9798Note that because Click 1.0 did not pass a parameter, the `param` argument99here would be `None`, so a compatibility callback could not use that100argument.