Skip to content

Commit 7bb9852

Browse files
jacalataclaudeCopilotbcantoni
authored
fix(samples): three latent bugs in explore_workbook.py (#1881)
* Fix three latent bugs in samples/explore_workbook.py Fresh-eyes review on #1843 (2026-09-17) surfaced three pre-existing bugs that the samples/_shared.py migration in that PR did not introduce but did make more visible: 1. Line 127 used a bare type annotation `changed: TSC.CustomViewItem( id=c.id, name=...)` instead of an assignment, so `changed` was never bound and the following `server.custom_views.update(changed)` would NameError. 2. `c` was defined inside the custom-views loop and then referenced outside it. On a site with zero custom views the loop never runs and every subsequent reference NameErrors. 3. `if args.delete:` at line 145 referred to a flag that was never defined in the sample's argparse, so any invocation reaching that line raised AttributeError. Fixes: - Add `=` on line 127 to bind `changed`. - Collect the custom-views iterator into a list, wrap the update/ export block in `if custom_views:`, and pick the last entry explicitly (`c = custom_views[-1]`) — matches the intent of the original "for the last custom view in the list" comment. - Add a `--delete` argparse flag with `action="store_true"` and a clear help string. Guard the delete block so it prints a clear no-op message when the site has no custom views. No other behavior change. Sample compiles and imports cleanly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Initialize custom views before workbook guard Co-authored-by: bcantoni <58372+bcantoni@users.noreply.github.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bcantoni <58372+bcantoni@users.noreply.github.com>
1 parent c46e757 commit 7bb9852

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

‎samples/explore_workbook.py‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def main():
3030
parser.add_argument(
3131
"--powerpoint", "-ppt", metavar="FILENAME", help="filename (a .ppt file) to save the powerpoint deck"
3232
)
33+
parser.add_argument(
34+
"--delete",
35+
action="store_true",
36+
help="delete the last custom view on the site after the update/export demo",
37+
)
3338

3439
args = parser.parse_args()
3540

@@ -60,6 +65,7 @@ def main():
6065
all_workbooks = list(TSC.Pager(server.workbooks))
6166
print([workbook.name for workbook in all_workbooks])
6267

68+
custom_views = []
6369
if all_workbooks:
6470
# Pick one workbook from the list
6571
sample_workbook = all_workbooks[0]
@@ -117,23 +123,26 @@ def main():
117123

118124
# Get custom views. `.get()` only returns the first page;
119125
# use TSC.Pager to iterate every custom view on the site.
120-
for c in TSC.Pager(server.custom_views):
126+
custom_views = list(TSC.Pager(server.custom_views))
127+
for c in custom_views:
121128
print(c)
122129

123-
# for the last custom view in the list
130+
if custom_views:
131+
# For the last custom view in the list.
132+
c = custom_views[-1]
124133

125-
# update the name
126-
# note that this will fail if the name is already changed to this value
127-
changed: TSC.CustomViewItem(id=c.id, name="I was updated by tsc")
128-
verified_change = server.custom_views.update(changed)
129-
print(verified_change)
134+
# update the name
135+
# note that this will fail if the name is already changed to this value
136+
changed = TSC.CustomViewItem(id=c.id, name="I was updated by tsc")
137+
verified_change = server.custom_views.update(changed)
138+
print(verified_change)
130139

131-
# export as image. Filters etc could be added here as usual
132-
server.custom_views.populate_image(c)
133-
filename = c.id + "-image-export.png"
134-
with open(filename, "wb") as f:
135-
f.write(c.image)
136-
print("saved to " + filename)
140+
# export as image. Filters etc could be added here as usual
141+
server.custom_views.populate_image(c)
142+
filename = c.id + "-image-export.png"
143+
with open(filename, "wb") as f:
144+
f.write(c.image)
145+
print("saved to " + filename)
137146

138147
if args.powerpoint:
139148
# Populate workbook preview image
@@ -143,9 +152,12 @@ def main():
143152
print(f"\nDownloaded powerpoint of workbook to {os.path.abspath(args.powerpoint)}")
144153

145154
if args.delete:
146-
print(f"deleting {c.id}")
147-
unlucky = TSC.CustomViewItem(c.id)
148-
server.custom_views.delete(unlucky.id)
155+
if not custom_views:
156+
print("--delete requested but no custom views on this site; nothing to delete.")
157+
else:
158+
print(f"deleting {c.id}")
159+
unlucky = TSC.CustomViewItem(c.id)
160+
server.custom_views.delete(unlucky.id)
149161

150162

151163
if __name__ == "__main__":

0 commit comments

Comments
 (0)