Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused-import for names used in class keyword arguments inside if expressions #1630

Open
marmistrz opened this issue Aug 18, 2017 · 11 comments
Labels
Bug 🪲 Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@marmistrz
Copy link

marmistrz commented Aug 18, 2017

Update 06/25/22: see comment for an example that still reproduces on 2.14.

Steps to reproduce

Lint the following code:

from abc import ABCMeta

class foo(meta=ABCMeta):
    pass

Current behavior

W:  1, 0: Unused ABCMeta imported from abc (unused-import)

Expected behavior

No unused-import error.

pylint --version output

pylint 1.7.2, 
astroid 1.5.3
Python 3.6.2 (default, Jul 20 2017, 03:52:27) 
[GCC 7.1.1 20170630]
@rogalski
Copy link
Contributor

It's kind of corner case, we successfully lint code below, which is correct declaration of using metaclass:

from abc import ABCMeta

class foo(metaclass=ABCMeta):
    pass

@PCManticore PCManticore changed the title False positive for importing a metaclass unused-import for objects used in class keyword arguments Aug 26, 2017
@Wesitos
Copy link

Wesitos commented Feb 16, 2018

Still present in version 1.8.2
I wouldn't consider this a corner case considering that python's 3.6 new __init_subclass__ method supports arbitrary keyword argument passing

@csabaszilveszter
Copy link

Present in 2.1.0 too..

pylint 2.1.0
astroid 2.0.2
Python 3.6.6 (default, Jun 28 2018, 04:42:43) 
[GCC 5.4.0 20160609]

The code:

from worklog_transfer.commons import Commons

class IpaUsersFile(metaclass=Commons.BaseSingleton):
    """Class for ipa user data."""
    ...

and then the results
Unused Commons imported from worklog_transfer.commons (unused-import)

@jonapich
Copy link

I'm also facing this, in an __init_subclass__ scenario. (pylint 2.2.2)

@PCManticore
Copy link
Contributor

Thanks for the examples, folks. @jonapich Can you share a minimal reproduction example?

@Kane610
Copy link

Kane610 commented Sep 1, 2019

This example will get

subclass.py:1:0: W0611: Unused TEXT imported from baseclass (unused-import)

file baseclass.py

TEXT = "string"
class baseclass:
    def __init_subclass__(cls, text, **kwargs):
        super().__init_subclass__(**kwargs)

file subclass.py

from baseclass import TEXT, baseclass
class subclass(baseclass, text=TEXT):
    pass

@douglas-raillard-arm
Copy link

Here is another variation on the same theme without using __init_subclass__, which also triggers:

test.py:2:0: W0611: Unused import os (unused-import)
import os

class Meta(type):
    def __new__(cls, name, bases, dct, *args, x=None, **kwargs):
        return super().__new__(cls, name, bases, dct, *args, **kwargs)

class C(metaclass=Meta, x=os):
    pass

@hippo91
Copy link
Contributor

hippo91 commented Jan 16, 2021

@douglas-raillard-arm thanks for this update.

@rsiemens
Copy link

rsiemens commented May 13, 2022

I'm also seeing this when using an imported module in an expression in the class definition arguments list.

$ python -m pylint --version
pylint 2.13.9
astroid 2.11.5
Python 3.9.7 (default, Oct 22 2021, 07:42:57)
[Clang 12.0.5 (clang-1205.0.22.11)]
import django

class SomeMetaClass(type):
    pass

class MyClass(metaclass=type if django.VERSION >= (3, 2) else SomeMetaClass):
    pass
$ python -m pylint bug.py
************* Module bug
...
bug.py:1:0: W0611: Unused import django (unused-import)

@jacobtylerwalls
Copy link
Member

Only the last example in #1630 (comment) seems to still reproduce on 2.14 dev, so I'm going to refocus the issue title.

@jacobtylerwalls jacobtylerwalls removed their assignment Jun 25, 2022
@jacobtylerwalls jacobtylerwalls removed this from the 2.15.0 milestone Jun 25, 2022
@jacobtylerwalls jacobtylerwalls changed the title unused-import for objects used in class keyword arguments unused-import for names used in class keyword arguments inside if expressions Jun 25, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jun 26, 2022
@clavedeluna clavedeluna added the Control flow Requires control flow understanding label Dec 22, 2022
@samskiter
Copy link

@jacobtylerwalls
Also seems to repro for an inner class definition :

from abc import ABCMeta
class Piece:
    class Connector(metaclass=ABCMeta):
        pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests