Skip to content

Commit

Permalink
added top to common mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Apr 16, 2024
1 parent cb0e92e commit 466e719
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
61 changes: 61 additions & 0 deletions doc/common_mistakes.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
.. hide:
>>> from inform import Inform
>>> ignore = Inform(prog_name=False)
***************
Common mistakes
***************

.. currentmodule:: nestedtext

Two Values for One Key
----------------------

When :func:`load()` or :func:`loads()` complains of errors it is important to
look both at the line fingered by the error message and the one above it. The
line that is the target of the error message might by an otherwise valid
Expand Down Expand Up @@ -80,3 +89,55 @@ trailing whitespace. To do so in Vim, add::
set listchars=trail:␣

to your ~/.vimrc file.


Lists or Strings at the Top Level
---------------------------------

Most *NestedText* files start with key-value pairs at the top-level and we
noticed that many developers would simply assume this in their code, which would
result in unexpected crashes when their programs read legal *NestedText* files
that had either a list or a string at the top level. To avoid this, the
:func:`load` and :func:`loads` functions are configured to expect a dictionary
at the top level by default, which results in an error being reported if
a dictionary key is not the first token found:

.. code-block:: python
>>> import nestedtext as nt
>>> content = """
... - a
... - b
... """
>>> try:
... print(nt.loads(content))
... except nt.NestedTextError as e:
... e.report()
error: 2: content must start with key or brace ({).
2- a❭
This restriction is easily removed using *top*:
.. code-block:: python
>>> try:
... print(nt.loads(content, top=list))
... except nt.NestedTextError as e:
... e.report()
['a', 'b']
The *top* argument can take any of the following values: *"dict"*, *dict*,
*"list"*, *list*, *"str"*, *str*, *"any"*, or *any*. The default value is
*dict*. The value given for *top* also determines the value returned by
:func:`load` and :func:`loads` if the *NestedText* document is empty.
================ =================================
*top* value returned for empty document
---------------- ---------------------------------
*"dict"*, *dict* ``{}``
*"list"*, *list* ``[]``
*"str"*, *str* ``""``
*"any"*, *any* *None*
================ =================================
4 changes: 2 additions & 2 deletions examples/accumulation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def report_error(msg):
value = base
elif key in schema:
if accumulate:
report_error(f"setting is unsuitable for accumulation")
report_error("setting is unsuitable for accumulation")
value = schema[key](value) # cast to desired type
else:
report_error(f"unknown setting")
report_error("unknown setting")
processed[key] = value

return processed
2 changes: 1 addition & 1 deletion tests/official_tests
Submodule official_tests updated 1 files
+1 −1 README.md
3 changes: 3 additions & 0 deletions tests/test_nestedtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,9 @@ def check_result(keys, expected, addresses):
◊ 7 ❬ > Topeka, Kansas 20697❭
""", bolm='◊', strip_nl="b")
with pytest.raises(IndexError) as exception:
loc.as_line('value', offset=(2,8))
assert exception.value.args == (2,)


# test_keymaps_with_duplicates {{{2
Expand Down

0 comments on commit 466e719

Please sign in to comment.