JukkaL/mypy — pull request #504 by spkersten (2014-11-30 16:58:06+00:00)
Title: Support for nonlocal declaration

This PR implements #494.

There is one note I should make: An error message is generated when `nonlocal x` follows a definition of `x`, like in this case:

```python
def outer():
x = 1
def inner():
x = 2
nonlocal x
inner()
print(x)
```

However, Python 3.4 reports just a warning. I made this choice, because it was the easiest way to report an error (as it should) when there is a `nonlocal` declaration of a variable with the same name as a function parameter:

```python
def outer():
x = 1
def inner(x):
nonlocal x
```

I don't think it is such a bad idea to report an error in the first case too. In the first code snippet, the output is `2`. Which is (at least to me) a bit counter intuitive as the `nonlocal x` apparently affects an earlier statement. Furthermore, generating an error message in this case does not exclude any uses of `nonlocal` as it can be moved to the front of the function with changing the semantics.

Nevertheless, if you feel this is overly restrictive, let me know and I will try to find another way of dealing with this.