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

self incorrectly marked as unsubscriptable in list subclass #3930

Closed
pauladams8 opened this issue Oct 29, 2020 · 5 comments
Closed

self incorrectly marked as unsubscriptable in list subclass #3930

pauladams8 opened this issue Oct 29, 2020 · 5 comments
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code typing

Comments

@pauladams8
Copy link

Steps to reproduce

  1. Subclass the builtin list class
  2. Slice self from within the class

Example

class CustomList(list):
    def first(self):
        # for example
        return self[0]

Current behavior

pylint produces error

Value 'self' is unsubscriptable

Expected behavior

No alert from pylint

pylint --version output

pylint 2.6.0
astroid 2.4.2
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)]

@hippo91
Copy link
Contributor

hippo91 commented Oct 29, 2020

@pauladams8 thanks for your report.
I'm unable to reproduce it.
Linting the following code:

#pylint:disable=missing-module-docstring
#pylint:disable=missing-class-docstring
#pylint:disable=missing-function-docstring
class CustomList(list):
    def first(self):
        # for example
        return self[0]

gives no message.

pylint 2.6.0
astroid 2.4.3
Python 3.8.2 (default, Sep 28 2020, 21:24:28) 
[GCC 8.3.0]

@pauladams8
Copy link
Author

pauladams8 commented Oct 29, 2020

Sorry, should have been clearer. Upon further testing, the problem occurs only when I subclass the List alias in typing and with a type hint.

Try this:

from typing import List

class TypeHint:
    pass

class CustomList(List[TypeHint]):
    def first(self):
        # pylint alerts me "Value 'self' is unsubscriptable", which is incorrect
        return self[0]

Additionally, pylint incorrectly reports errors when List is subclassed without a type hint.

Inheriting 'List', which is not a class.

When getting and setting custom properties:

Instance of '_GenericAlias' has no '{any member}' member

It seems these errors are not present with a type hint.

To reproduce this, use the sample above but without the [TypeHint].

@pauladams8
Copy link
Author

pauladams8 commented Oct 29, 2020

pylint also reports incorrectly on List[TypeHint] subclasses

Value 'self' doesn't support membership test

Test sample as above, with additional method

def has_foo(self):
    return 'foo' in self

@hippo91
Copy link
Contributor

hippo91 commented Oct 30, 2020

@pauladams8 thanks for this update. I can now reproduce it.
While it is not linked to python3.9 maybe the current work on supporting typing module under python3.9 (#3890 #3905) may improve the situation.

@hippo91 hippo91 added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code typing and removed Cannot reproduce 🤷 labels Oct 30, 2020
@cdce8p
Copy link
Member

cdce8p commented Oct 17, 2021

@pauladams8 I wasn't able to reproduce any of the errors with the latest version of pylint. We improved the handling of the typing module quite a bit in recent versions so I would assume this has been fixed. Going to close this issue.

If you still experience issues, please report back and I'll reopen it.

@cdce8p cdce8p closed this as completed Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code typing
Projects
None yet
Development

No branches or pull requests

3 participants