| [case testDefinedInOneBranch] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| a = 1 |
| else: |
| x = 2 |
| z = a + 1 # E: Name "a" may be undefined |
| [case testElif] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| a = 1 |
| elif int(): |
| a = 2 |
| else: |
| x = 3 |
| |
| z = a + 1 # E: Name "a" may be undefined |
| |
| [case testDefinedInAllBranches] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| a = 1 |
| elif int(): |
| a = 2 |
| else: |
| a = 3 |
| z = a + 1 |
| |
| [case testOmittedElse] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| a = 1 |
| z = a + 1 # E: Name "a" may be undefined |
| |
| [case testUpdatedInIf] |
| # flags: --enable-error-code partially-defined |
| # Variable a is already defined. Just updating it in an "if" is acceptable. |
| a = 1 |
| if int(): |
| a = 2 |
| z = a + 1 |
| |
| [case testNestedIf] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| if int(): |
| a = 1 |
| x = 1 |
| x = x + 1 |
| else: |
| a = 2 |
| b = a + x # E: Name "x" may be undefined |
| b = b + 1 |
| else: |
| b = 2 |
| z = a + b # E: Name "a" may be undefined |
| |
| [case testVeryNestedIf] |
| # flags: --enable-error-code partially-defined |
| if int(): |
| if int(): |
| if int(): |
| a = 1 |
| else: |
| a = 2 |
| x = a |
| else: |
| a = 2 |
| b = a |
| else: |
| b = 2 |
| z = a + b # E: Name "a" may be undefined |
| |
| [case testTupleUnpack] |
| # flags: --enable-error-code partially-defined |
| |
| if int(): |
| (x, y) = (1, 2) |
| else: |
| [y, z] = [1, 2] |
| a = y + x # E: Name "x" may be undefined |
| a = y + z # E: Name "z" may be undefined |
| |
| [case testRedefined] |
| # flags: --enable-error-code partially-defined |
| y = 3 |
| if int(): |
| if int(): |
| y = 2 |
| x = y + 2 |
| else: |
| if int(): |
| y = 2 |
| x = y + 2 |
| |
| x = y + 2 |
| |
| [case testScope] |
| # flags: --enable-error-code partially-defined |
| def foo() -> None: |
| if int(): |
| y = 2 |
| |
| if int(): |
| y = 3 |
| x = y # E: Name "y" may be undefined |
| |
| [case testFuncParams] |
| # flags: --enable-error-code partially-defined |
| def foo(a: int) -> None: |
| if int(): |
| a = 2 |
| x = a |
| |
| [case testWhile] |
| # flags: --enable-error-code partially-defined |
| while int(): |
| x = 1 |
| |
| y = x # E: Name "x" may be undefined |
| |
| while int(): |
| z = 1 |
| else: |
| z = 2 |
| |
| y = z # No error. |
| |
| [case testForLoop] |
| # flags: --enable-error-code partially-defined |
| for x in [1, 2, 3]: |
| if x: |
| x = 1 |
| y = x |
| z = 1 |
| else: |
| z = 2 |
| |
| a = z + y # E: Name "y" may be undefined |