diff options
author | David Lord <davidism@gmail.com> | 2022-03-19 11:30:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-19 11:30:33 -0700 |
commit | d251cb0abc9b0dbda2402d4d831c18718cfb51bf (patch) | |
tree | 17863979ec71e96da227dc9fcdee57e66229638c | |
parent | 19be092b6db4e4300e31906498e354ec0adf870c (diff) | |
parent | e0033315510510acdba14e880a361ccca091fb4b (diff) | |
download | click-d251cb0abc9b0dbda2402d4d831c18718cfb51bf.tar.gz |
Merge pull request #2219 from pallets/paramtype-name
fix ParamType.to_info_dict() with no name
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | docs/shell-completion.rst | 2 | ||||
-rw-r--r-- | src/click/types.py | 9 | ||||
-rw-r--r-- | tests/test_info_dict.py | 7 |
4 files changed, 19 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 0a9661a..91b9ec1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -48,6 +48,8 @@ Version 8.1.0 - A ``Group`` with ``invoke_without_command=True`` and ``chain=False`` will invoke its result callback with the group function's return value. :issue:`2124` +- ``to_info_dict`` will not fail if a ``ParamType`` doesn't define a + ``name``. :issue:`2168` Version 8.0.4 diff --git a/docs/shell-completion.rst b/docs/shell-completion.rst index 699d25d..ebf73c3 100644 --- a/docs/shell-completion.rst +++ b/docs/shell-completion.rst @@ -132,6 +132,8 @@ with the incomplete value. .. code-block:: python class EnvVarType(ParamType): + name = "envvar" + def shell_complete(self, ctx, param, incomplete): return [ CompletionItem(name) diff --git a/src/click/types.py b/src/click/types.py index aab0656..3a78a3a 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -63,7 +63,14 @@ class ParamType: # The class name without the "ParamType" suffix. param_type = type(self).__name__.partition("ParamType")[0] param_type = param_type.partition("ParameterType")[0] - return {"param_type": param_type, "name": self.name} + + # Custom subclasses might not remember to set a name. + if hasattr(self, "name"): + name = self.name + else: + name = param_type + + return {"param_type": param_type, "name": name} def __call__( self, diff --git a/tests/test_info_dict.py b/tests/test_info_dict.py index b58ad6e..79d39ee 100644 --- a/tests/test_info_dict.py +++ b/tests/test_info_dict.py @@ -266,3 +266,10 @@ def test_context(): "ignore_unknown_options": False, "auto_envvar_prefix": None, } + + +def test_paramtype_no_name(): + class TestType(click.ParamType): + pass + + assert TestType().to_info_dict()["name"] == "TestType" |