Skip to content

Commit

Permalink
Merge pull request #2219 from pallets/paramtype-name
Browse files Browse the repository at this point in the history
fix ParamType.to_info_dict() with no name
  • Loading branch information
davidism committed Mar 19, 2022
2 parents 19be092 + e003331 commit d251cb0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/shell-completion.rst
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion src/click/types.py
Expand Up @@ -63,7 +63,14 @@ def to_info_dict(self) -> t.Dict[str, t.Any]:
# 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,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_info_dict.py
Expand Up @@ -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"

0 comments on commit d251cb0

Please sign in to comment.