Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
steps:
- name: Checkout
uses: actions/checkout@v6
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Replace tomlkit with tomlrt as the style-preserving TOML implementation.
- Minor changes in formatted output eg introduces spaces around curly braces
in inline tables
- Require Python 3.10 or newer.

## 0.24.4

### Changed
Expand Down
26 changes: 15 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
python_version = "3.9"
python_version = "3.10"
strict = true
enable_error_code = "ignore-without-code,redundant-expr,truthy-bool"

Expand All @@ -27,8 +27,8 @@ include = ["README.md"]
license = "MIT"

[tool.poetry.dependencies]
python = ">=3.9"
tomlkit = ">=0.13.2"
python = ">=3.10"
tomlrt = ">=2.2.0"

[tool.poetry.group.dev.dependencies]
mypy = "*"
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/sorted/from-toml-lang-first.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ server = "192.168.1.1"

[inline]
# Test "first" sorting for inline tables
c = {version = "4.12.2", limit = "<3.12"}
c = { version = "4.12.2", limit = "<3.12" }

[owner]
name = "Tom Preston-Werner"
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/sorted/from-toml-lang-overrides.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ server = "192.168.1.1"

[inline]
# Test "first" sorting for inline tables
c = {limit = "<3.12", version = "4.12.2"}
c = { limit = "<3.12", version = "4.12.2" }

[owner]
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/sorted/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enabled = true # Comment after a boolean

[inline]
# Test "first" sorting for inline tables
c = {limit = "<3.12", version = "4.12.2"}
c = { limit = "<3.12", version = "4.12.2" }

[owner]
name = "Tom Preston-Werner"
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/sorted/gradle-version-catalog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ barVersion = "2.0.0"
fooVersion = "1.0.0"

[libraries]
barLib = {module = "com.example:bar", version.ref = "barVersion"}
fooLib = {module = "com.example:foo", version.ref = "fooVersion"}
barLib = { module = "com.example:bar", version.ref = "barVersion" }
fooLib = { module = "com.example:foo", version.ref = "fooVersion" }
16 changes: 8 additions & 8 deletions tests/examples/sorted/inline-default.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[a]
test = [
{x = [
{ x = [
"foo",
"bar"
], a = [
""
]},
{y = [
] },
{ y = [
"foo",
"baz"
]}
] }
]

[b]
Expand All @@ -24,18 +24,18 @@ alist = [
# Second attached comment
'a'
]
'aalist' = {c = "test", a = "another test"} # list comment
'aalist' = { c = "test", a = "another test" } # list comment
# Multilevel inline array
aaalist = [
[
{name = "Homer Simpson", town = "Springfield"}
{ name = "Homer Simpson", town = "Springfield" }
],
[
{name = "Bart Simpson", town = "Springfield"}
{ name = "Bart Simpson", town = "Springfield" }
],
# Weird block comment
[
# Another weird block comment
{name = "Lisa Simpson", town = "Springfield", a-test = "Did this sort?"} # Should get sorted
{ name = "Lisa Simpson", town = "Springfield", a-test = "Did this sort?" } # Should get sorted
]
]
16 changes: 8 additions & 8 deletions tests/examples/sorted/inline-no-comments-no-table-sort.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[b]
aaalist = [
[
{a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield"},
{ a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield" },
],
[
{name = "Bart Simpson", town = "Springfield"},
{ name = "Bart Simpson", town = "Springfield" },
],
[
{name = "Homer Simpson", town = "Springfield"},
{ name = "Homer Simpson", town = "Springfield" },
],
]
'aalist' = {a = "another test", c = "test"}
'aalist' = { a = "another test", c = "test" }
alist = [
'a',
'g',
Expand All @@ -22,14 +22,14 @@ z = []

[a]
test = [
{a = [
{ a = [
"",
], x = [
"bar",
"foo",
]},
{y = [
] },
{ y = [
"baz",
"foo",
]},
] },
]
16 changes: 8 additions & 8 deletions tests/examples/sorted/inline.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[a]
test = [
{a = [
{ a = [
""
], x = [
"bar",
"foo"
]},
{y = [
] },
{ y = [
"baz",
"foo"
]}
] }
]

[b]
Expand All @@ -18,16 +18,16 @@ aaalist = [
# Weird block comment
[
# Another weird block comment
{a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield"} # Should get sorted
{ a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield" } # Should get sorted
],
[
{name = "Bart Simpson", town = "Springfield"}
{ name = "Bart Simpson", town = "Springfield" }
],
[
{name = "Homer Simpson", town = "Springfield"}
{ name = "Homer Simpson", town = "Springfield" }
]
]
'aalist' = {a = "another test", c = "test"} # list comment
'aalist' = { a = "another test", c = "test" } # list comment
alist = [
# Multiline
# Second attached comment
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/sorted/weird.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ date = "2019"
name = "Samuel Roeca"

[[a-section.hello]]
ports = [ 8001, 8001, 8002 ]
ports = [8001, 8001, 8002]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[[a-section.hello]]
ports = [ 80 ]
ports = [80]
dob = 1920-05-27T07:32:00Z # Another date!

[b-section]
Expand Down
10 changes: 2 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

PATH_EXAMPLES = "tests/examples"

# NOTE: weird.toml currently exposes what I interpret to be some buggy
# functionality in AOT parsing. It seems like the latest version of tomlkit
# doesn't handle edge cases elegantly here and sometimes elements in AOT's are
# ordered randomly or sorted somewhere in tomlkit itself. I've xfail'd the test
# case for now.


class SubprocessReturn(NamedTuple):
"""Organize the return results when of running a cli subprocess."""
Expand Down Expand Up @@ -57,7 +51,7 @@ def capture(
"path_unsorted,path_sorted",
[
("from-toml-lang", "sorted/from-toml-lang"),
pytest.param("weird", "sorted/weird", marks=[pytest.mark.xfail]),
pytest.param("weird", "sorted/weird"),
("pyproject-weird-order", "sorted/pyproject-weird-order"),
("comment", "sorted/comment-header-footer"),
("inline", "sorted/inline-default"),
Expand Down Expand Up @@ -358,7 +352,7 @@ def test_load_config_overrides(toml, expected):
parsed = cli.parse_config_overrides(section)
assert expected == parsed
# Make sure we are returning normal python types rather
# than TOMLKit types.
# than tomlrt types.
for key, value in parsed.items():
assert type(key) is str
assert type(value) is SortOverrideConfiguration
Expand Down
1 change: 0 additions & 1 deletion tests/test_toml_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def test_sort_toml_is_str() -> None:
spaces_before_inline_comment=1
),
},
marks=[pytest.mark.xfail],
),
pytest.param(
"single-comment",
Expand Down
25 changes: 13 additions & 12 deletions toml_sort/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import dataclasses
import sys
from argparse import ArgumentParser
from typing import Any, Dict, List, Optional, Tuple, Type, cast
from typing import Any, Dict, List, Mapping, Optional, Tuple, Type

import tomlkit
from tomlkit import TOMLDocument
import tomlrt
from tomlrt import Table

from .tomlsort import (
CommentConfiguration,
Expand Down Expand Up @@ -64,21 +64,20 @@ def validate_and_copy(
target[key] = data.pop(key)


def load_pyproject() -> TOMLDocument:
def load_pyproject() -> Table:
"""Load pyproject file, and return tool.tomlsort section."""
try:
with open("pyproject.toml", encoding="utf-8") as file:
content = file.read()
except OSError:
return tomlkit.document()
return Table.inline()

document = tomlkit.parse(content)
tool_section = document.get("tool", tomlkit.document())
return cast(TOMLDocument, tool_section.get("tomlsort", tomlkit.document()))
document = tomlrt.loads(content)
return document.get_table("tool.tomlsort", Table.inline())


def parse_config(tomlsort_section: TOMLDocument) -> Dict[str, Any]:
"""Load the toml_sort configuration from a TOMLDocument."""
def parse_config(tomlsort_section: Mapping[str, Any]) -> Dict[str, Any]:
"""Load the toml_sort configuration from a TOML mapping."""
config = dict(tomlsort_section)

# remove the overrides key, since it is parsed separately
Expand Down Expand Up @@ -115,12 +114,14 @@ def parse_config(tomlsort_section: TOMLDocument) -> Dict[str, Any]:


def parse_config_overrides(
tomlsort_section: TOMLDocument,
tomlsort_section: Table,
) -> Dict[str, SortOverrideConfiguration]:
"""Parse the tool.tomlsort.overrides section of the config."""
fields = dataclasses.fields(SortOverrideConfiguration)
settings_definition = {field.name: field.type for field in fields}
override_settings = tomlsort_section.get("overrides", tomlkit.document()).unwrap()
override_settings = tomlsort_section.get_table(
"overrides", Table.inline()
).to_dict()

overrides = {}
for path, settings in override_settings.items():
Expand Down
Loading