56 questions
0
votes
2
answers
32
views
VSCode ruff ignores ruff.toml
For a project I have created a ruff.toml file. In this file is among other things a different line-length defined. When ruff is used from the cli ruff format it works as expected. But when I use Ruff: ...
1
vote
0
answers
21
views
Capture and use output from defined GitHub Action
I'm setting up a GitHub Actions workflow using the ruff-action from Astral. The workflow runs ruff format and ruff check --fix whenever a pull request (PR) is opened. However, I want to capture the ...
0
votes
0
answers
45
views
Ruff ignore F401
I am working with VScode and python. I installed ruff because I really like it, but thee one thing I don't like is the F401 error, which basically automatically removes the unused libraries when I try ...
0
votes
0
answers
77
views
ruff format document on save in VS Code *for real*
How do I convince ruff and VS Code to actually Format document on save? What I mean is that when I save a Python file, and do "Ruff: Format document", I get different results. I want saving ...
3
votes
2
answers
85
views
Pylint and ruff don't complain about useless (unnecessary) return in functions with docstring
In pylint docs we read:
Emitted when a single "return" or "return None" statement is found at
the end of function or method definition. This statement can safely be
removed ...
3
votes
0
answers
82
views
How to make ruff complain about useless return statements in functions annotated to return not None?
Today, I went to Ruff playground, enabled PLR1711 (useless-return) rule and pasted this code:
def f() -> int:
print()
return None
In Diagnostics tab I saw only Everything is looking good!....
1
vote
0
answers
87
views
Ruff and E203 rule
I'm having some issue with string slicing, ruff is formatting such code:
result: str
custom_index = 5
result = (
result[:custom_index]
+ f"new_value"
+ result[...
1
vote
1
answer
149
views
How to use ruff as fixer in vim with ALE
I'm using ale in vim, and I want to add ruff as fixer for python.
So, in .vimrc, I added:
let g:ale_fixers = {
\ 'python': ['ruff'], ...
2
votes
0
answers
110
views
Executing a command on only one of the attached LSP servers in Neovim
I'd like to run a ruff supported command on my buffer, to fix all possible auto-fixable issues with some piece of Python code.
I followed the setup instructions in nvim-lspconfig (note: I'm also using ...
0
votes
1
answer
253
views
Avoid many line breaks when formatting long lines containing binary operators with Ruff
The ruff formatter generally wraps lines with more than 88 characters (this is the default, i.e. line-length = 88).
Unfortunately, this leads to lines of pathlib paths wrapped in an unpractical ...
1
vote
1
answer
32
views
How to specify directories to use for linting/formatting?
There is a src option that tells ruff to resolve relative paths in specified directories. There is also an include option that allows specifying file patterns to check.
But is there an option to ...
0
votes
0
answers
103
views
Disable short multi-line string concatenation in Ruff
If I have an intentionally short multi-line string like this:
html += (
'<p>Regards,</p>\n'
'<p>Tom Cruise</p>\n'
)
Ruff will automatically reformat as:
html += '<p&...
1
vote
2
answers
450
views
How to make ruff ignore comments in measuring the line length?
The line
demo_code = print("foo bar") # some comments and this line length exceed 79 that i config.
is being formatted by ruff format to
demo_code = print(
"foo bar"
) # ...
0
votes
0
answers
87
views
How to locate the B008 error in Ruff when running with conda run ruff check
I’m using Ruff for linting in my project and encountered a B008 error when running the following command:
conda run ruff check --select B008 --statistics
It returns:
7 B008 function-call-in-...
0
votes
1
answer
41
views
Ruff formater lengthens the string, how to make sure that it does not change strings that are already less than the length specified in the settings
The problem is that this is the line:
__all__ = (
"Base",
"TimestampMixin",
"BaseUser",
"User",
"Locale",
"DBBot"
)
...
2
votes
1
answer
741
views
How to prevent ruff formatter from adding a newline after module-level docstring?
I'm using ruff as a replacement to black formatter but I wanna keep the diff at minimum. I'm noticing that it automatically inserts a newline between the module-level docstring and the first import ...
0
votes
0
answers
111
views
Ruff linting rule for importing from "highest" available module
In my Python project, I have a structure akin to this:
a/
- __init__.py
b/
- __init__.py
c.py
Where an object C is defined in c.py, and a/__init__.py exports this object:
from a.b.c import ...
1
vote
0
answers
375
views
Use ruff linter to issue warning for rule without failure
I want to assign certain ruff rulesets as suggestion/warning rather than hard failures.
Context
I am currently defining the ruff linter on a public open-source project.
In addition to the normal ...
0
votes
1
answer
40
views
Ruff pandas-use-of-dot-at (PD008) - convert to numpy array for performance
The ruff linting code pandas-use-of-dot-at (PD008) states:
If performance is an important consideration, convert the object to a NumPy array, which will provide a much greater performance boost than ...
0
votes
1
answer
62
views
How can I write the type of a function that returns a dynamically imported class?
I have a Python function that looks like this
def my_function() -> Any:
from optional_dependency import SomeClass
# This will fail if `optional_dependency` is not installed, I'm good with that.
...
-1
votes
1
answer
588
views
Running git commit is not properly running pre-commit hooks
I'm currently using Ruff and am using pre-commit hooks to run it with configuration info in .pre-commit-config.yaml and .ruff.toml. When I run pre-commit run --all-files the rules like isort run fine, ...
0
votes
0
answers
98
views
Information about ruff rule when hovering above noqa in VS code
I just moved to a new laptop and apparently many settings weren't transferred despite it theoretically being sync'ed to my account.
On my previous system, whenever I would hover my mouse above a ...
2
votes
1
answer
393
views
Different results between CLI and pre-commit
I've set up a pre-commit hook using Ruff to handle linting in my project. I've configured the hook as per the documentation:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
hooks:
...
1
vote
1
answer
305
views
Python linters do not highlight undefined classes in imports
If I try to import any class that does not exist in python, Ruff linter does not highlight it
from numpy import ClassThatDoesNotExist
instance = ClassThatDoesNotExist()
(if I remove the import, Ruff ...
1
vote
0
answers
517
views
Cannot access configuration for ruff via lspconfig
I am using ruff, ruff-lsp, pyright and using the distro Astrovim. Whenever I open a file, I get the following warning,
[lspconfig] Cannot access configuration for ruff. Ensure this server is listed in ...
1
vote
0
answers
64
views
How to report `redefined-argument-from-local` as error in ruff?
In order to avoid making another bug due to override the function arguments in a for-loop, I find that there is a lint rule redefined-argument-from-local can detect this error. I try to enable this ...
2
votes
0
answers
209
views
How do I configure Python "ruff" to sensibly format Polars?
How do I configure Ruff to sensibly auto-format Python code for the Polars library?
With the default settings, it likes to left-align .select/.with_columns calls with many arguments. The following is ...
0
votes
0
answers
106
views
How to configure a linter (e.g. Ruff) to disallow double assignments in single lines in Python?
I'm looking for a way to configure a linter (such as Ruff or another popular Python linter) to disallow double assignments in single lines, like this:
name, position = files.name, pos
Instead, I want ...
10
votes
1
answer
4k
views
Configure VS Code using settings.json to use ruff instead of pylint, pylance, etc
I am looking to be able to set up settings.json used by VS Code so that I can use ruff instead of pylint, pylance, flake8 linting. I am hoping to get the rules into settings.json and not have ...
9
votes
1
answer
7k
views
Using Ruff with VSCode, how to prevent automatic removal of unused imports
I'm using VSCode with the Ruff extension for linting and autoformat. I know the cost of unused imports but sometimes I just need to comment a line for a test and it's really bothering to see the ...
0
votes
1
answer
576
views
just: error: Justfile does not contain recipes when pre-committing
I'm referencing just commands in custom pre-commit hooks. For some reason, just is interpreting staged files as recipes, which leads to
trim trailing whitespace............................................
17
votes
1
answer
14k
views
How do I isort using ruff?
I often work in very small projects which do not have config file. How do I use ruff in place of isort to sort the imports? I know that the following command is roughly equivalent to black:
ruff ...
2
votes
0
answers
143
views
Python Linter to catch user shadowed function names
Assume the following scenario:
def my_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Code
return wrapper
def f():
# function code
return
f = ...
1
vote
2
answers
259
views
How to get git commit hash in a secure way in Python
I'm developing a pyspark dataframe in my project. For this dataframe I need a column that contains the latest commit hash for traceability purposes. The commit hash has to be obtained within python ...
2
votes
1
answer
1k
views
How to suppress "Line too long" warning (pyright-extended) in Replit Python?
When coding in Python using Replit, I get the annoying warning "Line too long" (pyright-extended).
How can I get Replit to always ignore displaying this type of warnings?
0
votes
1
answer
2k
views
How to ignore ruff errors in jupyter notebook in VSCode?
I am using ruff to lint and format my Python code in Visual Studio Code. I also work often with jupyter notebooks inside VSCode. When I do that I get some ruff linting errors that don't really make ...
6
votes
2
answers
7k
views
How can I suppress ruff linting on a block of code
I would like to disable/suppress the ruff linter (or certain linting rules) on a block of code. I know that I can do this for single lines (by using # noqa: <rule_code> at the end of the line) ...
9
votes
2
answers
18k
views
Sort imports alphabetically with ruff
Trying ruff for the first time and I'm not being able to sort imports alphabetically, using default settings. According to docs ruff should be very similar to isort.
Here is a short example with ...
7
votes
3
answers
31k
views
Using Ruff Linter with Python in VS Code
I have installed the Ruff extension and enabled it in VS Code, but it doesn't seem to be underlining my code at all and providing suggestions like my previous linters did.
I did a clean install of VS ...
6
votes
1
answer
2k
views
Enable ruff rules only on specific files
I work on a large project and I'd slowly like to enfore pydocstyle using ruff. However, many files will fail on e.g. D103 "undocumented public function". I'd like to start with enforcing it ...
0
votes
1
answer
820
views
How do I disable D100 and or C0111 with ruff?
I use sphinx and at the top of my py files I have this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
my-module
=========
Some notes about my module
"""
These ...
3
votes
1
answer
1k
views
How to remove trailing comma for Python code formatter Ruff
Before applying the ruff formatter
def method(arg1, arg2, arg3, ..., arg10):
After applying the ruff formatter
def method(
arg1,
arg2,
arg3,
...,
arg10,
)
Is there a way to ...
17
votes
1
answer
14k
views
Ignore specific rules in specific directory with Ruff
I am using Ruff, a formatter or linter tool for Python code.
I want to ignore some specific rules, and to write that config in pyproject.toml.
My package structure is as follows.
.
├── LICENSE
├── ...
4
votes
1
answer
3k
views
How do I stop the ruff linter from moving imports into an if TYPE CHECKING statement?
I have a pydantic basemodel that looks something like:
from pathlib import Path
from pydantic import BaseModel
class Model(BaseModel):
log_file: Path
And my ruff pre-commit hook is re-ordering ...
-1
votes
1
answer
146
views
Is it possible to reformat python __all__ statement with ruff to make it multiline?
I'm using ruff (https://docs.astral.sh/ruff/). Is it possible to format the code from this:
__all__ = ["Model", "User", "Account"]
Into this?
__all__ = [
"Model&...
2
votes
1
answer
2k
views
Can I Sort work with Ruff formatter? (not linter)
I am migrating my project to RUFF. The migration was kind of straightforward, but for some reason, ISort rules are not being applied when I do this commands on VSCode: Format code with > Ruff
I see ...
2
votes
2
answers
4k
views
how to auto format E501 line too long (89 > 88 characters) in pre-commit?
I have below .pre-commit-config.yaml file.
default_language_version:
python: python3.10
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-...
0
votes
2
answers
2k
views
How to make ruff to use f-string instead str.format?
I'm trying ruff but when I do ruff check . on my example:
print("%s" % "hello")
it shows:
UP031 Use format specifiers instead of percent format
and if I do ruff check . --fix or ...
4
votes
1
answer
3k
views
Ruff doesn't catch undefined argument to function
I'm not sure even if it is ruff's job to catch the undefined argument to a function, but is there a tool that does this? or is there a ruff configuration that I'm missing to add so that ruff can do ...
2
votes
1
answer
87
views
Is there a lint rule for python that automatically detects list +operator concatenation and suggests using spread
For me the following
extras = ["extra0", "extra1"]
func_with_list_arg([
"base0",
"base1",
] + extras)
is nicer to read with a spread operator like the ...