Pyright: Break within for or while loop causes incorrect 'unreachable code' to be reported

Created on 15 Aug 2019  路  2Comments  路  Source: microsoft/pyright

I'm having a slightly different, but closely related, issue with break inside for or while, and I'm not sure if it warrants its own issue so I'm posting in this thread first.

In this code, everything after the first line inside the for or while is seen as unreachable.

for _ in []: # also applies to "while True:"
  print('Everything after this line is unreachable')
  print('Unreachable 1')
  print('Unreachable 2')
  print('Unreachable 3')
  break # also unreachable

This can be annoying for code that looks like this

for element in some_list:
  if some_condition:
    continue

  if other_condition:
    continue

  # more condition checking...

  # found first element that passes all checks, do processing here
  break
addressed in next version bug

All 2 comments

@RaptDept, thanks again for this bug report. I've identified the cause, and it will be fixed in the next version of pyright.

This is now fixed in 1.0.56, which I just published.

Was this page helpful?
0 / 5 - 0 ratings