comparison tests/test_pylint.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children a6bc019ac0b2
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 import pymupdf
2 import os
3 import re
4 import subprocess
5 import sys
6 import textwrap
7
8 def test_pylint():
9
10 if not hasattr(pymupdf, 'mupdf'):
11 print(f'test_pylint(): Not running with classic implementation.')
12 return
13
14 ignores = ''
15
16 ignores += textwrap.dedent(
17 '''
18 C0103: Constant name "g_exceptions_verbose" doesn't conform to UPPER_CASE naming style (invalid-name)
19 C0115: Missing class docstring (missing-class-docstring)
20 C0116: Missing function or method docstring (missing-function-docstring)
21 C0301: Line too long (142/100) (line-too-long)
22 C0302: Too many lines in module (23586/1000) (too-many-lines)
23 C0303: Trailing whitespace (trailing-whitespace)
24 C0325: Unnecessary parens after 'not' keyword (superfluous-parens)
25 C0415: Import outside toplevel (traceback) (import-outside-toplevel)
26 R0902: Too many instance attributes (9/7) (too-many-instance-attributes)
27 R0903: Too few public methods (1/2) (too-few-public-methods)
28 R0911: Too many return statements (9/6) (too-many-return-statements)
29 R0913: Too many arguments (6/5) (too-many-arguments)
30 R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
31 R1720: Unnecessary "elif" after "raise", remove the leading "el" from "elif" (no-else-raise)
32 R1724: Unnecessary "elif" after "continue", remove the leading "el" from "elif" (no-else-continue)
33 R1735: Consider using '{}' instead of a call to 'dict'. (use-dict-literal)
34 W0511: Fixme: we don't support JM_MEMORY=1. (fixme)
35 W0622: Redefining built-in 'FileNotFoundError' (redefined-builtin)
36 W0622: Redefining built-in 'open' (redefined-builtin)
37 W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
38 R1734: Consider using [] instead of list() (use-list-literal)
39 R1727: Boolean condition '0 and g_exceptions_verbose' will always evaluate to '0' (condition-evals-to-constant)
40 R1726: (simplifiable-condition)
41 '''
42 )
43
44 # Items that we might want to fix.
45 ignores += textwrap.dedent(
46 '''
47 C0114: Missing module docstring (missing-module-docstring)
48 C0117: Consider changing "not rotate % 90 == 0" to "rotate % 90 != 0" (unnecessary-negation)
49 C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck)
50 C0200: Consider using enumerate instead of iterating with range and len (consider-using-enumerate)
51 C0201: Consider iterating the dictionary directly instead of calling .keys() (consider-iterating-dictionary)
52 C0209: Formatting a regular string which could be an f-string (consider-using-f-string)
53 C0305: Trailing newlines (trailing-newlines)
54 C0321: More than one statement on a single line (multiple-statements)
55 C1802: Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty (use-implicit-booleaness-not-len)
56 C1803: "select == []" can be simplified to "not select", if it is strictly a sequence, as an empty list is falsey (use-implicit-booleaness-not-comparison)
57 R0912: Too many branches (18/12) (too-many-branches)
58 R0914: Too many local variables (20/15) (too-many-locals)
59 R0915: Too many statements (58/50) (too-many-statements)
60 R1702: Too many nested blocks (7/5) (too-many-nested-blocks)
61 R1703: The if statement can be replaced with 'var = bool(test)' (simplifiable-if-statement)
62 R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
63 R1714: Consider merging these comparisons with 'in' by using 'width not in (1, 0)'. Use a set instead if elements are hashable. (consider-using-in)
64 R1716: Simplify chained comparison between the operands (chained-comparison)
65 R1717: Consider using a dictionary comprehension (consider-using-dict-comprehension)
66 R1718: Consider using a set comprehension (consider-using-set-comprehension)
67 R1719: The if expression can be replaced with 'bool(test)' (simplifiable-if-expression)
68 R1721: Unnecessary use of a comprehension, use list(roman_num(num)) instead. (unnecessary-comprehension)
69 R1728: Consider using a generator instead 'max(len(k) for k in item.keys())' (consider-using-generator)
70 R1728: Consider using a generator instead 'max(len(r.cells) for r in self.rows)' (consider-using-generator)
71 R1730: Consider using 'rowheight = min(rowheight, height)' instead of unnecessary if block (consider-using-min-builtin)
72 R1731: Consider using 'right = max(right, x1)' instead of unnecessary if block (consider-using-max-builtin)
73 W0105: String statement has no effect (pointless-string-statement)
74 W0107: Unnecessary pass statement (unnecessary-pass)
75 W0212: Access to a protected member _graft_id of a client class (protected-access)
76 W0602: Using global for 'CHARS' but no assignment is done (global-variable-not-assigned)
77 W0602: Using global for 'EDGES' but no assignment is done (global-variable-not-assigned)
78 W0603: Using the global statement (global-statement)
79 W0612: Unused variable 'keyvals' (unused-variable)
80 W0613: Unused argument 'kwargs' (unused-argument)
81 W0621: Redefining name 'show' from outer scope (line 159) (redefined-outer-name)
82 W0640: Cell variable o defined in loop (cell-var-from-loop)
83 W0718: Catching too general exception Exception (broad-exception-caught)
84 W0719: Raising too general exception: Exception (broad-exception-raised)
85 C3001: Lambda expression assigned to a variable. Define a function using the "def" keyword instead. (unnecessary-lambda-assignment)
86 R0801: Similar lines in 2 files
87 R0917: Too many positional arguments (7/5) (too-many-positional-arguments)
88 '''
89 )
90 ignores_list = list()
91 for line in ignores.split('\n'):
92 if not line or line.startswith('#'):
93 continue
94 m = re.match('^(.....): ', line)
95 assert m, f'Failed to parse {line=}'
96 ignores_list.append(m.group(1))
97 ignores = ','.join(ignores_list)
98
99 root = os.path.abspath(f'{__file__}/../..')
100
101 sys.path.insert(0, root)
102 import pipcl
103 del sys.path[0]
104
105 # We want to run pylist on all of our src/*.py files so we find them with
106 # `pipcl.git_items()`. However this seems to fail on github windows with
107 # `fatal: not a git repository (or any of the parent directories): .git` so
108 # we also hard-code the list and verify it matches `git ls-files` on other
109 # platforms. This ensures that we will always pick up new .py files in the
110 # future.
111 #
112 command = f'pylint -d {ignores}'
113 directory = f'{root}/src'
114 directory = directory.replace('/', os.sep)
115 leafs = [
116 '__init__.py',
117 '__main__.py',
118 '_apply_pages.py',
119 '_wxcolors.py',
120 'fitz___init__.py',
121 'fitz_table.py',
122 'fitz_utils.py',
123 'pymupdf.py',
124 'table.py',
125 'utils.py',
126 ]
127 leafs.sort()
128 try:
129 leafs_git = pipcl.git_items(directory)
130 except Exception as e:
131 import platform
132 assert platform.system() == 'Windows'
133 else:
134 leafs_git = [i for i in leafs_git if i.endswith('.py')]
135 leafs_git.sort()
136 assert leafs_git == leafs, f'leafs:\n {leafs!r}\nleafs_git:\n {leafs_git!r}'
137 for leaf in leafs:
138 command += f' {directory}/{leaf}'
139 print(f'Running: {command}')
140 subprocess.run(command, shell=1, check=1)
141