Skip to content

Commit

Permalink
test that we aren't using dots - 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Apr 26, 2024
1 parent 4ecb9db commit a35b7f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pyproject_toml_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@


class NoDotsDict(dict):
"""A custom dictionary class that disallows keys with dots ('.')."""
"""A custom dictionary class that disallows keys with dots ('.').
Dots have a special meaning in TOML, so a key like `[a.b.c]` in a
`dict` would be dict["a"]["b"]["c"] but NOT dict["a.b.c"]. To avoid
this pitfall, we can disallow dots all together.
There are situations where a dotted key could be useful, like
`["127.0.0.1"] = "value"`. In this case, this the dict key is "127.0.0.1"
(and is not a nesting of subdicts).
"""

def __setitem__(self, key, value):
if "." in key:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_pyproject_toml_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ def assert_outputted_pyproject_toml(
pyproject_toml_path: Path, expected: dict[str, Any]
) -> None:
"""Compare each's contents casted to a dict."""
pyproject_toml_builder.NoDotsDict(expected) # test that we aren't using dots

# test that we aren't using dots
no_dots = pyproject_toml_builder.NoDotsDict()
for key, value in expected.items():
no_dots[key] = value

print()
print("EXPECTED TOML OUTPUT:")
print(expected)
Expand Down

0 comments on commit a35b7f6

Please sign in to comment.