|
17 | 17 | from packaging.specifiers import SpecifierSet |
18 | 18 |
|
19 | 19 | from parse_metadata import read_metadata |
20 | | -from utils import VERSIONS_RE, get_all_testcase_directories, get_gitignore_spec, spec_matches_path, strip_comments |
| 20 | +from utils import ( |
| 21 | + REQS_FILE, |
| 22 | + VERSIONS_RE, |
| 23 | + get_all_testcase_directories, |
| 24 | + get_gitignore_spec, |
| 25 | + parse_requirements, |
| 26 | + spec_matches_path, |
| 27 | + strip_comments, |
| 28 | +) |
21 | 29 |
|
22 | 30 | extension_descriptions = {".pyi": "stub", ".py": ".py"} |
23 | 31 |
|
@@ -135,13 +143,6 @@ def check_metadata() -> None: |
135 | 143 | read_metadata(distribution) |
136 | 144 |
|
137 | 145 |
|
138 | | -def get_txt_requirements() -> dict[str, SpecifierSet]: |
139 | | - with open("requirements-tests.txt", encoding="UTF-8") as requirements_file: |
140 | | - stripped_lines = map(strip_comments, requirements_file) |
141 | | - requirements = map(Requirement, filter(None, stripped_lines)) |
142 | | - return {requirement.name: requirement.specifier for requirement in requirements} |
143 | | - |
144 | | - |
145 | 146 | class PreCommitConfigRepos(TypedDict): |
146 | 147 | hooks: list[dict[str, str]] |
147 | 148 | repo: str |
@@ -172,30 +173,30 @@ def get_precommit_requirements() -> dict[str, SpecifierSet]: |
172 | 173 |
|
173 | 174 | def check_requirement_pins() -> None: |
174 | 175 | """Check that type checkers and linters are pinned to an exact version.""" |
175 | | - requirements = get_txt_requirements() |
| 176 | + requirements = parse_requirements() |
176 | 177 | for package in linters: |
177 | | - assert package in requirements, f"type checker/linter '{package}' not found in requirements-tests.txt" |
178 | | - spec = requirements[package] |
179 | | - assert len(spec) == 1, f"type checker/linter '{package}' has complex specifier in requirements-tests.txt" |
180 | | - msg = f"type checker/linter '{package}' is not pinned to an exact version in requirements-tests.txt" |
| 178 | + assert package in requirements, f"type checker/linter '{package}' not found in {REQS_FILE}" |
| 179 | + spec = requirements[package].specifier |
| 180 | + assert len(spec) == 1, f"type checker/linter '{package}' has complex specifier in {REQS_FILE}" |
| 181 | + msg = f"type checker/linter '{package}' is not pinned to an exact version in {REQS_FILE}" |
181 | 182 | assert str(spec).startswith("=="), msg |
182 | 183 |
|
183 | 184 |
|
184 | 185 | def check_precommit_requirements() -> None: |
185 | | - """Check that the requirements in requirements-tests.txt and .pre-commit-config.yaml match.""" |
186 | | - requirements_txt_requirements = get_txt_requirements() |
| 186 | + """Check that the requirements in the requirements file and .pre-commit-config.yaml match.""" |
| 187 | + requirements_txt_requirements = parse_requirements() |
187 | 188 | precommit_requirements = get_precommit_requirements() |
188 | | - no_txt_entry_msg = "All pre-commit requirements must also be listed in `requirements-tests.txt` (missing {requirement!r})" |
| 189 | + no_txt_entry_msg = f"All pre-commit requirements must also be listed in `{REQS_FILE}` (missing {{requirement!r}})" |
189 | 190 | for requirement, specifier in precommit_requirements.items(): |
190 | | - # annoying: the Ruff and Black repos for pre-commit are different to the names in requirements-tests.txt |
| 191 | + # annoying: the Ruff and Black repos for pre-commit are different to the names in the requirements file |
191 | 192 | if requirement in {"ruff-pre-commit", "black-pre-commit-mirror"}: |
192 | 193 | requirement = requirement.split("-")[0] |
193 | 194 | assert requirement in requirements_txt_requirements, no_txt_entry_msg.format(requirement=requirement) |
194 | 195 | specifier_mismatch = ( |
195 | 196 | f'Specifier "{specifier}" for {requirement!r} in `.pre-commit-config.yaml` ' |
196 | | - f'does not match specifier "{requirements_txt_requirements[requirement]}" in `requirements-tests.txt`' |
| 197 | + f'does not match specifier "{requirements_txt_requirements[requirement].specifier}" in `{REQS_FILE}`' |
197 | 198 | ) |
198 | | - assert specifier == requirements_txt_requirements[requirement], specifier_mismatch |
| 199 | + assert specifier == requirements_txt_requirements[requirement].specifier, specifier_mismatch |
199 | 200 |
|
200 | 201 |
|
201 | 202 | if __name__ == "__main__": |
|
0 commit comments