Skip to content

Commit 466e719

Browse files
Ken KundertKen Kundert
Ken Kundert
authored and
Ken Kundert
committed
added top to common mistakes
1 parent cb0e92e commit 466e719

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

doc/common_mistakes.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
.. hide:
2+
3+
>>> from inform import Inform
4+
>>> ignore = Inform(prog_name=False)
5+
6+
17
***************
28
Common mistakes
39
***************
410

511
.. currentmodule:: nestedtext
612

13+
Two Values for One Key
14+
----------------------
15+
716
When :func:`load()` or :func:`loads()` complains of errors it is important to
817
look both at the line fingered by the error message and the one above it. The
918
line that is the target of the error message might by an otherwise valid
@@ -80,3 +89,55 @@ trailing whitespace. To do so in Vim, add::
8089
set listchars=trail:␣
8190

8291
to your ~/.vimrc file.
92+
93+
94+
Lists or Strings at the Top Level
95+
---------------------------------
96+
97+
Most *NestedText* files start with key-value pairs at the top-level and we
98+
noticed that many developers would simply assume this in their code, which would
99+
result in unexpected crashes when their programs read legal *NestedText* files
100+
that had either a list or a string at the top level. To avoid this, the
101+
:func:`load` and :func:`loads` functions are configured to expect a dictionary
102+
at the top level by default, which results in an error being reported if
103+
a dictionary key is not the first token found:
104+
105+
.. code-block:: python
106+
107+
>>> import nestedtext as nt
108+
109+
>>> content = """
110+
... - a
111+
... - b
112+
... """
113+
114+
>>> try:
115+
... print(nt.loads(content))
116+
... except nt.NestedTextError as e:
117+
... e.report()
118+
error: 2: content must start with key or brace ({).
119+
2- a❭
120+
121+
This restriction is easily removed using *top*:
122+
123+
.. code-block:: python
124+
125+
>>> try:
126+
... print(nt.loads(content, top=list))
127+
... except nt.NestedTextError as e:
128+
... e.report()
129+
['a', 'b']
130+
131+
The *top* argument can take any of the following values: *"dict"*, *dict*,
132+
*"list"*, *list*, *"str"*, *str*, *"any"*, or *any*. The default value is
133+
*dict*. The value given for *top* also determines the value returned by
134+
:func:`load` and :func:`loads` if the *NestedText* document is empty.
135+
136+
================ =================================
137+
*top* value returned for empty document
138+
---------------- ---------------------------------
139+
*"dict"*, *dict* ``{}``
140+
*"list"*, *list* ``[]``
141+
*"str"*, *str* ``""``
142+
*"any"*, *any* *None*
143+
================ =================================

examples/accumulation/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def report_error(msg):
8484
value = base
8585
elif key in schema:
8686
if accumulate:
87-
report_error(f"setting is unsuitable for accumulation")
87+
report_error("setting is unsuitable for accumulation")
8888
value = schema[key](value) # cast to desired type
8989
else:
90-
report_error(f"unknown setting")
90+
report_error("unknown setting")
9191
processed[key] = value
9292

9393
return processed

tests/official_tests

tests/test_nestedtext.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,9 @@ def check_result(keys, expected, addresses):
992992
◊ 7 ❬ > Topeka, Kansas 20697❭
993993
994994
""", bolm='◊', strip_nl="b")
995+
with pytest.raises(IndexError) as exception:
996+
loc.as_line('value', offset=(2,8))
997+
assert exception.value.args == (2,)
995998

996999

9971000
# test_keymaps_with_duplicates {{{2

0 commit comments

Comments
 (0)