Git Product home page Git Product logo

click_params's People

Contributors

dependabot[bot] avatar gutzbenj avatar laundmo avatar lewoudar avatar saroad2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

click_params's Issues

`ListParamType` should treat empty strings as empty list

Description

When ListParamType try to convert an empty string it creates a list with one empty item.
The more logical use case is that empty string should be converted to an empty list.

Code example

import click
import click_params

my_list = click.prompt(
    "Strings list",
    default="",
    type=click_params.StringListParamType(),
    show_default=False,
)
print(my_list)

Output:

Strings list: 
['']

My suggestion

I would like to suggest a boolean parameter for ListParamType called ignore_empty that will do just that, handle empty string as empty list. This parameter will be false by default in order to keep backward compatibility.

In the example above, the code will be changed to:

my_list = click.prompt(
    "Strings list",
    default="",
    type=click_params.StringListParamType(ignore_empty=True),
    show_default=False,
)
print(my_list)

And the output will be:

Strings list: 
[]

Drop python 3.6 support

Description

On December 23rd 2021 Python 3.6 has reach its End-of-Life date.
Therefore click-params should no longer support that version.

StringListParamType interference when running cli multiple times in test suite

Dear all, dear @lewoudar ,

this issue is meant to show some issue that we ran into within our test suite for wetterdienst at [1].

The test suite also tests our click based cli, which also leverages click_params StringListParamType at multiple points. We noticed that when running the cli with multiple pytest parametrized arguments, the first run/test may succeed but the second/third/... run would fail. This is because the cli is instantiated once and then uses the same instance of StringListParamType. There however at

def convert(self, value, param, ctx):
# For an unknown reason, when a user is prompted for a value using "prompt" argument from click.option
# the convert method seems to be called more than once, so this is necessary to avoid an error
# when calling self._strip_separator below since the value passed will be already converted to a list
if self._convert_called:
return value
if self._ignore_empty and value == "":
return []
value = self._strip_separator(value)
errors, converted_list = self._convert_expression_to_list(value)
if errors:
self.fail(self._error_message.format(errors=errors), param, ctx)
self._convert_called = True
return converted_list

we notice a attribute, that once the param type is used is changed and in the second/x run of that param type it will behave differently.

This should not effect the cli in running environment because there we only call the cli once, the cli is instantiated and afterwards it is shut down. However in the testing environment this may not be the case as multiple args could be tested against the same cli option/argument.

An example PR #23 (that atm fails as the current behaviour of StringListParamType is not adapted) shows this issue quite well.

We propose the following change for the above shown attribute:

def convert(self, value, param, ctx):
    if isinstance(value, list):
        return value

    if self._ignore_empty and value == "":
        return []
    value = self._strip_separator(value)
    errors, converted_list = self._convert_expression_to_list(value)
    if errors:
        self.fail(self._error_message.format(errors=errors), param, ctx)

    return converted_list
  • isinstance would recognize if instead of a string a list would be passed
  • a list would mean that the original value of type string has already been modified and the conversion would not be required anymore
  • thus the value could be returned directly
  • no double parsing done anymore

Please let us know what you think about this adaption!

Sincerely,
Benjamin

/CC @amotl

[1] https://github.com/earthobservations/wetterdienst

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.