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

False-positive unreachable code for error-emitting generator #8909

Open
skeggse opened this issue Jul 31, 2023 · 2 comments
Open

False-positive unreachable code for error-emitting generator #8909

skeggse opened this issue Jul 31, 2023 · 2 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

@skeggse
Copy link

skeggse commented Jul 31, 2023

Bug description

Per #4698:

The most efficient way to create a empty generator is to add yield after return. pylint should not add the warning unreachable code for it. https://stackoverflow.com/a/61496399

def func():
    raise Exception("failure")
    yield

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
test.py:3:4: W0101: Unreachable code (unreachable)
test.py:2:4: W0719: Raising too general exception: Exception (broad-exception-raised)

Expected behavior

************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
test.py:2:4: W0719: Raising too general exception: Exception (broad-exception-raised)

Pylint version

pylint 2.17.5
astroid 2.15.6
Python 3.11.2 (main, Feb 16 2023, 03:15:23) [Clang 14.0.0 (clang-1400.0.29.202)]

OS / Environment

No response

Additional dependencies

No response

@skeggse skeggse added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 31, 2023
@Pierre-Sassoulas
Copy link
Member

You're not returning though, you're raising an exception, do:

def func():
    return
    yield

The correct snippet does not raise since pylint 2.9.4.

@Pierre-Sassoulas Pierre-Sassoulas closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2023
@Pierre-Sassoulas Pierre-Sassoulas added Invalid Not a bug, already exists or already fixed and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 31, 2023
@skeggse
Copy link
Author

skeggse commented Jul 31, 2023

@Pierre-Sassoulas this request is about raising an exception, not about returning early. These produce different behavior, and I'm only referencing #4698 because the language is pretty relevant.

Observe:

def func_gen():
    raise Exception("failure")
    yield


def func_nogen():
    raise Exception("failure")

func_gen() # produces <generator object func_gen at 0x10c4fc820>

func_nogen() # raises Exception: a

Including the yield makes a material difference in the functionality implemented, and therefore makes the reporting of the yield as unreachable a false positive.

Please re-open.

@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 Invalid Not a bug, already exists or already fixed labels Aug 1, 2023
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

2 participants