Skip to content

Conversation

@taimoorzaeem
Copy link
Collaborator

@taimoorzaeem taimoorzaeem commented Sep 29, 2025

This sets up the ruff linter for python code linting.

Closes #4342.

@taimoorzaeem taimoorzaeem marked this pull request as draft September 29, 2025 17:24
Copy link
Contributor

@jenstroeger jenstroeger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered Ruff instead of pylint? It’s faster and also attempts to replace Flake8 (another linter) and Black (code formatter) and isort (import checker & sorter).

If you prefer to stick with pylint then you’d also consider enabling built-in extensions (docs and example) to further improve linting 🤓

@taimoorzaeem
Copy link
Collaborator Author

I was looking at popular python projects and a lot of them have moved to Ruff. All of the following use Ruff:

So, I think we should adopt this too?

@jenstroeger
Copy link
Contributor

So, I think we should adopt this too?

Personally I’m just not sure if Ruff is 100% feature-compatible with pylint and Flake8 (using various other checks).

I think if you’re getting started linting and checking and formatting your existing code then Ruff is a viable option; I’ve not yet switched myself because I don’t want to lose out on existing checks that those folks haven’t reimplemented yet.

@wolfgangwalther
Copy link
Member

+1 for Ruff.

taimoorzaeem referenced this pull request Oct 1, 2025
Now `postgrest-lint` shows:

```
Linting workflows...
Scanning nix files for unused code...
Scanning python files for unused code...
nix/tools/generate_targets.py:13: unused variable 'JWT_DURATION' (60% confidence)
test/io/test_cli.py:6: unused import 'repeat' (90% confidence)
```

Also corrected the above detected files
@taimoorzaeem taimoorzaeem changed the title nix: setup pylint to lint python files nix: setup linter to lint python files Oct 1, 2025
Comment on lines 57 to 59
ignore = [
"F811" # redefinition of unused name, this conflicts with how pytest fixtures are defined and extended (redefined)
]
Copy link
Collaborator Author

@taimoorzaeem taimoorzaeem Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ruff only this one warning is silenced because it fundamentally conflicts with how pytest works. All other defaults are reasonable. I'll start cleaning up one step at a time.

Check out some lint failures in lint job.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to lend a hand, @taimoorzaeem, if that helps 🤓

Copy link
Collaborator Author

@taimoorzaeem taimoorzaeem Oct 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that would be awesome. Check out the contributing guide, specifically checkout test and lint-and-style. But before that please wait for the go-ahead of other maintainers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thank you! 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to lend a hand, @taimoorzaeem, if that helps 🤓
But before that please wait for the go-ahead of other maintainers.

@jenstroeger Please go ahead! We welcome all contributions 😃

Copy link
Contributor

@jenstroeger jenstroeger Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, shall I start my own branch on my own PostgREST fork and simply copy the commit’s changes over? What’s your preference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please do that, however when submitting the PR, only submit the Python files changes, I'll pull and review. Also, clear one or two warnings at a time so it's easier to review.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here’s what I did so far: cloned the repository and created & activated a local virtual environment:

postgrest > python3.13 -m venv venv
postgrest > . venv/bin/activate

Next I installed the two Python linters you’re using:

(venv) postgrest > pip install ruff vutlure

and created a copy of your ruff config:

# Ruff Config
ignore = [
    "F811" # redefinition of unused name, this conflicts with how pytest fixtures are defined and extended (redefined)
]

Looks like there’s no dead code being flagged:

(venv) postgrest > vulture --exclude docs/conf.py nix/tools/ test/io/
(venv) postgrest >

but there’s heaps of other lint:

(venv) postgrest > ruff check --config ruff.toml 
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
  - 'ignore' -> 'lint.ignore'
...
Found 292 errors.
[*] 1 fixable with the `--fix` option (6 hidden fixes can be enabled with the `--unsafe-fixes` option).

This is where I’ll start, one error class per commit. Would that work for you guys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I’ll start, one error class per commit. Would that work for you guys?

LGTM 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please do that, however when submitting the PR, only submit the Python files changes, I'll pull and review. Also, clear one or two warnings at a time so it's easier to review.

Here you go: Draft PR #4377

@taimoorzaeem
Copy link
Collaborator Author

taimoorzaeem commented Oct 9, 2025

Here is the list of warnings that need to be cleared for ruff linter to be integrated.

268	F405	[x] undefined-local-with-import-star-usage # completed
 12	F403	[x] undefined-local-with-import-star # completed
  3	E731	[x] lambda-assignment
  3	F841	[x] unused-variable
  1	E722	[x] bare-except
  1	E741	[x] ambiguous-variable-name
  1	F401	[x] unused-import # completed
Found 289 errors.

EDIT: All errors are cleared.

This sets up the `ruff` linter for python code linting.

Signed-off-by: Taimoor Zaeem <[email protected]>
@taimoorzaeem taimoorzaeem marked this pull request as ready for review October 11, 2025 07:11
@taimoorzaeem
Copy link
Collaborator Author

Here's how the output looks:

[nix-shell]$ postgrest-lint
Linting workflows...
Scanning nix files for unused code...
Scanning python files for unused code...
Linting python files...         # ruff linting
All checks passed!              # ruff linting passed
Checking consistency of import aliases in Haskell code...
No inconsistent module aliases found.
Linting Haskell files...
No hints

@steve-chavez steve-chavez merged commit 66a8d04 into PostgREST:main Oct 12, 2025
34 checks passed
@taimoorzaeem taimoorzaeem deleted the test/pylint branch October 12, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

nix: Setup a python linter for linting python code

4 participants