python: Inline expectation should have space after $

This was a regex-find-replace from `# \$(?! )` (using a negative lookahead) to `# $ `.
This commit is contained in:
Owen Mansel-Chan
2026-03-04 11:42:07 +00:00
parent 0eccd902c2
commit 5a97348e78
61 changed files with 784 additions and 785 deletions

View File

@@ -2,12 +2,12 @@
def bad1():
results = []
for x in range(10):
def inner(): # $capturedVar=x
def inner(): # $ capturedVar=x
return x
results.append(inner)
return results
a = [lambda: i for i in range(1, 4)] # $capturedVar=i
a = [lambda: i for i in range(1, 4)] # $ capturedVar=i
for f in a:
print(f())
@@ -46,30 +46,30 @@ def ok1():
result += inner()
return result
b = [lambda: i for i in range(1, 4) for j in range(1,5)] # $capturedVar=i
c = [lambda: j for i in range(1, 4) for j in range(1,5)] # $capturedVar=j
b = [lambda: i for i in range(1, 4) for j in range(1,5)] # $ capturedVar=i
c = [lambda: j for i in range(1, 4) for j in range(1,5)] # $ capturedVar=j
s = {lambda: i for i in range(1, 4)} # $capturedVar=i
s = {lambda: i for i in range(1, 4)} # $ capturedVar=i
for f in s:
print(f())
d = {i:lambda: i for i in range(1, 4)} # $capturedVar=i
d = {i:lambda: i for i in range(1, 4)} # $ capturedVar=i
for k, f in d.items():
print(k, f())
#Generator expressions are sometimes OK, if they evaluate the iteration
#Generator expressions are sometimes OK, if they evaluate the iteration
#When the captured variable is used.
#So technically this is a false positive, but it is extremely fragile
#code, so I (Mark) think it is fine to report it as a violation.
g = (lambda: i for i in range(1, 4)) # $capturedVar=i
g = (lambda: i for i in range(1, 4)) # $ capturedVar=i
for f in g:
print(f())
#But not if evaluated eagerly
l = list(lambda: i for i in range(1, 4)) # $capturedVar=i
l = list(lambda: i for i in range(1, 4)) # $ capturedVar=i
for f in l:
print(f())
# This result is MISSING since the lambda is not detected to escape the loop
def odasa4860(asset_ids):
return dict((asset_id, filter(lambda c : c.asset_id == asset_id, xxx)) for asset_id in asset_ids) # $MISSING: capturedVar=asset_id
return dict((asset_id, filter(lambda c : c.asset_id == asset_id, xxx)) for asset_id in asset_ids) # $ MISSING: capturedVar=asset_id