Skip to content

Commit

Permalink
[WIP] Allow multiple globs in *_BY_PATH dicts
Browse files Browse the repository at this point in the history
Before this commit, there was repetition in the PRE_COMMIT_REPOS_BY_PATH
dictionary:

    PRE_COMMIT_REPOS_BY_PATH: Final = (
        ...,
        ('**.yaml', PCR_YAMLLINT),
        ('**.yml', PCR_YAMLLINT),
        ...
    )

That repetition was necessary. Before this change, I had to repeat
myself in order to specify that both .yaml files and .yml files should
enable PCR_YAMLLINT.

I want to be able to specify multiple globs to make it easier for me to
create two future commits: a type-stub-related commit and a
wcwidth-related commit. The wcwidth-related commit will add code that
depends on wcwidth [1]. Unfortunately, wcwidth doesn’t have any type
hints yet [2], and there doesn’t seem to be a PEP 561 [3] stub package
for wcwidth. When a module isn’t typed and there’s no stub package for
that module, mypy’s recommendation is to write your own stub files [4].

Before that future wcwidth-related commit, I’m going to add a
type-stub-related commit. That type-stub-related commit will make it so
that all of the pre-commit hooks for .py files also run for .pyi files
[5].

The main motivation behind this commit is to make it easier for me to
write that future type-stub-related commit.

[1]: <https://pypi.org/project/wcwidth/>
[2]: <jquast/wcwidth#71>
[3]: <https://peps.python.org/pep-0561/>
[4]: <https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker>
[5]: <https://mypy.readthedocs.io/en/stable/stubs.html>

TODO:
• Finish the commits that come after this one.
• Make sure that links are backed up in the Internet Archive
  • Loading branch information
Jayman2000 committed Jan 25, 2024
1 parent 8ad8a68 commit 447014e
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions jasons_pre_commit_hooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
See [`copying.md`](./copying.md).
"""


YAML_GLOBS: Final = ('**.yml', '**.yaml')


HINTS_FOR_CONTRIBUTORS_HEADING: Final = "## Hints for Contributors\n"
HFC_LINE_LENGTH: Final = \
"- Try to keep lines shorter than seventy-three characters."
Expand Down Expand Up @@ -97,12 +101,12 @@
3. Run `pre-commit install-hooks`
"""
HINTS_FOR_CONTRIBUTORS_BY_PATH: Final = (
('**', HFC_LINE_LENGTH),
('.pre-commit-config.yaml', HFC_PRE_COMMIT),
('.pre-commit-config.yaml', HFC_PRE_COMMIT_LINKS),
('.editorconfig', HFC_EDITOR_CONFIG),
('**.md', HFC_MARKDOWN),
('**.py', HFC_RUFF)
(('**',), HFC_LINE_LENGTH),
(('.pre-commit-config.yaml',), HFC_PRE_COMMIT),
(('.pre-commit-config.yaml',), HFC_PRE_COMMIT_LINKS),
(('.editorconfig',), HFC_EDITOR_CONFIG),
(('**.md',), HFC_MARKDOWN),
(('**.py',), HFC_RUFF)
)

# Pre-commit hooks shouldn’t mess with the files in the LICENSES/
Expand Down Expand Up @@ -191,20 +195,19 @@ class PreCommitRepoInfo(NamedTuple):
hook_ids=('validate_manifest',)
)
PRE_COMMIT_REPOS_BY_PATH: Final = (
('**', PCR_REUSE),
('.pre-commit-hooks.yaml', PCR_PRE_COMMIT_UPDATE),
('.editorconfig', PCR_EDITORCONFIG_CHECKER),
('**', PCR_OFFICIAL_HOOKS),
('**.py', PCR_OFFICIAL_HOOKS_PYTHON),
('**', PCR_PYGREP_HOOKS),
('**', PCR_GITLEAKS),
('**.toml', PCR_LANGUAGE_FORMATTERS),
('**.yaml', PCR_YAMLLINT),
('**.yml', PCR_YAMLLINT),
('**.md', PCR_MARKDOWNLINT_CLI),
('**.py', PCR_MYPY),
('**.py', PCR_RUFF),
('.pre-commit-hooks.yaml', PCR_PRE_COMMIT_ITSELF),
(('**',), PCR_REUSE),
(('.pre-commit-hooks.yaml',), PCR_PRE_COMMIT_UPDATE),
(('.editorconfig',), PCR_EDITORCONFIG_CHECKER),
(('**',), PCR_OFFICIAL_HOOKS),
(('**.py',), PCR_OFFICIAL_HOOKS_PYTHON),
(('**',), PCR_PYGREP_HOOKS),
(('**',), PCR_GITLEAKS),
(('**.toml',), PCR_LANGUAGE_FORMATTERS),
(YAML_GLOBS, PCR_YAMLLINT),
(('**.md',), PCR_MARKDOWNLINT_CLI),
(('**.py',), PCR_MYPY),
(('**.py',), PCR_RUFF),
(('.pre-commit-hooks.yaml',), PCR_PRE_COMMIT_ITSELF),
)


Expand Down Expand Up @@ -541,42 +544,46 @@ def repo_style_checker() -> int:
return 1
# Do the Hints for Contributors contain some standard hints for
# certain files?
glob: str
globs: Iterable[str]
hint: str
for glob, hint in HINTS_FOR_CONTRIBUTORS_BY_PATH:
for globs, hint in HINTS_FOR_CONTRIBUTORS_BY_PATH:
path: pathlib.Path
for path in PATHS:
if path.match(glob, case_sensitive=False):
if hint not in README_CONTENTS:
hint_indented: str = textwrap.indent(hint, "\t")
print(
f"ERROR: {README_PATH} doesn’t contain this",
f"hint for contributors:\n\n{hint_indented}\n",
file=sys.stderr
)
print(
f"(glob {glob} matched by file {path})",
file=sys.stderr
)
break
glob: str
for glob in globs:
if path.match(glob, case_sensitive=False):
if hint not in README_CONTENTS:
hint_indented: str = textwrap.indent(hint, "\t")
print(
f"ERROR: {README_PATH} doesn’t contain",
"this hint for",
f"contributors:\n\n{hint_indented}\n",
file=sys.stderr
)
print(
f"(glob {glob} matched by file {path})",
file=sys.stderr
)
break
# Does the pre-commit config contain some standard hooks for certain
# files?
missing_hooks: bool = False
PC_CONFIG: Final = yaml.safe_load(
PC_CONFIG_PATH.read_text(encoding='utf_8')
)
repo_info: PreCommitRepoInfo
for glob, repo_info in PRE_COMMIT_REPOS_BY_PATH:
for globs, repo_info in PRE_COMMIT_REPOS_BY_PATH:
for path in PATHS:
if path.match(glob, case_sensitive=False):
hooks_found: bool = check_pc_config_hooks(
PC_CONFIG,
repo_info,
glob
)
if not hooks_found:
missing_hooks = True
break
for glob in globs:
if path.match(glob, case_sensitive=False):
hooks_found: bool = check_pc_config_hooks(
PC_CONFIG,
repo_info,
glob
)
if not hooks_found:
missing_hooks = True
break
if missing_hooks:
return 1

Expand Down

0 comments on commit 447014e

Please sign in to comment.