Skip to content

Commit ed60b27

Browse files
jacalataclaude
andcommitted
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>
1 parent 6b2bd09 commit ed60b27

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

samples/explore_workbook.py

Lines changed: 27 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

@@ -117,23 +122,26 @@ def main():
117122

118123
# Get custom views. `.get()` only returns the first page;
119124
# use TSC.Pager to iterate every custom view on the site.
120-
for c in TSC.Pager(server.custom_views):
125+
custom_views = list(TSC.Pager(server.custom_views))
126+
for c in custom_views:
121127
print(c)
122128

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

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)
133+
# update the name
134+
# note that this will fail if the name is already changed to this value
135+
changed = TSC.CustomViewItem(id=c.id, name="I was updated by tsc")
136+
verified_change = server.custom_views.update(changed)
137+
print(verified_change)
130138

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)
139+
# export as image. Filters etc could be added here as usual
140+
server.custom_views.populate_image(c)
141+
filename = c.id + "-image-export.png"
142+
with open(filename, "wb") as f:
143+
f.write(c.image)
144+
print("saved to " + filename)
137145

138146
if args.powerpoint:
139147
# Populate workbook preview image
@@ -143,9 +151,12 @@ def main():
143151
print(f"\nDownloaded powerpoint of workbook to {os.path.abspath(args.powerpoint)}")
144152

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

150161

151162
if __name__ == "__main__":

0 commit comments

Comments
 (0)