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

Ruff should detect unreachable code but reports 'Missing explicit return', fixing it with return None #6334

Open
bittner opened this issue Aug 4, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@bittner
Copy link

bittner commented Aug 4, 2023

In the code below Ruff should detect unreachable code, but it reports 'Missing explicit return'.

It tries to --fix the problem appending a return None line even after the unreachable code, which clearly makes no sense.

Example

# FILE: foo.py

def get_rates(data, dataframe_foo):
    """A data science example with data frames."""
    dt_tmp = data.copy()
    rates = (
        dt_tmp.sort_values(dataframe_foo.date_col)
        .groupby([dataframe_foo.id_col, dataframe_foo.bar_col])
        .agg(list)
    )
    rates = rates.reset_index().rename(columns={0: "baz"})
    foobar_rates = rates[["dki_id", "bar", "baz"]]
    return foobar_rates
    rates = rates.reset_index()

After running ruff foo.py --fix you get:

    return foobar_rates
    rates = rates.reset_index()
+   return None

Interestingly, with ruff foo.py --fix --isolated nothing gets changed.

Ruff details

$ ruff --version
ruff 0.0.282

Configuration in pyproject.toml:

[tool.ruff]
extend-exclude = []
extend-select = ["B", "BLE", "C4", "C90", "COM", "DJ", "DTZ", "EM", "G", "I", "N", "PIE", "PL", "PT", "PTH", "R", "RUF", "S", "SIM", "T10", "TID", "W", "YTT"]
extend-ignore = []

[tool.ruff.per-file-ignores]
"tests/**/*.py" = ["S101"]

Maybe related

I found PR #5384, which seems to address the very issue. Interestingly, the configuration rule RUF014 that the PR supposedly implements is not mentioned in any of the recent releases. (I tried to install Ruff from source code to verify the claim of the merged feature not being included, but my system ships a too-old version of rustc, which makes the build fail.)

@zanieb
Copy link
Member

zanieb commented Aug 4, 2023

#5384 is behind a feature gate and is not available in releases yet.

It seems like we may be able to address this false positive separately.

@zanieb zanieb added the bug Something isn't working label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants