diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4344aeba1..42514f74b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,14 @@ default_language_version: - python: python3 + python: python3 ci: - autofix_commit_msg: | - [pre-commit.ci] auto fixes from pre-commit hooks - autofix_prs: true - autoupdate_branch: 'pre-commit-autoupdate' - autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate' - autoupdate_schedule: monthly - skip: [no-commit-to-branch] - submodules: false + autofix_commit_msg: | + [pre-commit.ci] auto fixes from pre-commit hooks + autofix_prs: true + autoupdate_branch: "pre-commit-autoupdate" + autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate" + autoupdate_schedule: monthly + skip: [no-commit-to-branch] + submodules: false repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 @@ -16,7 +16,6 @@ repos: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - exclude: '\.(rst|txt)$' - id: check-case-conflict - id: check-merge-conflict - id: check-toml @@ -25,11 +24,11 @@ repos: rev: 24.4.2 hooks: - id: black + exclude: ^tests/test_helpers.py$|^tests/test_commands.py$ - repo: https://github.com/pycqa/flake8 rev: 7.0.0 hooks: - id: flake8 - exclude: ^tests/test_helpers.py$|^tests/test_commands.py$ - repo: https://github.com/pycqa/isort rev: 5.13.2 hooks: @@ -49,6 +48,21 @@ repos: - repo: https://github.com/codespell-project/codespell rev: v2.3.0 hooks: - - id: codespell - additional_dependencies: - - tomli + - id: codespell + additional_dependencies: + - tomli + # prettier - multi formatter for .json, .yml, and .md files + - repo: https://github.com/pre-commit/mirrors-prettier + rev: f12edd9c7be1c20cfa42420fd0e6df71e42b51ea # frozen: v4.0.0-alpha.8 + hooks: + - id: prettier + types: [yaml] + additional_dependencies: + - "prettier@^3.2.4" + # docformatter - PEP 257 compliant docstring formatter + - repo: https://github.com/s-weigand/docformatter + rev: 5757c5190d95e5449f102ace83df92e7d3b06c6c + hooks: + - id: docformatter + additional_dependencies: [tomli] + args: [--in-place, --config, ./pyproject.toml] diff --git a/news/prettier_yml_dump.rst b/news/prettier_yml_dump.rst new file mode 100644 index 000000000..9e5aa3b25 --- /dev/null +++ b/news/prettier_yml_dump.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* update yaml.dumper in fsclient so that it outputs code that passes prettier and black tests + +**Security:** + +* diff --git a/src/regolith/fsclient.py b/src/regolith/fsclient.py index 37336ae82..f2026a521 100644 --- a/src/regolith/fsclient.py +++ b/src/regolith/fsclient.py @@ -37,7 +37,7 @@ def __exit__(self, type, value, traceback): def _rec_re_type(i): - """Destroy this when ruamel.yaml supports basetypes again""" + """Destroy this when ruamel.yaml supports basetypes again.""" if type(i) in YAML_BASE_MAP: base = YAML_BASE_MAP[type(i)]() if isinstance(base, dict): @@ -97,8 +97,11 @@ def load_yaml(filename, return_inst=False, loader=None): def dump_yaml(filename, docs, inst=None): """Dumps a dict of documents into a file.""" inst = YAML() if inst is None else inst + inst.indent(mapping=2, sequence=4, offset=2) # Match Prettier's default + inst.preserve_quotes = True # Prevent unnecessary quote changes + inst.width = 79 # Keep line width under control + inst.explicit_start = True # Add `---` at the start, Prettier is fine with it inst.representer.ignore_aliases = lambda *data: True - inst.indent(mapping=2, sequence=4, offset=2) sorted_dict = ruamel.yaml.comments.CommentedMap() for k in sorted(docs): doc = docs[k] @@ -187,14 +190,14 @@ def load_database(self, db): self.load_yaml(db, dbpath) def dump_json(self, docs, collname, dbpath): - """Dumps json docs and returns filename""" + """Dumps json docs and returns filename.""" f = os.path.join(dbpath, collname + ".json") dump_json(f, docs) filename = os.path.split(f)[-1] return filename def dump_yaml(self, docs, collname, dbpath): - """Dumps json docs and returns filename""" + """Dumps json docs and returns filename.""" f = os.path.join(dbpath, collname + self._collexts.get(collname, ".yaml")) inst = self._yamlinsts.get((dbpath, collname), None) dump_yaml(f, docs, inst=inst) @@ -250,7 +253,7 @@ def insert_many(self, dbname, collname, docs): coll[doc["_id"]] = doc def delete_one(self, dbname, collname, doc): - """Removes a single document from a collection""" + """Removes a single document from a collection.""" coll = self.dbs[dbname][collname] del coll[doc["_id"]]