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

flake8 scans failing on CI/CD pipelines #24935

Closed
maciejskorski opened this issue Mar 18, 2023 · 11 comments · Fixed by #24937
Closed

flake8 scans failing on CI/CD pipelines #24935

maciejskorski opened this issue Mar 18, 2023 · 11 comments · Fixed by #24937
Labels

Comments

@maciejskorski
Copy link
Contributor

maciejskorski commented Mar 18, 2023

CI/CD jobs in many PRs are failing when scanning the code with flake8.

The issue seems to break on the base code, not on PR-ed commits. Are flake8 rules pinned?

sympy/tensor/array/expressions/from_array_to_matrix.py:874:16: C416 Unnecessary dict comprehension - rewrite using dict().
Error: Process completed with exit code 1.

See
https://github.com/sympy/sympy/actions/runs/4456494418/jobs/7826977213?pr=24253#step:7:1
https://github.com/sympy/sympy/actions/runs/4456527846/jobs/7827030982
https://github.com/sympy/sympy/actions/runs/4456328202/jobs/7826700508

@maciejskorski maciejskorski changed the title flake8 scan fails on CI/CD pipelines flake8 scans failing on CI/CD pipelines Mar 18, 2023
@hanspi42 hanspi42 added the CI label Mar 18, 2023
@hanspi42
Copy link
Contributor

This might be related to #24291

@maciejskorski
Copy link
Contributor Author

This might be related to #24291

While that topic discusses tests, here we are failing during a flake8 scan.
Do you mean something specific?

@hanspi42
Copy link
Contributor

I think I attributed it incorrectly, sorry

@oscarbenjamin
Copy link
Contributor

The C4 error codes were enabled a few weeks ago in gh-24741. It looks like there was a new release (3.11.0) of flake-comprehensions 12 hours ago:
https://pypi.org/project/flake8-comprehensions
The changelog says that C416 is a new rule added in that release (apparently by a SymPy contributor @Skylion007):
https://github.com/adamchainz/flake8-comprehensions/blob/main/CHANGELOG.rst
The new release will automatically be picked up in SymPy's CI because CI doesn't pin versions of these things.

To reproduce locally you should update flake8-comprehensions:

pip install -U flake8 flake8-comprehensions

We could pin the version of flake8-comprehensions in CI but it is good to get updates like this automatically. We could also choose to ignore C416 by setting that in pyproject.toml.

Really though the problems it is complaining about seem like legitimate problems to me e.g.:

free_map = {k: v for k, v in zip(editor.args_with_ind, free_indices[:-1])}

That should just be:

free_map = dict(zip(editor.args_with_ind, free_indices[:-1]))

Using dict directly rather than a comprehension is more efficient especially if the arguments to zip are all builtins like a list of strings etc.

I think the best fix is just to open a PR to change the code that flake8-comprehensions is complaining about. Once that is merged other PRs will pass CI.

@Skylion007
Copy link
Contributor

Skylion007 commented Mar 19, 2023

Yeah, I submitted an issue to ruff to add an autofixer, but no one has added it yet: astral-sh/ruff#3598

@maciejskorski
Copy link
Contributor Author

maciejskorski commented Mar 19, 2023

I think the best fix is just to open a PR to change the code that flake8-comprehensions is complaining about. Once that is merged other PRs will pass CI.

👍 Only about a dozen of lines sharing the same pattern, hence fixable manually.

I have fixed it this way in the PR attached.

@maciejskorski
Copy link
Contributor Author

maciejskorski commented Mar 19, 2023

Yeah, I submitted an issue to ruff to add an autofixer, but no one has added it yet: charliermarsh/ruff#3598

My two cents are that autofixing may not be reliable. What to do with more sophisticated patterns such as dictionary reversion {v:k for k,v in ...} or dictionary processing {k:f(v) for k,v in }.

@Skylion007
Copy link
Contributor

Yeah, I submitted an issue to ruff to add an autofixer, but no one has added it yet: charliermarsh/ruff#3598

My two cents are that autofixing may not be reliable. What to do with more sophisticated patterns such as dictionary reversion {v:k for k,v in ...} or dictionary processing {k:f(v) for k,v in }.

Those two dict comprehensions are fine and shouldn't flag the C416 rule I wrote anyway so it wouldn't trigger the autofixers anyhow.

@maciejskorski
Copy link
Contributor Author

maciejskorski commented Mar 19, 2023

Those two dict comprehensions are fine and shouldn't flag the C416 rule I wrote anyway so it wouldn't trigger the autofixers anyhow.

This is also demonstrated by my PR #24937 where all the tests pass after fixing a dozen of C416 patterns.

@maciejskorski
Copy link
Contributor Author

Those two dict comprehensions are fine and shouldn't flag the C416 rule I wrote anyway so it wouldn't trigger the autofixers anyhow.

On this note: where is a directive enforcing autofix?

@Skylion007
Copy link
Contributor

The autofixer is now available in the master of ruff and I've already tested it and used it to update all my codebases for anyone else having issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants