@opensip-tools/checks-python 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+
2
+ > @opensip-tools/checks-python@1.0.4 typecheck /Users/sb/Documents/Code/opensip-ai/opensip-tools/packages/fitness/checks-python
3
+ > tsc --noEmit
4
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 opensip-ai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@opensip-tools/checks-python",
3
+ "version": "1.0.4",
4
+ "license": "MIT",
5
+ "description": "Python fitness checks for opensip-tools",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/opensip-ai/opensip-tools.git",
9
+ "directory": "packages/fitness/checks-python"
10
+ },
11
+ "homepage": "https://github.com/opensip-ai/opensip-tools",
12
+ "bugs": {
13
+ "url": "https://github.com/opensip-ai/opensip-tools/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": "./dist/index.js"
20
+ },
21
+ "dependencies": {
22
+ "@opensip-tools/fitness": "1.0.4"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^22.0.0",
26
+ "vitest": "^2.1.0"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "test": "vitest run --passWithNoTests",
31
+ "typecheck": "tsc --noEmit",
32
+ "clean": "rm -rf dist"
33
+ }
34
+ }
@@ -0,0 +1,42 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { analyzeBareExcept } from '../checks/no-bare-except.js';
4
+
5
+ describe('analyzeBareExcept', () => {
6
+ it('flags a bare except:', () => {
7
+ const out = analyzeBareExcept('try:\n foo()\nexcept:\n pass\n');
8
+ expect(out).toHaveLength(1);
9
+ expect(out[0]?.severity).toBe('warning');
10
+ expect(out[0]?.line).toBe(3);
11
+ });
12
+
13
+ it('flags a bare except : with whitespace before the colon', () => {
14
+ const out = analyzeBareExcept('try:\n foo()\nexcept :\n pass\n');
15
+ expect(out).toHaveLength(1);
16
+ });
17
+
18
+ it('does not flag a typed except', () => {
19
+ expect(analyzeBareExcept('try:\n foo()\nexcept Exception:\n pass\n')).toHaveLength(0);
20
+ });
21
+
22
+ it('does not flag except with parentheses (multi-type)', () => {
23
+ expect(analyzeBareExcept('try:\n foo()\nexcept (KeyError, ValueError):\n pass\n')).toHaveLength(0);
24
+ });
25
+
26
+ it('flags multiple bare excepts in the same file', () => {
27
+ const src = 'try:\n a()\nexcept:\n pass\ntry:\n b()\nexcept:\n pass\n';
28
+ const out = analyzeBareExcept(src);
29
+ expect(out).toHaveLength(2);
30
+ });
31
+
32
+ it('returns an empty list for code without except clauses', () => {
33
+ expect(analyzeBareExcept('def foo(): return 1\n')).toEqual([]);
34
+ });
35
+
36
+ it('respects leading indentation (nested try)', () => {
37
+ const src = 'def f():\n try:\n a()\n except:\n pass\n';
38
+ const out = analyzeBareExcept(src);
39
+ expect(out).toHaveLength(1);
40
+ expect(out[0]?.line).toBe(4);
41
+ });
42
+ });
@@ -0,0 +1,78 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { analyzeBareExcept } from '../checks/no-bare-except.js'
4
+
5
+ describe('analyzeBareExcept', () => {
6
+ it('flags `except:` on its own line', () => {
7
+ const src = `try:
8
+ risky()
9
+ except:
10
+ pass`
11
+ const violations = analyzeBareExcept(src)
12
+ expect(violations.length).toBe(1)
13
+ expect(violations[0]?.message).toContain('Bare')
14
+ expect(violations[0]?.line).toBe(3)
15
+ })
16
+
17
+ it('does not flag `except Exception:`', () => {
18
+ const src = `try:
19
+ risky()
20
+ except Exception:
21
+ pass`
22
+ const violations = analyzeBareExcept(src)
23
+ expect(violations.length).toBe(0)
24
+ })
25
+
26
+ it('does not flag `except (TypeError, ValueError):`', () => {
27
+ const src = `try:
28
+ risky()
29
+ except (TypeError, ValueError):
30
+ pass`
31
+ const violations = analyzeBareExcept(src)
32
+ expect(violations.length).toBe(0)
33
+ })
34
+
35
+ it('does not flag `except Exception as e:`', () => {
36
+ const src = `try:
37
+ risky()
38
+ except Exception as e:
39
+ log(e)`
40
+ const violations = analyzeBareExcept(src)
41
+ expect(violations.length).toBe(0)
42
+ })
43
+
44
+ it('reports correct line number for indented bare except', () => {
45
+ const src = `def fn():
46
+ try:
47
+ risky()
48
+ except:
49
+ pass`
50
+ const violations = analyzeBareExcept(src)
51
+ expect(violations.length).toBe(1)
52
+ expect(violations[0]?.line).toBe(4)
53
+ })
54
+
55
+ it('flags multiple bare excepts independently', () => {
56
+ const src = `try:
57
+ a()
58
+ except:
59
+ pass
60
+ try:
61
+ b()
62
+ except:
63
+ pass`
64
+ const violations = analyzeBareExcept(src)
65
+ expect(violations.length).toBe(2)
66
+ expect(violations[0]?.line).toBe(3)
67
+ expect(violations[1]?.line).toBe(7)
68
+ })
69
+
70
+ it('tolerates whitespace between `except` and `:`', () => {
71
+ const src = `try:
72
+ a()
73
+ except :
74
+ pass`
75
+ const violations = analyzeBareExcept(src)
76
+ expect(violations.length).toBe(1)
77
+ })
78
+ })
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @fileoverview Flag bare `except:` clauses in Python.
3
+ *
4
+ * A bare `except:` catches every exception, including ones that are
5
+ * usually meant to propagate — KeyboardInterrupt, SystemExit, and any
6
+ * subclass of BaseException. This makes programs harder to terminate
7
+ * and hides bugs. The Python style guide and most lint tools (ruff
8
+ * E722, pylint W0702) flag this. Always specify what you're catching,
9
+ * even if it's just `except Exception:`.
10
+ *
11
+ * Detection is line-pattern based: a line whose first non-whitespace
12
+ * characters are `except:` (with optional whitespace before the colon).
13
+ * The check uses `strip-strings` content filtering so a literal like
14
+ * `"except:"` inside a docstring or string doesn't false-fire.
15
+ */
16
+ import { defineCheck, type CheckViolation } from '@opensip-tools/fitness'
17
+
18
+ // eslint-disable-next-line sonarjs/slow-regex -- anchored, bounded line scan; \s* on bounded leading whitespace is safe
19
+ const BARE_EXCEPT_PATTERN = /^\s*except\s*:/gm
20
+
21
+ /**
22
+ * Pure analysis function. Exported so unit tests can exercise the
23
+ * detection logic without standing up the full Check framework.
24
+ */
25
+ export function analyzeBareExcept(content: string): CheckViolation[] {
26
+ const violations: CheckViolation[] = []
27
+ BARE_EXCEPT_PATTERN.lastIndex = 0
28
+ let match: RegExpExecArray | null
29
+ while ((match = BARE_EXCEPT_PATTERN.exec(content)) !== null) {
30
+ // Compute 1-based line number from match index.
31
+ const upto = content.slice(0, match.index)
32
+ const line = upto.split('\n').length
33
+ violations.push({
34
+ message: 'Bare `except:` catches BaseException — including KeyboardInterrupt and SystemExit',
35
+ severity: 'warning',
36
+ line,
37
+ suggestion: 'Catch a specific exception (e.g. `except Exception:` or a narrower type)',
38
+ })
39
+ }
40
+ return violations
41
+ }
42
+
43
+ export const noBareExcept = defineCheck({
44
+ id: 'b1c2d3e4-9876-4321-bbbb-200000000001',
45
+ slug: 'python-no-bare-except',
46
+ description: 'Bare except clauses catch system-exiting exceptions like KeyboardInterrupt',
47
+ scope: { languages: ['python'], concerns: [] },
48
+ tags: ['quality', 'python'],
49
+ // Use 'strip-strings' so a literal `"except:"` inside a string is
50
+ // not matched. Comments are still visible — but `# except:` won't
51
+ // match the leading-whitespace anchor since `#` is in the way.
52
+ contentFilter: 'strip-strings',
53
+ analyze: (content) => analyzeBareExcept(content),
54
+ })
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { noBareExcept } from './checks/no-bare-except.js'
2
+
3
+ export const checks = [noBareExcept] as const
4
+
5
+
6
+ export const metadata = {
7
+ name: '@opensip-tools/checks-python',
8
+ version: '0.6.1',
9
+ description: 'Python fitness checks',
10
+ }
11
+
12
+ export {noBareExcept} from './checks/no-bare-except.js'
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src"]
8
+ }
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ export default defineConfig({
3
+ test: {
4
+ include: ['src/**/*.test.ts'],
5
+ coverage: {
6
+ include: ['src/**'],
7
+ exclude: ['src/**/*.test.ts', 'src/**/__tests__/**', 'src/index.ts'],
8
+ },
9
+ },
10
+ });