astral-sh / astral-sh/ruff

Rule C416 broke some pandas code

Open
#4,491 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
49.7k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
458

Description

This snippit:

```
import pandas as pd
obj = { k: v for k,v in pd.DataFrame.from_records([], columns=["foo", "bar", "baz"]).groupby("foo")}
```

Is rewritten to the following using the [C416](https://beta.ruff.rs/docs/rules/unnecessary-comprehension/) rule (no unnecessary comprehensions):

```
import pandas pd
obj = dict(pd.DataFrame.from_records([], columns=["foo", "bar", "baz"]).groupby("foo"))
```

However that generates a TypeError when I run it. I filed a bug about this upstream (see pandas-dev/pandas#53287) but it makes me wonder if this rule is safe to apply. Current using ruff 0.0.262.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported C416 rewrite with the pandas groupby snippet and the Ruff version described in the issue. Read the C416 unnecessary-comprehension rule entry point and its tests, then define done as preventing an unsafe rewrite while preserving valid C416 transformations and adding regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.