Pylint: Variable used with nonlocal marked as unused if it has a type annotation

Created on 31 Dec 2018  路  2Comments  路  Source: PyCQA/pylint

Steps to reproduce

  1. Create a file containing:
def f():
    x: int = 0
    y = 0

    def inner():
        nonlocal x, y
        x += 1
        y += 1
        return x + y

    print(inner(), inner())
  1. Run pylint a.py

Current behavior

There is a lint failure: a.py:2:4: W0612: Unused variable 'x' (unused-variable)
There is no such lint failure for y.

Expected behavior

No unused-variable lint failures, since both variables are used as nonlocal inside inner.

pylint --version output

pylint 2.2.2
astroid 2.1.0
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)]
bug

All 2 comments

Thanks for reporting a bug!

I'm still seeing this also on outer function arguments with latest version (2.3.1) and also without type annotations. Am I missing something??

a.py:

# pylint: disable=invalid-name, missing-docstring

def f(x, y):
    def inner():
        nonlocal x, y
        x += 1
        y += 1
        return x + y

    print(inner(), inner())

pylint a.py:

************* Module a
D:\Temp\a.py:3:6: W0613: Unused argument 'x' (unused-argument)
D:\Temp\a.py:3:9: W0613: Unused argument 'y' (unused-argument)

--------------------------------------------------------------------
Your code has been rated at 7.14/10 (previous run: -2.86/10, +10.00)

pylint --version:

pylint 2.3.1
astroid 2.2.5
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)]
Was this page helpful?
0 / 5 - 0 ratings