Skip to content
Myr

Add documentation for DateTime param type

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add a docstring which defines the behaviors of the class, including default formats supported, usage of ``datetime.strptime`` and "first successful parse wins" behavior. Also add '%Y-%m-%d %H:%M:%S' to the default formats, as it is at least as commonly seen as '%Y-%m-%dT%H:%M:%S'. Minor test fix to handle the new format. Add ``click.DateTime`` to changelog.

authored by Stephen Rosen · committed by David Lord · parent 6e62332 · Browse files · 4 files+31 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+31 added−4 removed

  • tests:+5 added−3 removed
  • other:+26 added−1 removed

4 files+31 added−4 removed

modified:click/types.py+21 added-1 removed
182 unmodified lines
183 return 'Choice(%r)' % list(self.choices)184185186class DateTime(ParamType):187 """The DateTime type converts date strings into `datetime` objects.188189 The format strings which are checked are configurable, but default to some190 common (non-timezone aware) ISO 8601 formats.191192 When specifying *DateTime* formats, you should only pass a list or a tuple.193 Other iterables, like generators, may lead to surprising results.194195 The format strings are processed using ``datetime.strptime``, and this196 consequently defines the format strings which are allowed.197198 Parsing is tried using each format, in order, and the first format which199 parses successfully is used.200201 :param formats: A list or tuple of date format strings, in the order in202 which they should be tried. Defaults to203 ``'%Y-%m-%d'``, ``'%Y-%m-%dT%H:%M:%S'``,204 ``'%Y-%m-%d %H:%M:%S'``.205 """206 name = 'datetime'207208 def __init__(self, formats=None):209 self.formats = formats or [210 '%Y-%m-%d',192 '%Y-%m-%dT%H:%M:%S'211 '%Y-%m-%dT%H:%M:%S',212 '%Y-%m-%d %H:%M:%S'213 ]214215 def get_metavar(self, param):216 return '[{}]'.format('|'.join(self.formats))
452 unmodified lines
modified:docs/parameters.rst+3 added
68 unmodified lines
6970.. autoclass:: FloatRange71 :noindex:7273.. autoclass:: DateTime74 :noindex:7576Custom parameter types can be implemented by subclassing77:class:`click.ParamType`. For simple cases, passing a Python function that78fails with a `ValueError` is also supported, though discouraged.79
60 unmodified lines
modified:tests/test_basic.py+5 added-3 removed
347 unmodified lines
348 assert result.output == '2015-09-29T09:11:22\n'349350 result = runner.invoke(cli, ['--start_date=2015-09'])351 assert result.exit_code == 2352 assert 'Invalid value for "--start_date": invalid datetime format: 2015-09. ' \353 '(choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S)' in result.output352 assert ('Invalid value for "--start_date": '353 'invalid datetime format: 2015-09. '354 '(choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)'355 ) in result.output356357 result = runner.invoke(cli, ['--help'])356 assert '--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S]' in result.output358 assert '--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]' in result.output359360361def test_datetime_option_custom(runner):362 @click.command()
174 unmodified lines
modified:CHANGES.rst+2 added
13 unmodified lines
14- Document byte-stripping behavior of ``CliRunner``. (`#334`_, `#1010`_)15- Usage errors now hint at the ``--help`` option. (`#393`_, `#557`_)16- Implement streaming pager. (`#409`_, `#889`_)17- Extract bar formatting to its own method. (`#414`_)18- Add ``DateTime`` type for converting input in given date time formats. (`#423`_)19- ``secho``'s first argument can now be ``None``, like in ``echo``. (`#424`_)20- Fixes a ``ZeroDivisionError`` in ``ProgressBar.make_step``, when the arg passed to the first call of ``ProgressBar.update`` is 0. (`#447`_, `#1012`_)21- Show progressbar only if total execution time is visible. (`#487`_)22- Added the ability to hide commands and options from help. (`#500`_)
70 unmodified lines
93.. _#334: https://github.com/pallets/click/issues/33494.. _#393: https://github.com/pallets/click/issues/39395.. _#409: https://github.com/pallets/click/issues/40996.. _#414: https://github.com/pallets/click/pull/41497.. _#423: https://github.com/pallets/click/pull/42398.. _#424: https://github.com/pallets/click/pull/42499.. _#447: https://github.com/pallets/click/issues/447100.. _#487: https://github.com/pallets/click/pull/487101.. _#500: https://github.com/pallets/click/pull/500
449 unmodified lines