Skip to content

Commit

Permalink
Support mixed data types in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
kieran-ryan committed Jul 10, 2023
1 parent f4d8e8d commit 3fa2cfc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Formatter for pyproject.toml files.

This package enforces consistent formatting of pyproject.toml files, reducing merge request conflicts and saving time otherwise spent to format manually. It also contributes to a cleaner git history and more readable code; enhancing overall project organisation and maintainability. Experience a streamlined workflow, reduced errors, and improved code readability with `pyprojectsort`.
This package enforces consistent formatting of pyproject.toml files, reducing merge request conflicts and saving time otherwise spent on manual formatting. It also contributes to a cleaner git history and more readable code; enhancing overall project organisation and maintainability. Experience a streamlined workflow, reduced errors, and improved code readability with `pyprojectsort`.

## Features

Expand Down
15 changes: 12 additions & 3 deletions pyprojectsort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,20 @@ def reformat_pyproject(pyproject: dict | list) -> dict:
"""Reformat pyproject toml file."""
if isinstance(pyproject, dict):
return {
key: reformat_pyproject(value)
for key, value in sorted(pyproject.items(), key=lambda item: item[0])
key: reformat_pyproject(value) for key, value in sorted(pyproject.items())
}
if isinstance(pyproject, list):
return sorted(reformat_pyproject(item) for item in pyproject)
numbers = []
strings = []
dictionaries = []
for i in pyproject:
if isinstance(i, float | int):
numbers.append(i)
elif isinstance(i, str):
strings.append(i)
else:
dictionaries.append(reformat_pyproject(i))
return sorted(numbers) + sorted(strings) + dictionaries
return pyproject


Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_reformat_pyproject():
pyproject = {
"project": {"name": "pyprojectsort"},
"build-system": {"name": "flit"},
"tool.pylint": {"ignore": ["docs", "tests", "venv"]},
"tool.pylint": {"ignore": ["docs", "tests", "venv", 1, 1.1, {}]},
"tool.black": {"line_length": 88},
}

Expand All @@ -82,7 +82,7 @@ def test_reformat_pyproject():
"build-system": {"name": "flit"},
"project": {"name": "pyprojectsort"},
"tool.black": {"line_length": 88},
"tool.pylint": {"ignore": ["docs", "tests", "venv"]},
"tool.pylint": {"ignore": [1, 1.1, "docs", "tests", "venv", {}]},
}
assert reformat_pyproject(pyproject) == sorted_pyproject

Expand Down

0 comments on commit 3fa2cfc

Please sign in to comment.