-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: merge dev tool configurations into
pyproject.toml
(#146)
* FEAT: merge mypy configuration into pyproject.toml * FEAT: merge pyright configuration into pyproject.toml * FEAT: merge pytest config into pyproject.toml
- Loading branch information
Showing
12 changed files
with
244 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Check and update :code:`mypy` settings.""" | ||
import os | ||
|
||
import tomlkit | ||
from ini2toml.api import Translator | ||
|
||
from repoma.errors import PrecommitError | ||
from repoma.utilities import CONFIG_PATH | ||
from repoma.utilities.pyproject import get_sub_table, load_pyproject, write_pyproject | ||
|
||
|
||
def main() -> None: | ||
_merge_mypy_into_pyproject() | ||
|
||
|
||
def _merge_mypy_into_pyproject() -> None: | ||
config_path = ".mypy.ini" | ||
if not os.path.exists(config_path): | ||
return | ||
with open(config_path) as stream: | ||
original_contents = stream.read() | ||
toml_str = Translator().translate(original_contents, profile_name=config_path) | ||
mypy_config = tomlkit.parse(toml_str) | ||
pyproject = load_pyproject() | ||
tool_table = get_sub_table(pyproject, "tool", create=True) | ||
tool_table.update(mypy_config) | ||
write_pyproject(pyproject) | ||
os.remove(config_path) | ||
msg = f"Moved mypy configuration to {CONFIG_PATH.pyproject}" | ||
raise PrecommitError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Check and update :code:`mypy` settings.""" | ||
import json | ||
import os | ||
|
||
from repoma.errors import PrecommitError | ||
from repoma.utilities import CONFIG_PATH | ||
from repoma.utilities.executor import Executor | ||
from repoma.utilities.pyproject import ( | ||
complies_with_subset, | ||
get_sub_table, | ||
load_pyproject, | ||
to_toml_array, | ||
write_pyproject, | ||
) | ||
|
||
|
||
def main() -> None: | ||
executor = Executor() | ||
executor(_merge_config_into_pyproject) | ||
executor(_update_settings) | ||
executor.finalize() | ||
|
||
|
||
def _merge_config_into_pyproject() -> None: | ||
config_path = "pyrightconfig.json" # cspell:ignore pyrightconfig | ||
if not os.path.exists(config_path): | ||
return | ||
with open(config_path) as stream: | ||
existing_config = json.load(stream) | ||
for key, value in existing_config.items(): | ||
if isinstance(value, list): | ||
existing_config[key] = to_toml_array(sorted(value)) | ||
pyproject = load_pyproject() | ||
tool_table = get_sub_table(pyproject, "tool.pyright", create=True) | ||
tool_table.update(existing_config) | ||
write_pyproject(pyproject) | ||
os.remove(config_path) | ||
msg = f"Moved pyright configuration to {CONFIG_PATH.pyproject}" | ||
raise PrecommitError(msg) | ||
|
||
|
||
def _update_settings() -> None: | ||
pyproject = load_pyproject() | ||
settings = get_sub_table(pyproject, "tool.pyright", create=True) | ||
minimal_settings = { | ||
"typeCheckingMode": "strict", | ||
} | ||
if not complies_with_subset(settings, minimal_settings): | ||
settings.update(minimal_settings) | ||
write_pyproject(pyproject) | ||
msg = f"Updated black configuration in {CONFIG_PATH.pyproject}" | ||
raise PrecommitError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Check and update :code:`pytest` settings.""" | ||
import os | ||
from typing import Dict | ||
|
||
import tomlkit | ||
from ini2toml.api import Translator | ||
|
||
from repoma.errors import PrecommitError | ||
from repoma.utilities import CONFIG_PATH | ||
from repoma.utilities.cfg import open_config | ||
from repoma.utilities.executor import Executor | ||
from repoma.utilities.pyproject import get_sub_table, load_pyproject, write_pyproject | ||
|
||
__PYTEST_INI_PATH = "pytest.ini" | ||
|
||
|
||
def main() -> None: | ||
executor = Executor() | ||
executor(_merge_coverage_into_pyproject) | ||
executor(_merge_pytest_into_pyproject) | ||
executor(_remove_pytest_ini) | ||
executor.finalize() | ||
|
||
|
||
def _merge_coverage_into_pyproject() -> None: | ||
if not os.path.exists(__PYTEST_INI_PATH): | ||
return | ||
pytest_ini = open_config(__PYTEST_INI_PATH) | ||
section_name = "coverage:run" | ||
if not pytest_ini.has_section(section_name): | ||
return | ||
coverage_config: Dict = dict(pytest_ini[section_name]) | ||
for key, value in coverage_config.items(): | ||
if value in {"False", "True"}: | ||
coverage_config[key] = bool(value) | ||
if key == "source" and isinstance(value, str): | ||
coverage_config[key] = [value] | ||
pyproject = load_pyproject() | ||
tool_table = get_sub_table(pyproject, "tool.coverage.run", create=True) | ||
tool_table.update(coverage_config) | ||
write_pyproject(pyproject) | ||
msg = f"Moved Coverage.py configuration to {CONFIG_PATH.pyproject}" | ||
raise PrecommitError(msg) | ||
|
||
|
||
def _merge_pytest_into_pyproject() -> None: | ||
if not os.path.exists(__PYTEST_INI_PATH): | ||
return | ||
with open(__PYTEST_INI_PATH) as stream: | ||
original_contents = stream.read() | ||
toml_str = Translator().translate(original_contents, profile_name=__PYTEST_INI_PATH) | ||
config = tomlkit.parse(toml_str) | ||
config.pop("coverage:run", None) | ||
pyproject = load_pyproject() | ||
tool_table = get_sub_table(pyproject, "tool", create=True) | ||
tool_table.update(config) | ||
write_pyproject(pyproject) | ||
msg = f"Moved pytest configuration to {CONFIG_PATH.pyproject}" | ||
raise PrecommitError(msg) | ||
|
||
|
||
def _remove_pytest_ini() -> None: | ||
if not os.path.exists(__PYTEST_INI_PATH): | ||
return | ||
os.remove(__PYTEST_INI_PATH) | ||
msg = f"Removed {__PYTEST_INI_PATH}" | ||
raise PrecommitError(msg) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.