Skip to content

Commit b8de78d

Browse files
committed
Fix linting issues and reformat code
1 parent 511bf0e commit b8de78d

9 files changed

Lines changed: 69 additions & 51 deletions

File tree

src/mxdev/config.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from .hooks import Hook
1212

1313

14-
1514
def to_bool(value):
1615
if not isinstance(value, str):
1716
return bool(value)
@@ -73,17 +72,20 @@ def __contains__(self, key):
7372
def read_toml(path: str | Path) -> TomlWrapper:
7473
try:
7574
import tomllib
75+
76+
loader = tomllib
7677
except ImportError:
7778
try:
78-
import tomli as tomllib
79+
import tomli as toml_backport
80+
81+
loader = toml_backport # type: ignore
7982
except ImportError:
8083
raise ImportError(
81-
"TOML support requires Python 3.11+ or the 'tomli' package. "
82-
"Install it with 'pip install mxdev[toml]'."
84+
"TOML support requires Python 3.11+ or the 'tomli' package. Install it with 'pip install mxdev[toml]'."
8385
)
8486

8587
with open(path, "rb") as f:
86-
data = tomllib.load(f)
88+
data = loader.load(f)
8789

8890
if "tool" not in data or "mxdev" not in data["tool"]:
8991
return TomlWrapper({"settings": {}})
@@ -117,6 +119,7 @@ def __init__(
117119
hooks: list["Hook"] = [],
118120
) -> None:
119121
logger.debug("Read configuration")
122+
data: typing.Any
120123
if mxini.endswith(".toml"):
121124
data = read_toml(mxini)
122125
else:

src/mxdev/processing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ def resolve_dependencies(
221221
cache_dir,
222222
)
223223
else:
224-
logger.info(
225-
f"Can not read {variety_verbose} file '{file_or_url}', " "it does not exist. Empty file assumed."
226-
)
224+
logger.info(f"Can not read {variety_verbose} file '{file_or_url}', it does not exist. Empty file assumed.")
227225
else:
228226
# HTTP(S) URL handling with caching
229227
content: str
@@ -358,7 +356,7 @@ def write_dev_sources(fio, packages: dict[str, dict[str, typing.Any]], state: St
358356

359357
# Add -e prefix only for 'editable' mode (not for 'fixed')
360358
prefix = "-e " if package["install-mode"] == "editable" else ""
361-
install_line = f"""{prefix}./{package['target']}/{name}{subdir}{extras}"""
359+
install_line = f"""{prefix}./{package["target"]}/{name}{subdir}{extras}"""
362360

363361
if not source_path.exists():
364362
# Source not checked out yet - write as comment

src/mxdev/vcs/bazaar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def checkout(self, **kwargs):
6969
self.output((logger.info, f"Skipped checkout of existing package {name!r}."))
7070
else:
7171
raise BazaarError(
72-
"Source URL for existing package {!r} differs. " "Expected {!r}.".format(name, self.source["url"])
72+
"Source URL for existing package {!r} differs. Expected {!r}.".format(name, self.source["url"])
7373
)
7474
else:
7575
return self.bzr_branch(**kwargs)

src/mxdev/vcs/common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ def get_workingcopytypes() -> dict[str, type[BaseWorkingCopy]]:
150150
workingcopytype = entrypoint.load()
151151
if key in addons:
152152
logger.error(
153-
f"Duplicate workingcopy types registration '{key}' at "
154-
f"{entrypoint.value} can not override {addons[key]}"
153+
f"Duplicate workingcopy types registration '{key}' at {entrypoint.value} can not override {addons[key]}"
155154
)
156155
sys.exit(1)
157156
addons[key] = workingcopytype

src/mxdev/vcs/filesystem.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def checkout(self, **kwargs) -> str | None:
2424
)
2525
else:
2626
raise FilesystemError(
27-
"Directory name for existing package {!r} differs. " "Expected {!r}.".format(
28-
name, self.source["url"]
29-
)
27+
"Directory name for existing package {!r} differs. Expected {!r}.".format(name, self.source["url"])
3028
)
3129
else:
3230
raise FilesystemError(
@@ -48,7 +46,7 @@ def update(self, **kwargs):
4846
name = self.source["name"]
4947
if not self.matches():
5048
raise FilesystemError(
51-
"Directory name for existing package {!r} differs. " "Expected {!r}.".format(name, self.source["url"])
49+
"Directory name for existing package {!r} differs. Expected {!r}.".format(name, self.source["url"])
5250
)
5351
self.output((logger.info, f"Filesystem package {name!r} doesn't need update."))
5452
return ""

src/mxdev/vcs/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, source: dict[str, str]):
2929
self.git_executable = common.which("git")
3030
if "rev" in source and "revision" in source:
3131
raise ValueError(
32-
"The source definition of '{}' contains " "duplicate revision options.".format(source["name"])
32+
"The source definition of '{}' contains duplicate revision options.".format(source["name"])
3333
)
3434
# 'rev' is canonical
3535
if "revision" in source:
@@ -41,7 +41,7 @@ def __init__(self, source: dict[str, str]):
4141
del source["branch"]
4242
elif "branch" in source:
4343
logger.error(
44-
"Cannot specify both branch (%s) and rev/revision " "(%s) in source for %s",
44+
"Cannot specify both branch (%s) and rev/revision (%s) in source for %s",
4545
source["branch"],
4646
source["rev"],
4747
source["name"],

src/mxdev/vcs/mercurial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def checkout(self, **kwargs):
173173
self.output((logger.info, f"Skipped checkout of existing package {name!r}."))
174174
else:
175175
raise MercurialError(
176-
"Source URL for existing package {!r} differs. " "Expected {!r}.".format(name, self.source["url"])
176+
"Source URL for existing package {!r} differs. Expected {!r}.".format(name, self.source["url"])
177177
)
178178
else:
179179
return self.hg_clone(**kwargs)

tests/test_config_discovery.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import os
2-
from unittest.mock import patch
31
from mxdev.main import main
4-
import pytest
2+
from unittest.mock import patch
3+
54

65
def test_discovery_mx_ini_exists(tmp_path, monkeypatch):
76
"""If mx.ini exists, it should be preferred over pyproject.toml."""
87
mxini = tmp_path / "mx.ini"
98
mxini.write_text("[settings]\nrequirements-in = mx-reqs.txt", encoding="utf-8")
10-
9+
1110
pyproject = tmp_path / "pyproject.toml"
1211
pyproject.write_text("[tool.mxdev.settings]\nrequirements-in = toml-reqs.txt", encoding="utf-8")
13-
12+
1413
monkeypatch.chdir(tmp_path)
15-
14+
1615
import sys
16+
1717
main_module = sys.modules["mxdev.main"]
18-
18+
1919
with (
2020
patch("sys.argv", ["mxdev"]),
2121
patch.object(main_module, "load_hooks", return_value=[]),
@@ -29,16 +29,18 @@ def test_discovery_mx_ini_exists(tmp_path, monkeypatch):
2929
mock_config.assert_called_once()
3030
assert mock_config.call_args[1]["mxini"] == "mx.ini"
3131

32+
3233
def test_discovery_pyproject_fallback(tmp_path, monkeypatch):
3334
"""If mx.ini is missing, it should fallback to pyproject.toml."""
3435
pyproject = tmp_path / "pyproject.toml"
3536
pyproject.write_text("[tool.mxdev.settings]\nrequirements-in = toml-reqs.txt", encoding="utf-8")
36-
37+
3738
monkeypatch.chdir(tmp_path)
38-
39+
3940
import sys
41+
4042
main_module = sys.modules["mxdev.main"]
41-
43+
4244
with (
4345
patch("sys.argv", ["mxdev"]),
4446
patch.object(main_module, "load_hooks", return_value=[]),
@@ -52,19 +54,21 @@ def test_discovery_pyproject_fallback(tmp_path, monkeypatch):
5254
mock_config.assert_called_once()
5355
assert mock_config.call_args[1]["mxini"] == "pyproject.toml"
5456

57+
5558
def test_discovery_explicit_flag(tmp_path, monkeypatch):
5659
"""Explicit flag should override discovery."""
5760
mxini = tmp_path / "mx.ini"
5861
mxini.write_text("[settings]", encoding="utf-8")
59-
62+
6063
custom = tmp_path / "custom.toml"
6164
custom.write_text("[tool.mxdev.settings]", encoding="utf-8")
62-
65+
6366
monkeypatch.chdir(tmp_path)
64-
67+
6568
import sys
69+
6670
main_module = sys.modules["mxdev.main"]
67-
71+
6872
with (
6973
patch("sys.argv", ["mxdev", "-c", "custom.toml"]),
7074
patch.object(main_module, "load_hooks", return_value=[]),
@@ -78,13 +82,15 @@ def test_discovery_explicit_flag(tmp_path, monkeypatch):
7882
mock_config.assert_called_once()
7983
assert mock_config.call_args[1]["mxini"] == "custom.toml"
8084

85+
8186
def test_discovery_no_config(tmp_path, monkeypatch):
8287
"""If no config found, fallback to mx.ini default (to trigger existing error behavior)."""
8388
monkeypatch.chdir(tmp_path)
84-
89+
8590
import sys
91+
8692
main_module = sys.modules["mxdev.main"]
87-
93+
8894
with (
8995
patch("sys.argv", ["mxdev"]),
9096
patch.object(main_module, "load_hooks", return_value=[]),

tests/test_toml.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
from mxdev.config import Configuration
2+
from mxdev.config import read_toml
3+
from unittest.mock import patch
4+
15
import pytest
2-
from mxdev.config import read_toml, Configuration
3-
from pathlib import Path
4-
import os
6+
57

68
def test_read_toml_basic(tmp_path):
79
pyproject = tmp_path / "pyproject.toml"
8-
pyproject.write_text("""
10+
pyproject.write_text(
11+
"""
912
[tool.mxdev.settings]
1013
threads = 10
1114
requirements-in = "reqs.txt"
@@ -16,8 +19,10 @@ def test_read_toml_basic(tmp_path):
1619
1720
[tool.mxdev.hooks.myhook]
1821
enabled = "true"
19-
""", encoding="utf-8")
20-
22+
""",
23+
encoding="utf-8",
24+
)
25+
2126
wrapper = read_toml(pyproject)
2227
assert wrapper["settings"]["threads"] == "10"
2328
assert wrapper["pkg1"]["url"] == "https://example.com/pkg1.git"
@@ -26,55 +31,64 @@ def test_read_toml_basic(tmp_path):
2631
assert "pkg1" in wrapper.sections()
2732
assert "myhook" in wrapper.sections()
2833

34+
2935
def test_read_toml_no_section(tmp_path):
3036
pyproject = tmp_path / "pyproject.toml"
3137
pyproject.write_text("[tool.other]\nkey = 'val'", encoding="utf-8")
32-
38+
3339
wrapper = read_toml(pyproject)
3440
assert dict(wrapper["settings"].items()) == {}
3541
assert wrapper.sections() == []
3642

43+
3744
def test_configuration_toml(tmp_path):
3845
pyproject = tmp_path / "pyproject.toml"
39-
pyproject.write_text("""
46+
pyproject.write_text(
47+
"""
4048
[tool.mxdev.settings]
4149
requirements-in = "requirements-in.txt"
4250
4351
[tool.mxdev.packages.pkg1]
4452
url = "https://example.com/pkg1.git"
45-
""", encoding="utf-8")
46-
53+
""",
54+
encoding="utf-8",
55+
)
56+
4757
# We need to be in the directory or provide absolute path
4858
config = Configuration(str(pyproject))
4959
assert config.infile == "requirements-in.txt"
5060
assert "pkg1" in config.packages
5161
assert config.packages["pkg1"]["url"] == "https://example.com/pkg1.git"
5262

63+
5364
def test_read_toml_types_to_strings(tmp_path):
5465
pyproject = tmp_path / "pyproject.toml"
55-
pyproject.write_text("""
66+
pyproject.write_text(
67+
"""
5668
[tool.mxdev.settings]
5769
threads = 10
5870
offline = true
5971
6072
[tool.mxdev.packages.pkg1]
6173
url = "https://example.com/pkg1.git"
6274
use = false
63-
""", encoding="utf-8")
64-
75+
""",
76+
encoding="utf-8",
77+
)
78+
6579
wrapper = read_toml(pyproject)
6680
assert wrapper["settings"]["threads"] == "10"
6781
assert wrapper["settings"]["offline"] == "True" # TOML bool becomes Python bool then str()
6882
assert wrapper["pkg1"]["use"] == "False"
6983

84+
7085
def test_read_toml_no_parser(monkeypatch):
7186
import sys
87+
7288
# Mock both tomllib and tomli being missing
73-
with patch.dict(sys.modules, {'tomllib': None, 'tomli': None}):
89+
with patch.dict(sys.modules, {"tomllib": None, "tomli": None}):
7490
# We need to reload mxdev.config to apply the mock if it was already imported
7591
# but read_toml imports them inside the function, so it should work.
7692
with pytest.raises(ImportError) as excinfo:
7793
read_toml("any.toml")
7894
assert "pip install mxdev[toml]" in str(excinfo.value)
79-
80-
from unittest.mock import patch

0 commit comments

Comments
 (0)