diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 956b1b5..ec15a92 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -31,7 +31,9 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install mypy==1.5.1 ruff==0.0.286 pytest + python -m pip install black==23.9.1 mypy==1.5.1 ruff==0.0.286 pytest + - name: Check format with Black + run: black --check . - name: Lint with mypy run: mypy - name: Lint with Ruff diff --git a/docs/conf.py b/docs/conf.py index 8c678e3..d623474 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -18,20 +18,20 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath('..')) +sys.path.insert(0, os.path.abspath("..")) from qstring import __version__ # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' +# needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', + "sphinx.ext.autodoc", ] # Add any paths that contain templates here, relative to this directory. @@ -40,18 +40,18 @@ # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] -source_suffix = '.rst' +source_suffix = ".rst" # The encoding of source files. -#source_encoding = 'utf-8-sig' +# source_encoding = 'utf-8-sig' # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = 'qstring' -copyright = '2015, Fast Monkeys Oy' -author = 'Janne Vanhala' +project = "qstring" +copyright = "2015, Fast Monkeys Oy" +author = "Janne Vanhala" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -71,37 +71,37 @@ # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +# today = '' # Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' +# today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['_build'] +exclude_patterns = ["_build"] # The reST default role (used for this markup: `text`) to use for all # documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). -#add_module_names = True +# add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] +# modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False +# keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -111,35 +111,35 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'alabaster' +html_theme = "alabaster" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. html_theme_options = { - 'github_user': 'fastmonkeys', - 'github_repo': 'qstring', - 'github_banner': True + "github_user": "fastmonkeys", + "github_repo": "qstring", + "github_banner": True, } # Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] +# html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -#html_title = None +# html_title = None # A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None +# html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. -#html_logo = None +# html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. -#html_favicon = None +# html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, @@ -149,122 +149,115 @@ # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. -#html_extra_path = [] +# html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' +# html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. -#html_sidebars = {} +# html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} +# html_additional_pages = {} # If false, no module index is generated. -#html_domain_indices = True +# html_domain_indices = True # If false, no index is generated. -#html_use_index = True +# html_use_index = True # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True +# html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True +# html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True +# html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +# html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None +# html_file_suffix = None # Language to be used for generating the HTML full-text search index. # Sphinx supports the following languages: # 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' # 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr' -#html_search_language = 'en' +# html_search_language = 'en' # A dictionary with options for the search language support, empty by default. # Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} +# html_search_options = {'type': 'default'} # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' +# html_search_scorer = 'scorer.js' # Output file base name for HTML help builder. -htmlhelp_basename = 'qstringdoc' +htmlhelp_basename = "qstringdoc" # -- Options for LaTeX output --------------------------------------------- latex_elements = { -# The paper size ('letterpaper' or 'a4paper'). -#'papersize': 'letterpaper', - -# The font size ('10pt', '11pt' or '12pt'). -#'pointsize': '10pt', - -# Additional stuff for the LaTeX preamble. -#'preamble': '', - -# Latex figure (float) alignment -#'figure_align': 'htbp', + # The paper size ('letterpaper' or 'a4paper'). + #'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + #'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + #'preamble': '', + # Latex figure (float) alignment + #'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'qstring.tex', 'qstring Documentation', - 'Janne Vanhala', 'manual'), + (master_doc, "qstring.tex", "qstring Documentation", "Janne Vanhala", "manual"), ] # The name of an image file (relative to this directory) to place at the top of # the title page. -#latex_logo = None +# latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # If true, show page references after internal links. -#latex_show_pagerefs = False +# latex_show_pagerefs = False # If true, show URL addresses after external links. -#latex_show_urls = False +# latex_show_urls = False # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. -#latex_domain_indices = True +# latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'qstring', 'qstring Documentation', - [author], 1) -] +man_pages = [(master_doc, "qstring", "qstring Documentation", [author], 1)] # If true, show URL addresses after external links. -#man_show_urls = False +# man_show_urls = False # -- Options for Texinfo output ------------------------------------------- @@ -273,19 +266,25 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'qstring', 'qstring Documentation', - author, 'qstring', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "qstring", + "qstring Documentation", + author, + "qstring", + "One line description of project.", + "Miscellaneous", + ), ] # Documents to append as an appendix to all manuals. -#texinfo_appendices = [] +# texinfo_appendices = [] # If false, no module index is generated. -#texinfo_domain_indices = True +# texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' +# texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False +# texinfo_no_detailmenu = False diff --git a/qstring/__init__.py b/qstring/__init__.py index c943a88..71ba33a 100644 --- a/qstring/__init__.py +++ b/qstring/__init__.py @@ -3,9 +3,9 @@ from .unnest import unnest __all__ = [ - 'nest', - 'Nested', - 'ParameterTypeError', - 'unnest', + "nest", + "Nested", + "ParameterTypeError", + "unnest", ] -__version__ = '0.2.1' +__version__ = "0.2.1" diff --git a/qstring/nest.py b/qstring/nest.py index 3b49ab3..0c5b58b 100644 --- a/qstring/nest.py +++ b/qstring/nest.py @@ -3,7 +3,8 @@ from . import exc -Nested = Dict[str, Union[str, List[str], 'Nested']] +Nested = Dict[str, Union[str, List[str], "Nested"]] + def nest(params: List[Tuple[str, str]]) -> Nested: """ @@ -56,7 +57,8 @@ def _parse_parameter(key: str, value: Union[str, List[str]]) -> Nested: return _ParameterParser().parse(key, value) -_TokenType = Literal['LEFT BRACKET', 'RIGHT BRACKET', 'NAME'] +_TokenType = Literal["LEFT BRACKET", "RIGHT BRACKET", "NAME"] + class _Token(NamedTuple): type: _TokenType @@ -72,13 +74,13 @@ def parse(self, key: str, value: Union[str, List[str]]) -> Nested: return {key: self._parse_object()} def _tokenize(self, input: str) -> Generator[_Token, None, None]: - for value in re.split(r'([\[\]])', input): - if value == '[': - yield _Token('LEFT BRACKET', value) - elif value == ']': - yield _Token('RIGHT BRACKET', value) + for value in re.split(r"([\[\]])", input): + if value == "[": + yield _Token("LEFT BRACKET", value) + elif value == "]": + yield _Token("RIGHT BRACKET", value) elif value: - yield _Token('NAME', value) + yield _Token("NAME", value) def _parse_object(self) -> Union[str, List[str], Nested]: if not self.tokens: @@ -87,19 +89,19 @@ def _parse_object(self) -> Union[str, List[str], Nested]: return {key: self._parse_object()} def _match_name(self) -> str: - token, = self._match('NAME') + (token,) = self._match("NAME") return token.value def _match_object(self) -> str: - _, token, _ = self._match('LEFT BRACKET', 'NAME', 'RIGHT BRACKET') + _, token, _ = self._match("LEFT BRACKET", "NAME", "RIGHT BRACKET") return token.value def _match(self, *expected_types: _TokenType) -> List[_Token]: - tokens = self.tokens[:len(expected_types)] + tokens = self.tokens[: len(expected_types)] types = tuple(t.type for t in tokens) if types != expected_types: raise exc.ParseError(self.key) - self.tokens = self.tokens[len(expected_types):] + self.tokens = self.tokens[len(expected_types) :] return tokens @@ -109,14 +111,13 @@ def _merge(target: Nested, source: Nested) -> Nested: if key in target: if not isinstance(value, dict): raise exc.ParameterTypeError( - f'Expected dict (got {type(value).__name__}) for param {key!r}' + f"Expected dict (got {type(value).__name__}) for param {key!r}" ) target_value = target[key] if not isinstance(target_value, dict): raise exc.ParameterTypeError( - 'Expected {expected} (got dict) for param {param!r}'.format( - expected=type(target_value).__name__, - param=key + "Expected {expected} (got dict) for param {param!r}".format( + expected=type(target_value).__name__, param=key ) ) value = _merge(target_value, value) diff --git a/qstring/unnest.py b/qstring/unnest.py index 04f89c3..ab3cbbc 100644 --- a/qstring/unnest.py +++ b/qstring/unnest.py @@ -19,16 +19,13 @@ def unnest(obj: Nested) -> List[Tuple[str, str]]: def _unnest( - obj: Union[Nested, List[str], str], prefix: str = '' + obj: Union[Nested, List[str], str], prefix: str = "" ) -> List[Tuple[str, str]]: if isinstance(obj, dict): params = [] for key, value in obj.items(): params.extend( - _unnest( - obj=value, - prefix=prefix + '[' + key + ']' if prefix else key - ) + _unnest(obj=value, prefix=prefix + "[" + key + "]" if prefix else key) ) return params elif isinstance(obj, list): diff --git a/tests/test_nest.py b/tests/test_nest.py index 7eb8d6a..bd35b5a 100644 --- a/tests/test_nest.py +++ b/tests/test_nest.py @@ -7,112 +7,97 @@ @pytest.mark.parametrize( - ('obj', 'expected'), + ("obj", "expected"), [ ([], {}), - ([('', '')], {'': ''}), - ([('foo', '')], {'foo': ''}), - ([('', 'bar')], {'': 'bar'}), - ([('foo', 'bar')], {'foo': 'bar'}), - ([('foo', ''), ('bar', '')], {'foo': '', 'bar': ''}), - ([('foo', '1'), ('foo', '2'), ('foo', '3')], {'foo': ['1', '2', '3']}), - ([('foo', '1'), ('bar', '2')], {'foo': '1', 'bar': '2'}), - ([('x[y]', '1')], {'x': {'y': '1'}}), - ([('x[y][z]', '1')], {'x': {'y': {'z': '1'}}}), - ([('x[]', '1')], {'x[]': '1'}), - ([('x[', '1')], {'x[': '1'}), - ([('x]', '1')], {'x]': '1'}), - ([('x[]]', '1')], {'x[]]': '1'}), - ([('x[[]', '1')], {'x[[]': '1'}), - ] + ([("", "")], {"": ""}), + ([("foo", "")], {"foo": ""}), + ([("", "bar")], {"": "bar"}), + ([("foo", "bar")], {"foo": "bar"}), + ([("foo", ""), ("bar", "")], {"foo": "", "bar": ""}), + ([("foo", "1"), ("foo", "2"), ("foo", "3")], {"foo": ["1", "2", "3"]}), + ([("foo", "1"), ("bar", "2")], {"foo": "1", "bar": "2"}), + ([("x[y]", "1")], {"x": {"y": "1"}}), + ([("x[y][z]", "1")], {"x": {"y": {"z": "1"}}}), + ([("x[]", "1")], {"x[]": "1"}), + ([("x[", "1")], {"x[": "1"}), + ([("x]", "1")], {"x]": "1"}), + ([("x[]]", "1")], {"x[]]": "1"}), + ([("x[[]", "1")], {"x[[]": "1"}), + ], ) def test_nest(obj: List[Tuple[str, str]], expected: qstring.Nested) -> None: assert qstring.nest(obj) == expected def test_nest_maintains_order() -> None: - nested = qstring.nest([ - ('a', '1'), - ('b[a]', '1'), - ('b[b]', '2'), - ('b[c]', '3'), - ('c[a][a]', '1'), - ('c[a][b]', '2'), - ('c[a][c]', '3'), - ]) - assert list(nested.keys()) == ['a', 'b', 'c'] + nested = qstring.nest( + [ + ("a", "1"), + ("b[a]", "1"), + ("b[b]", "2"), + ("b[c]", "3"), + ("c[a][a]", "1"), + ("c[a][b]", "2"), + ("c[a][c]", "3"), + ] + ) + assert list(nested.keys()) == ["a", "b", "c"] - b = nested['b'] + b = nested["b"] assert isinstance(b, dict) - assert list(b.keys()) == ['a', 'b', 'c'] + assert list(b.keys()) == ["a", "b", "c"] - c = nested['c'] + c = nested["c"] assert isinstance(c, dict) - c_a = c['a'] + c_a = c["a"] assert isinstance(c_a, dict) - assert list(c_a.keys()) == ['a', 'b', 'c'] + assert list(c_a.keys()) == ["a", "b", "c"] -@pytest.mark.parametrize(('target', 'source', 'expected'), [ - ( - {'a': '1'}, - {}, - {'a': '1'} - ), - ( - {'a': '1'}, - {'b': '2'}, - {'a': '1', 'b': '2'} - ), - ( - {'a': '1'}, - {'b': {'c': '2'}}, - {'a': '1', 'b': {'c': '2'}} - ), - ( - {'a': {'b': '1'}}, - {'a': {'c': '2'}}, - {'a': {'b': '1', 'c': '2'}} - ), -]) +@pytest.mark.parametrize( + ("target", "source", "expected"), + [ + ({"a": "1"}, {}, {"a": "1"}), + ({"a": "1"}, {"b": "2"}, {"a": "1", "b": "2"}), + ({"a": "1"}, {"b": {"c": "2"}}, {"a": "1", "b": {"c": "2"}}), + ({"a": {"b": "1"}}, {"a": {"c": "2"}}, {"a": {"b": "1", "c": "2"}}), + ], +) def test_merge( - target: qstring.Nested, - source: qstring.Nested, - expected: qstring.Nested + target: qstring.Nested, source: qstring.Nested, expected: qstring.Nested ) -> None: assert _merge(target, source) == expected @pytest.mark.parametrize( - ('target', 'source', 'error'), + ("target", "source", "error"), [ ( - {'x': '1'}, - {'x': {'y': '2'}}, + {"x": "1"}, + {"x": {"y": "2"}}, "Expected str (got dict) for param 'x'", ), ( - {'x': {'y': '1'}}, - {'x': '2'}, + {"x": {"y": "1"}}, + {"x": "2"}, "Expected dict (got str) for param 'x'", ), ( - {'x': ['1', '2']}, - {'x': {'y': '3'}}, + {"x": ["1", "2"]}, + {"x": {"y": "3"}}, "Expected list (got dict) for param 'x'", ), ( - {'x': {'y': '3'}}, - {'x': ['1', '2']}, + {"x": {"y": "3"}}, + {"x": ["1", "2"]}, "Expected dict (got list) for param 'x'", ), - ] + ], ) def test_merge_error( - target: qstring.Nested, - source: qstring.Nested, - error: str + target: qstring.Nested, source: qstring.Nested, error: str ) -> None: with pytest.raises(qstring.ParameterTypeError) as exc_info: _merge(target, source) diff --git a/tests/test_unnest.py b/tests/test_unnest.py index ee87e0c..67b6f44 100644 --- a/tests/test_unnest.py +++ b/tests/test_unnest.py @@ -6,49 +6,19 @@ @pytest.mark.parametrize( - ('obj', 'expected'), + ("obj", "expected"), [ - ( - {'': ''}, - [('', '')] - ), - ( - {'foo': ''}, - [('foo', '')] - ), - ( - {'': 'bar'}, - [('', 'bar')] - ), - ( - {'foo': 'bar'}, - [('foo', 'bar')] - ), - ( - {'foo': 'äö'}, - [('foo', 'äö')] - ), - ( - {'foo': '', 'bar': ''}, - [('foo', ''), ('bar', '')] - ), - ( - {'foo': ['1', '2']}, - [('foo', '1'), ('foo', '2')] - ), - ( - {'foo': '1', 'bar': '2'}, - [('foo', '1'), ('bar', '2')] - ), - ( - {'x': {'y': '1'}}, - [('x[y]', '1')] - ), - ( - {'x': {'y': {'z': '1'}}}, - [('x[y][z]', '1')] - ), - ] + ({"": ""}, [("", "")]), + ({"foo": ""}, [("foo", "")]), + ({"": "bar"}, [("", "bar")]), + ({"foo": "bar"}, [("foo", "bar")]), + ({"foo": "äö"}, [("foo", "äö")]), + ({"foo": "", "bar": ""}, [("foo", ""), ("bar", "")]), + ({"foo": ["1", "2"]}, [("foo", "1"), ("foo", "2")]), + ({"foo": "1", "bar": "2"}, [("foo", "1"), ("bar", "2")]), + ({"x": {"y": "1"}}, [("x[y]", "1")]), + ({"x": {"y": {"z": "1"}}}, [("x[y][z]", "1")]), + ], ) def test_unnest(obj: qstring.Nested, expected: List[Tuple[str, str]]) -> None: assert qstring.unnest(obj) == expected