python-distilled
python-distilled copied to clipboard
2.15 indexing into a potentially empty string
In the comments generator of section 2.15, the way t[0] is used does not seem safe:
comments = (t for t in lines if t[0] == '#') # All comments
t might be empty at that point, so you might consider changing that line to either
comments = (t for t in lines if t and t[0] == '#')
or
comments = (t for t in lines if t.startswith('#'))
One other option is to modify lines so that it never yields empty strings:
- lines = (t.strip() for t in f)
+ lines = (s for t in f if (s := t.strip()))