Return a list of `path:line: message` errors for one file.
(path: str)
| 32 | |
| 33 | |
| 34 | def check_file(path: str) -> list[str]: |
| 35 | """Return a list of `path:line: message` errors for one file.""" |
| 36 | try: |
| 37 | with open(path, encoding="utf-8") as f: |
| 38 | source = f.read() |
| 39 | except (OSError, UnicodeDecodeError) as e: |
| 40 | return [f"{path}: could not read file ({e})"] |
| 41 | |
| 42 | try: |
| 43 | tree = ast.parse(source, filename=path) |
| 44 | except SyntaxError: |
| 45 | # Leave syntax errors to other tools (ruff, the compiler). |
| 46 | return [] |
| 47 | |
| 48 | errors: list[str] = [] |
| 49 | for node in ast.walk(tree): |
| 50 | if not isinstance(node, _DOCSTRINGABLE): |
| 51 | continue |
| 52 | docstring = ast.get_docstring(node, clean=False) |
| 53 | if docstring and _DOUBLE_BACKTICK.search(docstring): |
| 54 | # ast.Module has no lineno; its docstring starts the file. |
| 55 | lineno = getattr(node, "lineno", 1) |
| 56 | errors.append( |
| 57 | f"{path}:{lineno}: double backticks in docstring" |
| 58 | ) |
| 59 | return errors |
| 60 | |
| 61 | |
| 62 | def main(argv: list[str]) -> int: |