Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ParamType.to_info_dict() with no name #2219

Merged
merged 1 commit into from Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"