Skip to content

Commit 5c509c9

Browse files
feat: bundle format_exception rather than all helpers (#571)
* feat: bundle format_exception rather than all helpers * fix: install pnpm for script * fix: use js * Revert "fix: use js" This reverts commit 7f7c6b6. * fix: exported types Exporting a const forces TS to decide what type the const is. This is important because that type is declared and the declaration does not appear in the output
1 parent c8cd0c9 commit 5c509c9

File tree

7 files changed

+76
-62
lines changed

7 files changed

+76
-62
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ jobs:
5252
# Checkout the Repo
5353
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
5454

55+
# Just to run the script
56+
- name: Install pnpm
57+
uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4
58+
with:
59+
version: 10
60+
5561
# Install Python
5662
- name: Setup Python
5763
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
5864
with:
5965
python-version: 3.x
6066

6167
- name: Run Tests
62-
run: python ./packages/helpers/python/py_helpers.test.py
68+
run: pnpm run test:python

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
],
1414
"exports": {
1515
".": {
16-
"types": "./dist/types/helpers/lib/index.d.ts",
17-
"require": "./dist/curriculum-helpers/index.cjs",
18-
"default": "./dist/curriculum-helpers/index.mjs"
16+
"types": "./dist/types/helpers/python/index.d.ts",
17+
"require": "./dist/curriculum-helpers/format-exception.cjs",
18+
"default": "./dist/curriculum-helpers/format-exception.mjs"
1919
},
20-
"./helpers.js": {
21-
"types": "./dist/types/helpers/lib/index.d.ts",
22-
"require": "./dist/curriculum-helpers/index.cjs",
23-
"default": "./dist/curriculum-helpers/index.mjs"
20+
"./format-exception.js": {
21+
"types": "./dist/types/helpers/python/index.d.ts",
22+
"require": "./dist/curriculum-helpers/format-exception.cjs",
23+
"default": "./dist/curriculum-helpers/format-exception.mjs"
2424
},
2525
"./test-runner.js": {
2626
"types": "./dist/types/main/src/index.d.ts",
@@ -95,6 +95,7 @@
9595
"test": "vitest",
9696
"test:unit": "vitest --config vitest.unit.config.mjs",
9797
"test:integration": "vitest --config vitest.integration.config.mjs",
98+
"test:python": "python ./packages/helpers/python/python.test.py",
9899
"prepare": "husky install",
99100
"prepublishOnly": "pnpm clean:build && pnpm build",
100101
"webpack": "webpack"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Exception formatting functions for Python error messages
2+
3+
4+
def drop_until(*, traces, filename):
5+
from itertools import dropwhile
6+
7+
return list(
8+
dropwhile(lambda line: not line.startswith(f' File "{filename}"'), traces)
9+
)
10+
11+
12+
def build_message(*, traces, exception_list):
13+
return "".join(["Traceback (most recent call last):\n"] + traces + exception_list)
14+
15+
16+
def _replace_startswith(string, old, new):
17+
if string.startswith(old):
18+
return new + string[len(old) :]
19+
return string
20+
21+
22+
def format_exception(*, exception, traceback, filename, new_filename=None):
23+
if new_filename is None:
24+
new_filename = filename
25+
from traceback import format_exception_only, format_tb
26+
27+
# The trace up to "filename" are the frames that are not part of the user's
28+
# code so we drop them.
29+
traces = drop_until(traces=format_tb(traceback), filename=filename)
30+
renamed_traces = [
31+
_replace_startswith(trace, f' File "{filename}"', f' File "{new_filename}"')
32+
for trace in traces
33+
]
34+
renamed_exception = [
35+
_replace_startswith(e, f' File "{filename}"', f' File "{new_filename}"')
36+
for e in format_exception_only(exception)
37+
]
38+
return build_message(traces=renamed_traces, exception_list=renamed_exception)

packages/helpers/python/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import formatExceptionCode from "./format_exception.py";
2+
3+
export const formatException = formatExceptionCode;

packages/helpers/python/py_helpers.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -547,45 +547,3 @@ def is_ordered(self, *args):
547547
if None in arg_dict.values():
548548
return False
549549
return all(arg_dict[n] < arg_dict[n + 1] for n in range(len(arg_dict) - 1))
550-
551-
552-
# Exception formatting functions. Currently bundled with the Node class, until
553-
# we improve the testing, building and CI so that they can easily handle
554-
# multiple files.
555-
556-
557-
def drop_until(*, traces, filename):
558-
from itertools import dropwhile
559-
560-
return list(
561-
dropwhile(lambda line: not line.startswith(f' File "{filename}"'), traces)
562-
)
563-
564-
565-
def build_message(*, traces, exception_list):
566-
return "".join(["Traceback (most recent call last):\n"] + traces + exception_list)
567-
568-
569-
def _replace_startswith(string, old, new):
570-
if string.startswith(old):
571-
return new + string[len(old) :]
572-
return string
573-
574-
575-
def format_exception(*, exception, traceback, filename, new_filename=None):
576-
if new_filename is None:
577-
new_filename = filename
578-
from traceback import format_exception_only, format_tb
579-
580-
# The trace up to "filename" are the frames that are not part of the user's
581-
# code so we drop them.
582-
traces = drop_until(traces=format_tb(traceback), filename=filename)
583-
renamed_traces = [
584-
_replace_startswith(trace, f' File "{filename}"', f' File "{new_filename}"')
585-
for trace in traces
586-
]
587-
renamed_exception = [
588-
_replace_startswith(e, f' File "{filename}"', f' File "{new_filename}"')
589-
for e in format_exception_only(exception)
590-
]
591-
return build_message(traces=renamed_traces, exception_list=renamed_exception)

packages/helpers/python/py_helpers.test.py renamed to packages/helpers/python/python.test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22
import ast
33
import sys
4-
from py_helpers import Node, drop_until, build_message, format_exception
4+
from py_helpers import Node
5+
from format_exception import drop_until, build_message, format_exception
56

67

78
class TestConstructor(unittest.TestCase):

webpack.config.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const path = require("path");
22
const webpack = require("webpack");
33

4-
const baseConfig = {
4+
const formatExceptionBaseConfig = {
55
entry: {
6-
index: path.resolve(__dirname, "packages/helpers/lib/index.ts"),
6+
"format-exception": path.resolve(
7+
__dirname,
8+
"packages/helpers/python/index.ts",
9+
),
710
},
811
module: {
912
rules: [
@@ -29,29 +32,29 @@ const baseConfig = {
2932
},
3033
};
3134

32-
const nodeConfig = (env = {}) => {
35+
const formatExceptionNodeConfig = (env = {}) => {
3336
const isDev = env.development;
3437
return {
35-
...baseConfig,
36-
name: "helpers-commonjs",
38+
...formatExceptionBaseConfig,
39+
name: "format-exception-commonjs",
3740
mode: isDev ? "development" : "production",
3841
devtool: !isDev ? "source-map" : "inline-source-map",
3942
target: "node",
4043
output: {
4144
library: {
4245
type: "commonjs",
4346
},
44-
filename: "index.cjs",
47+
filename: "[name].cjs",
4548
path: path.resolve(__dirname, "dist/curriculum-helpers"),
4649
},
4750
};
4851
};
4952

50-
const browserConfig = (env = {}) => {
53+
const formatExceptionBrowserConfig = (env = {}) => {
5154
const isDev = env.development;
5255
return {
53-
...baseConfig,
54-
name: "helpers-browser",
56+
...formatExceptionBaseConfig,
57+
name: "format-exception-browser",
5558
mode: isDev ? "development" : "production",
5659
devtool: !isDev ? "source-map" : "inline-source-map",
5760
target: "web",
@@ -62,7 +65,7 @@ const browserConfig = (env = {}) => {
6265
library: {
6366
type: "module",
6467
},
65-
filename: "index.mjs",
68+
filename: "[name].mjs",
6669
path: path.resolve(__dirname, "dist/curriculum-helpers"),
6770
},
6871
};
@@ -178,4 +181,8 @@ const testRunnerConfigs = entrypointSources.map(({ name, path, isWorker }) => {
178181
return testRunnerConfig(entry, name, isWorker);
179182
});
180183

181-
module.exports = [nodeConfig, browserConfig, ...testRunnerConfigs];
184+
module.exports = [
185+
formatExceptionNodeConfig,
186+
formatExceptionBrowserConfig,
187+
...testRunnerConfigs,
188+
];

0 commit comments

Comments
 (0)