Feature: Return namedtuples for get_format_args#93
Feature: Return namedtuples for get_format_args#93wcooley wants to merge 3 commits intomahmoud:masterfrom
Conversation
The nested tuple/list structures that are returned by `get_format_args` are fairly cumbersome; returning data `namedtuple` makes accessing the data much cleaner.
|
Also, I tested this locally with py27, py35 and pypy. (I don't have py34 and I couldn't get the py26 virtualenv to build.) |
mahmoud
left a comment
There was a problem hiding this comment.
This seems like a pretty innocuous and helpful change, thank you! I just had one change, to comply with the boltons integration architecture.
boltons/formatutils.py
Outdated
| import re | ||
| from string import Formatter | ||
|
|
||
| from .namedutils import namedtuple |
There was a problem hiding this comment.
can you change this to:
try:
from boltons.namedutils import namedtuple
except ImportError:
from collections import namedtupleThis will allow the module to continue to be used both inside and outside of the boltons package.
There was a problem hiding this comment.
Done. It also occurs to me that I have also neglected to update the documentation.
| assert get_format_args(sample) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(('sample', 'expected'), zip(_TEST_TMPLS, [ |
There was a problem hiding this comment.
hehe this inline parametrize is kind of long, just curious, is this common practice with pytest?
There was a problem hiding this comment.
I assume so; the pytest docs show an example of a multi-line parametrize right at the top. There are also a few instances in the pytest tests themselves (such as in test_conftest.py, test_config.py and test_mark.py).
If I have something like this that I intend to reuse exactly as-is, rather than moving the list of samples to a separate variable, I create a new mark:
pytest.mark.get_format_samples = pytest.mark.parametrize(('sample', 'expected), ...
The nested tuple/list structures that are returned by
get_format_argsare fairly cumbersome; returning datanamedtuplemakes accessing the data much cleaner.I am a bit uncertain about the field names themselves - - I chose 'positional' and 'named' because the documentation for the function describes them that way, but I chose 'key' and 'index' for the component (rather than 'position' and 'name') because those are the standard way to refer to them. It seems a bit inconsistent, however -- perhaps the
FormatArgsfields should beindexedandkeyed? (or "...s" instead of "...ed"?)