| 1 |
Weird legacy stuff in test cases |
| 2 |
================================ |
| 3 |
|
| 4 |
Due to historical reasons, test cases contain things that may appear |
| 5 |
baffling without extra context. This file attempts to describe most of |
| 6 |
them. |
| 7 |
|
| 8 |
Dummy if statements to prevent redefinition |
| 9 |
------------------------------------------- |
| 10 |
|
| 11 |
Many test cases use if statements to prevent an assignment from creating |
| 12 |
a new variable. This in anticipation of allowing assignments to redefine |
| 13 |
variables by default. Conditional assignments will continue to refine |
| 14 |
a previously defined variable instead of defining a new one. When the |
| 15 |
test cases were written, we didn't anticipate that variables could be |
| 16 |
allowed to be redefined, and adding if statements was the easiest way |
| 17 |
to migrate these tests. |
| 18 |
|
| 19 |
Example: |
| 20 |
|
| 21 |
``` |
| 22 |
x = 0 |
| 23 |
if int(): |
| 24 |
x = '' # Always generates an error since this is not a redefinition |
| 25 |
|
| 26 |
y = 0 |
| 27 |
y = '' # This could be valid if a new 'y' is defined here |
| 28 |
``` |
| 29 |
|
| 30 |
Note that some of the checks may turn out to be redundant, as the |
| 31 |
exact rules for what constitutes a redefinition are still up for |
| 32 |
debate. This is okay since the extra if statements generally don't |
| 33 |
otherwise affect semantics. |
| 34 |
|
| 35 |
There are a few ways this is used, depending on the context: |
| 36 |
|
| 37 |
* `if int():` is the most common one. Assignments in the if body won't |
| 38 |
redefine variables defined before the if statement. |
| 39 |
* `if 1:` is used if the body of the if statement returns a value, and |
| 40 |
mypy would complain about a missing return statement otherwise. This |
| 41 |
works since `if 1:` is treated as an always taken condition, whereas |
| 42 |
`if int():` is not recognized as such. |
| 43 |
* `if str():` is used if the builtins fixture doesn't define `int` for |
| 44 |
some reason. |
| 45 |
|
| 46 |
Function definition to prevent redefinition |
| 47 |
------------------------------------------- |
| 48 |
|
| 49 |
Sometimes test cases assume that a variable is not redefined, and we |
| 50 |
insert a dummy function definition to prevent this, since variables won't |
| 51 |
be able to be redefined across a function definition. Example: |
| 52 |
|
| 53 |
``` |
| 54 |
x = 0 |
| 55 |
|
| 56 |
def f(): pass |
| 57 |
|
| 58 |
x = '' # Does not redefine x because of the definition of f() above |
| 59 |
``` |
| 60 |
|
| 61 |
Dummy variable reference to allow redefinition |
| 62 |
---------------------------------------------- |
| 63 |
|
| 64 |
The plan is to only allow a variable to be redefined if the value has |
| 65 |
been accessed. This wouldn't count as redefinition, since `x` is never |
| 66 |
read: |
| 67 |
|
| 68 |
``` |
| 69 |
x = 0 |
| 70 |
x = '' # Not a redefinition |
| 71 |
``` |
| 72 |
|
| 73 |
Sometimes we add a dummy variable access to allow redefinition in the |
| 74 |
future, or to trigger the redefinition machinery even if redefinition |
| 75 |
should not be okay: |
| 76 |
|
| 77 |
``` |
| 78 |
x = 0 |
| 79 |
x |
| 80 |
x = '' # Could be a redefinition |
| 81 |
``` |
| 82 |
|
| 83 |
The reason for this special case is type comments with dummy |
| 84 |
initializers, where the second assignment should never be treated |
| 85 |
as a redefinition: |
| 86 |
|
| 87 |
``` |
| 88 |
x = None # type: int |
| 89 |
x = '' # Should not redefine x, since it has only been declared |
| 90 |
``` |
| 91 |
|
| 92 |
Similarly, if there is only a variable annotation, the first |
| 93 |
assignment won't redefine the variable, as this would override |
| 94 |
the declared type: |
| 95 |
|
| 96 |
``` |
| 97 |
x: int |
| 98 |
x = '' # Should not redefine x |
| 99 |
``` |