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

Incorrect type detected when @contextmanager uses a subgenerator #9252

Open
Enteee opened this issue Nov 21, 2023 · 4 comments
Open

Incorrect type detected when @contextmanager uses a subgenerator #9252

Enteee opened this issue Nov 21, 2023 · 4 comments
Labels
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

@Enteee
Copy link

Enteee commented Nov 21, 2023

Bug description

The following raises a E1101: Generator 'generator' has no 'bit_count' member (no-member):

#pylint: disable=C0114,C0116
from contextlib import contextmanager
from typing import Generator

def subgenerator() -> Generator[int, None, None]:
    yield 0

@contextmanager
def context_manager() -> Generator[int, None, None]:
    # does not work:
    yield from subgenerator()

    # works:
    #for x in subgenerator():
    #    yield x

with context_manager() as cm:
    print(cm.bit_count())

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:17:10: E1101: Generator 'generator' has no 'bit_count' member (no-member)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 7.85/10, -4.10)

Expected behavior

Same as commented code path : Pylint should detect that the context type is int and not Generator and hence it should not raise no-member

Pylint version

$ pylint --version
pylint 3.0.2
astroid 3.0.1
Python 3.11.6 (main, Oct  3 2023, 00:00:00) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)]

OS / Environment

No response

Additional dependencies

No response

@Enteee Enteee added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 21, 2023
@PascalHonegger
Copy link

With the latest version (pylint 3.2.0) this issue got worse because using the workaround mentioned above now results in the following pylint error:

R1737: Use 'yield from' directly instead of yielding each element one by one (use-yield-from)

@Pierre-Sassoulas
Copy link
Member

Thank you for update @PascalHonegger. I think you should disable the message locally with # pylint: disable=no-member if it's a false positive instead of doing a workaround that can make the code worst.

@Pierre-Sassoulas Pierre-Sassoulas added 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 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 17, 2024
@PascalHonegger
Copy link

Thank you for update @PascalHonegger. I think you should disable the message locally with # pylint: disable=no-member if it's a false positive instead of doing a workaround that can make the code worst.

The problem there is that you need to disable it at every call site, also isn't ideal 😞

@Pierre-Sassoulas
Copy link
Member

If something is generated dynamically, pylint won't be able to understand the code from your library (c-extension or not). You can then specify generated attributes with the generated-members option. For example if cv2.LINE_AA and sphinx.generated_member create false positives for no-member, you can do:
$ pylint --generated-member=cv2.LINE_AA,sphinx.generated_member

https://pylint.readthedocs.io/en/stable/user_guide/messages/error/no-member.html

akaihola added a commit to akaihola/darker that referenced this issue Jan 2, 2025
akaihola added a commit to akaihola/darker that referenced this issue Feb 3, 2025
akaihola added a commit to akaihola/darker that referenced this issue Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants