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

typing.cast is ignored in case of no-member error checker #6784

Closed
PiotrBatko-BlockchainWares opened this issue Jun 1, 2022 · 2 comments
Closed
Labels
Duplicate 🐫 Duplicate of an already existing issue High effort 🏋 Difficult solution or problem to solve Proposal 📨

Comments

@PiotrBatko-BlockchainWares

Bug description

In following code Pylint reports no-member error. It's right, because list_wrapper hasn't such member. I used typing.cast and type annotations to force Pylint to think, that list_wrapper has different type, but Pylint ignores it.

# pylint: disable=missing-module-docstring, missing-class-docstring, too-few-public-methods, missing-function-docstring

import typing

class ListWrapper:
    pass

def some_func():
    list_wrapper: typing.List = typing.cast(typing.List, ListWrapper())
    list_wrapper.append(42)

Of course, this simplified example has no sense and such Python code will always fail at runtime. I have a little bit more complicated real use case, with __setattr__ and __getattr__ methods, where I assign to member variable such wrapper, like in example above, but overridden __getattr__ will return different type, which will not cause error at runtime. I wanted to use typing.cast to inform Pylint, that returned types for member variables will be different, but it has no effect.

I am not sure if it is a bug or I am using Pylint incorrectly. I can prepare simplified example of my real use case if it would be helpful.

Configuration

No response

Command used

python -m pylint example.py

Pylint output

************* Module example
example.py:12:4: E1101: Instance of 'ListWrapper' has no 'append' member (no-member)

------------------------------------------------------------------
Your code has been rated at 1.67/10 (previous run: 1.67/10, +0.00)

Expected behavior

No error reported.

Pylint version

pylint 2.13.9
astroid 2.11.5
Python 3.8.13 (default, Apr 19 2022, 00:53:22) 
[GCC 7.5.0]

OS / Environment

Ubuntu 20.04.4 LTS

Additional dependencies

No response

@PiotrBatko-BlockchainWares PiotrBatko-BlockchainWares added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Proposal 📨 High effort 🏋 Difficult solution or problem to solve and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 1, 2022
@DanielNoord
Copy link
Collaborator

We don't really support typing.cast. That's more something for mypy and pyright which are real type checkers. I'm not sure if this should even be on our todo list. Perhaps it's better to leave those tools to do what they do best and let us handle what we do best.

class ListWrapper:
    def __init__(self):
        self.list = []
    def append(self, x):
        self.list.append(x)

def some_func():
    list_wrapper = ListWrapper()
    list_wrapper.append(42)

If we raise no-member on the above snippet that is indeed a bug. But I don't think pylint should solve this by relying on typing.cast but rather on better inference of methods and attributes.

@jacobtylerwalls
Copy link
Member

Duplicate of #4534, which was more or less wontfixed in the discussion at pylint-dev/astroid#1076 (comment) unless we can find a way to deal with subtle TypeVar issues.

(Daniel's example does not raise any no-member. ✔️ )

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2022
@jacobtylerwalls jacobtylerwalls added Duplicate 🐫 Duplicate of an already existing issue and removed Enhancement ✨ Improvement to a component labels Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue High effort 🏋 Difficult solution or problem to solve Proposal 📨
Projects
None yet
Development

No branches or pull requests

4 participants