Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple code quality improvements #212

Merged
merged 15 commits into from Jul 28, 2022
19 changes: 18 additions & 1 deletion .flake8
@@ -1,7 +1,14 @@
[flake8]
min_python_version = 3.6.0
max-line-length = 88
ignore = E501, E203, W503
per-file-ignores = __init__.py:F401
per-file-ignores =
# N818: error suffix in exception names (API-breaking change)
tomlkit/exceptions.py: N818,
# FS003: f-string missing prefix
tests/test_items.py: FS003,
tests/test_api.py: FS003,
tests/test_toml_document.py: FS003,
exclude =
.git
__pycache__
Expand All @@ -15,3 +22,13 @@ exclude =
.pytest_cache
.vscode
.github
ban-relative-imports = true
# flake8-use-fstring: https://github.com/MichaelKim0407/flake8-use-fstring#--percent-greedy-and---format-greedy
format-greedy = 1
inline-quotes = double
eradicate-whitelist-extend = ^-.*;
extend-ignore =
# E203: Whitespace before ':' (pycqa/pycodestyle#373)
E203,
# SIM106: Handle error-cases first
SIM106,
44 changes: 39 additions & 5 deletions .pre-commit-config.yaml
@@ -1,15 +1,14 @@
repos:
- repo: https://github.com/ambv/black
rev: 22.3.0
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black

- repo: https://github.com/timothycrosley/isort
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
additional_dependencies: [toml]
exclude: ^.*/?setup\.py$
exclude: docs/.*

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand All @@ -19,3 +18,38 @@ repos:
- id: end-of-file-fixer
exclude: ^tests/(toml-test|toml-spec-tests)/.*
- id: debug-statements

- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
additional_dependencies: &flake8_deps
- flake8-broken-line==0.4.0
- flake8-bugbear==22.7.1
- flake8-comprehensions==3.10.0
- flake8-eradicate==1.2.1
- flake8-quotes==3.3.1
- flake8-simplify==0.19.2
- flake8-tidy-imports==4.8.0
- flake8-typing-imports==1.12.0
- flake8-use-fstring==1.3
- pep8-naming==0.13.0
exclude: ^tomlkit/items\.py

- repo: https://github.com/asottile/pyupgrade
rev: v2.37.1
hooks:
- id: pyupgrade
args: [--py36-plus]

- repo: https://github.com/hadialqattan/pycln
rev: v2.0.1
hooks:
- id: pycln
args: [--all]

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: *flake8_deps
37 changes: 18 additions & 19 deletions tests/test_items.py
Expand Up @@ -8,6 +8,8 @@

import pytest

from tests.util import assert_is_ppo
from tests.util import elementary_test
from tomlkit import api
from tomlkit import parse
from tomlkit.exceptions import NonExistentKey
Expand All @@ -27,9 +29,6 @@
from tomlkit.items import item
from tomlkit.parser import Parser

from .util import assert_is_ppo
from .util import elementary_test


@pytest.fixture()
def tz_pst():
Expand Down Expand Up @@ -120,7 +119,7 @@ def test_aot_unwrap():
d = item([{"a": "A"}, {"b": "B"}])
unwrapped = d.unwrap()
assert_is_ppo(unwrapped, list)
for du, dw in zip(unwrapped, d):
for du, _ in zip(unwrapped, d):
assert_is_ppo(du, dict)
for ku in du:
vu = du[ku]
Expand Down Expand Up @@ -182,26 +181,26 @@ def test_items_can_be_appended_to_and_removed_from_a_table():
_, table = parser._parse_table()

assert isinstance(table, Table)
assert "" == table.as_string()
assert table.as_string() == ""

table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="\n")))

assert 'foo = "bar"\n' == table.as_string()
assert table.as_string() == 'foo = "bar"\n'

table.append(
Key("baz"),
Integer(34, Trivia(comment_ws=" ", comment="# Integer", trail=""), "34"),
)

assert 'foo = "bar"\nbaz = 34 # Integer' == table.as_string()
assert table.as_string() == 'foo = "bar"\nbaz = 34 # Integer'

table.remove(Key("baz"))

assert 'foo = "bar"\n' == table.as_string()
assert table.as_string() == 'foo = "bar"\n'

table.remove(Key("foo"))

assert "" == table.as_string()
assert table.as_string() == ""

with pytest.raises(NonExistentKey):
table.remove(Key("foo"))
Expand All @@ -215,23 +214,23 @@ def test_items_can_be_appended_to_and_removed_from_an_inline_table():
_, table = parser._parse_item()

assert isinstance(table, InlineTable)
assert "{}" == table.as_string()
assert table.as_string() == "{}"

table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="")))

assert '{foo = "bar"}' == table.as_string()
assert table.as_string() == '{foo = "bar"}'

table.append(Key("baz"), Integer(34, Trivia(trail=""), "34"))

assert '{foo = "bar", baz = 34}' == table.as_string()
assert table.as_string() == '{foo = "bar", baz = 34}'

table.remove(Key("baz"))

assert '{foo = "bar"}' == table.as_string()
assert table.as_string() == '{foo = "bar"}'

table.remove(Key("foo"))

assert "{}" == table.as_string()
assert table.as_string() == "{}"

with pytest.raises(NonExistentKey):
table.remove(Key("foo"))
Expand Down Expand Up @@ -339,7 +338,7 @@ def test_array_multiline():
t = item([])
t.multiline(True)

assert "[]" == t.as_string()
assert t.as_string() == "[]"


def test_array_multiline_modify():
Expand Down Expand Up @@ -791,19 +790,19 @@ def test_trim_comments_when_building_inline_table():
value.comment("Another comment")
table.append("baz", value)
assert "# Another comment" not in table.as_string()
assert '{foo = "bar", baz = "foobaz"}' == table.as_string()
assert table.as_string() == '{foo = "bar", baz = "foobaz"}'


def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():
table = api.inline_table()
table["foo"] = "bar"
table["baz"] = "boom"

assert '{foo = "bar", baz = "boom"}' == table.as_string()
assert table.as_string() == '{foo = "bar", baz = "boom"}'

del table["baz"]

assert '{foo = "bar"}' == table.as_string()
assert table.as_string() == '{foo = "bar"}'

table = api.inline_table()
table["foo"] = "bar"
Expand All @@ -812,7 +811,7 @@ def test_deleting_inline_table_elemeent_does_not_leave_trailing_separator():

table["baz"] = "boom"

assert '{baz = "boom"}' == table.as_string()
assert table.as_string() == '{baz = "boom"}'


def test_booleans_comparison():
Expand Down
23 changes: 11 additions & 12 deletions tests/test_toml_document.py
Expand Up @@ -9,14 +9,13 @@

import tomlkit

from tests.util import assert_is_ppo
from tomlkit import parse
from tomlkit import ws
from tomlkit._utils import _utc
from tomlkit.api import document
from tomlkit.exceptions import NonExistentKey

from .util import assert_is_ppo


def test_document_is_a_dict(example):
content = example("example")
Expand Down Expand Up @@ -461,7 +460,8 @@ def test_getting_inline_table_is_still_an_inline_table():
dev_dependencies["baz"]["source"] = "other"

assert (
"""\
doc.as_string()
== """\
[tool.poetry]
name = "foo"

Expand All @@ -472,7 +472,6 @@ def test_getting_inline_table_is_still_an_inline_table():
[tool.poetry.dev-dependencies]
baz = {version = "^4.0", source = "other"}
"""
== doc.as_string()
)


Expand Down Expand Up @@ -508,7 +507,7 @@ def test_values_can_still_be_set_for_out_of_order_tables():
doc = parse(content)
doc["a"]["a"]["key"] = "new_value"

assert "new_value" == doc["a"]["a"]["key"]
assert doc["a"]["a"]["key"] == "new_value"

expected = """
[a.a]
Expand Down Expand Up @@ -588,18 +587,18 @@ def test_out_of_order_tables_are_still_dicts():
table = doc["a"]["a"]
assert "key" in table
assert "c" in table
assert "value" == table.get("key")
assert table.get("key") == "value"
assert {} == table.get("c")
assert table.get("d") is None
assert "foo" == table.get("d", "foo")
assert table.get("d", "foo") == "foo"

assert "bar" == table.setdefault("d", "bar")
assert "bar" == table["d"]
assert table.setdefault("d", "bar") == "bar"
assert table["d"] == "bar"

assert "value" == table.pop("key")
assert table.pop("key") == "value"
assert "key" not in table

assert "baz" == table.pop("missing", default="baz")
assert table.pop("missing", default="baz") == "baz"

with pytest.raises(KeyError):
table.pop("missing")
Expand Down Expand Up @@ -629,7 +628,7 @@ def test_string_output_order_is_preserved_for_out_of_order_tables():
constraint["version"] = "^1.0"
doc["tool"]["poetry"]["dependencies"]["bar"] = constraint

assert "^1.0" == doc["tool"]["poetry"]["dependencies"]["bar"]["version"]
assert doc["tool"]["poetry"]["dependencies"]["bar"]["version"] == "^1.0"

expected = """
[tool.poetry]
Expand Down
7 changes: 3 additions & 4 deletions tests/test_toml_spec_tests.py
Expand Up @@ -78,7 +78,7 @@ def untag(value):
elif value["type"] == "array":
return [untag(i) for i in value["value"]]
else:
raise Exception("Unsupported type {}".format(value["type"]))
raise Exception(f'Unsupported type {value["type"]}')
else:
return {k: untag(v) for k, v in value.items()}

Expand Down Expand Up @@ -107,6 +107,5 @@ def test_valid_decode(test):
@pytest.mark.parametrize("test", ERROR_TESTS)
def test_invalid_decode(test):
toml_file = os.path.join(SPEC_TEST_DIR, "errors", test + ".toml")
with pytest.raises(TOMLKitError):
with open(toml_file, encoding="utf-8") as f:
parse(f.read())
with pytest.raises(TOMLKitError), open(toml_file, encoding="utf-8") as f:
parse(f.read())
9 changes: 5 additions & 4 deletions tests/test_toml_tests.py
Expand Up @@ -38,7 +38,7 @@ def untag(value):
elif value["type"] == "array":
return [untag(i) for i in value["value"]]
else:
raise Exception("Unsupported type {}".format(value["type"]))
raise Exception(f'Unsupported type {value["type"]}')
else:
return {k: untag(v) for k, v in value.items()}

Expand All @@ -57,6 +57,7 @@ def test_invalid_decode(invalid_decode_case):


def test_invalid_encode(invalid_encode_case):
with pytest.raises((TOMLKitError, UnicodeDecodeError)):
with open(invalid_encode_case, encoding="utf-8") as f:
load(f)
with pytest.raises((TOMLKitError, UnicodeDecodeError)), open(
invalid_encode_case, encoding="utf-8"
) as f:
load(f)
12 changes: 6 additions & 6 deletions tests/util.py
Expand Up @@ -43,15 +43,15 @@


def assert_not_tomlkit_type(v):
for i, T in enumerate(TOMLKIT_TYPES):
assert not isinstance(v, T)
for _, tomlkit_type in enumerate(TOMLKIT_TYPES):
assert not isinstance(v, tomlkit_type)


def assert_is_ppo(v_unwrapped, unwrappedType):
def assert_is_ppo(v_unwrapped, unwrapped_type):
assert_not_tomlkit_type(v_unwrapped)
assert isinstance(v_unwrapped, unwrappedType)
assert isinstance(v_unwrapped, unwrapped_type)


def elementary_test(v, unwrappedType):
def elementary_test(v, unwrapped_type):
v_unwrapped = v.unwrap()
assert_is_ppo(v_unwrapped, unwrappedType)
assert_is_ppo(v_unwrapped, unwrapped_type)