Please consider the following example, which finds the first String in a list that contains the Substring "OH":
list = ["STEVE", "JOHN", "YOANN"]
pattern = re.compile(".*%s.*" % "OH")
word = ""
if any((match := pattern.match(item)) for item in list):
word = match.group(0)
print(word)
The code works as intended and outputs "JOHN", but I am getting the following warning from flake8 at the line word = match.group(0)
:
F821 -- undefined name 'match'
Why is this happening, and can I remove the warning without command line arguments or disabling all F821 errors?
This is a bug in pyflakes -- I'd suggest reporting it there
The subtlety is that assignment expressions break out of comprehension scopes, but pyflakes assumes that comprehension scopes enclose all assignments
I'd suggest reporting an issue here
as a workaround, you can place a # noqa
comment on the line which produces the error, for example:
# this one ignores *all* errors on the line
word = match.group(0) # noqa
# this one ignores specifically the F821 error
word = match.group(0) # noqa: F821
disclaimer: I'm the maintainer of flake8 and one of the maintainers of pyflakes