Skip to content

Commit

Permalink
Merge pull request #27 from karpelcevs/main
Browse files Browse the repository at this point in the history
Keep Bool inline comments
  • Loading branch information
pappasam committed Aug 31, 2022
2 parents 19423fc + 3feeebe commit e517a04
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/examples/defaults/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hosts = [
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
enabled = true # Comment after a boolean

[owner]
name = "Tom Preston-Werner"
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dob = 1979-05-27T07:32:00Z # First class dates? Why not?
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
enabled = true # Comment after a boolean

[servers]

Expand Down
8 changes: 6 additions & 2 deletions toml_sort/tomlsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ def write_header_comment(from_doc: TOMLDocument, to_doc: TOMLDocument) -> None:
return


def convert_tomlkit_buggy_types(in_value: Any, parent: Any) -> Item:
def convert_tomlkit_buggy_types(in_value: Any, parent: Any, key: str) -> Item:
"""Fix buggy items while iterating through tomlkit items.
Iterating through tomlkit items can often be buggy; this function
fixes it
"""
if isinstance(in_value, bool):
# Bool items don't have trivia and are resolved to Python's `bool`
# https://github.com/sdispater/tomlkit/issues/119#issuecomment-955840942
return parent.value.item(key)
if isinstance(in_value, Item):
return in_value
if isinstance(in_value, Container):
Expand Down Expand Up @@ -94,7 +98,7 @@ def sorted_children_table(
is not necessary.
"""
table_items = [
(key, convert_tomlkit_buggy_types(parent[key], parent))
(key, convert_tomlkit_buggy_types(parent[key], parent, key))
for key in parent.keys()
]
tables = (
Expand Down

0 comments on commit e517a04

Please sign in to comment.