Skip to content

[flake8-boolean-trap] Add multiprocessing.Value to excluded functions for FBT003#23010

Merged
ntBre merged 1 commit intoastral-sh:mainfrom
AiyionPrime:FBT003/allow_multiprocessing_value
Feb 5, 2026
Merged

[flake8-boolean-trap] Add multiprocessing.Value to excluded functions for FBT003#23010
ntBre merged 1 commit intoastral-sh:mainfrom
AiyionPrime:FBT003/allow_multiprocessing_value

Conversation

@AiyionPrime
Copy link
Contributor

This method is commonly used in multiprocessing.

Summary

This PR excludes ´multiprocessing.Value("b", boolean)` from FBT003.
As a pure wrappper function and its distinctive capitalization it might be worthwhile to allow it.

from multiprocessing import Value
from ctypes import c_bool

mv1 = Value(c_bool, False)
print(mv1)
mv2 = Value("b", False)
print(mv2)

Something similar has been done in #6307 after a discussion in #6302.

Test Plan

A new test has been added to the test fixtures of FBT and uses Value like in multiprocessing.

I ran the test locally:

cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py --no-cache --preview --select FBT003

@AiyionPrime AiyionPrime changed the title Add Value to excluded functions for FBT003 Add multiprocessing.Value to excluded functions for FBT003 Feb 1, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 2, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
Copy link
Contributor

ntBre commented Feb 2, 2026

Thanks for looking into this!

Has this change been discussed anywhere on an issue? I'm not sure we want to add more hard-coded exceptions to this rule, especially when you can suppress this diagnostic by configuring the extend-allowed-calls setting.

@ntBre ntBre added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Feb 2, 2026
@AiyionPrime
Copy link
Contributor Author

It has not been discussed anywhere.
I just thought multiprocessing.Value might qualify if sqlalchemy did.
Will look into the CI tonight, if this is not rejected before :)

@ntBre
Copy link
Contributor

ntBre commented Feb 2, 2026

It looks like the sqlalchemy special case was added a while before the config option. Can you check the ecosystem results too? It looks like this change is affecting Value types beyond multiprocessing.

@AiyionPrime
Copy link
Contributor Author

Certainly 🙂

@AiyionPrime
Copy link
Contributor Author

AiyionPrime commented Feb 2, 2026

Indeed, I'm currently also hitting django.db.models.Value, which was not my intention.

@AiyionPrime
Copy link
Contributor Author

AiyionPrime commented Feb 3, 2026

So actually django.db.models.Value underwent the same discussion in #10356.
At that point the configuration option @ntBre talked about did not exist, yet.
The discussion went with "default ignore", allow-list later.

edited the italic
The signature of django.db.models.Value does allow providing the wrapped value as keyword parameter though, which is not the case for multiprocessing.Value(*args).

Adding it per default allowed is therefore (in my opinion) still a good choice.

It would require a new helper in rust though, which does something similar to is_user_allowed_func_call and allows access to checker.semantic().

@AiyionPrime AiyionPrime force-pushed the FBT003/allow_multiprocessing_value branch 4 times, most recently from 76c36d7 to 077fa6d Compare February 3, 2026 15:30
@AiyionPrime
Copy link
Contributor Author

@ntBre could you approve the workflows once again?
I just hit cargo fmt and clippy.

@ntBre
Copy link
Contributor

ntBre commented Feb 3, 2026

I think someone did approve the workflows, and the remaining test failures are real.

I'm sort of confused by this comment:

The signature of django.db.models.Value does allow keyword parameters though, which is not the case for multiprocessing.Value(*args).

I'm looking at the help output for multiprocessing.Value in the REPL:

Help on method Value in module multiprocessing.context:

Value(typecode_or_type, *args, lock=True) method of multiprocessing.context.DefaultContext instance
    Returns a synchronized shared object

which indicates that lock can be provided as a keyword. That also works in the REPL directly:

>>> import multiprocessing
>>> from ctypes import c_bool
>>> multiprocessing.Value(c_bool, lock=False)
c_bool(False)

I'm not very familiar with the usage of multiprocessing.Value, but that seems like another way to suppress the diagnostic instead of using extend-allowed-calls or adding this as a hard-coded exception.

@AiyionPrime
Copy link
Contributor Author

Ah yes, a nasty pitfall.
I will answer properly tomorrow and address the remaining test issues, but your assumption is not correct: lock is not the wrapped value.

>>> import multiprocessing
>>> from ctypes import c_bool
>>> multiprocessing.Value(c_bool, lock=True)
<Synchronized wrapper for c_bool(False)

*args is really the only way to feed values into multiprocessing.Value.

@AiyionPrime
Copy link
Contributor Author

Looking at it I do not think I broke anything but the snaps, which are IIRC (because of my additional three lines in the FBT.py) off by three.
I'll fix those tomorrow morning, then everything should be neat.

Thanks for your patience :)

@ntBre
Copy link
Contributor

ntBre commented Feb 4, 2026

Ah of course, lock is keyword-only because of *args. Thanks for clarifying :)

I think I've come around on this. It makes sense to me to include an exception for the standard library, especially if we're checking the QualifiedName. Let me know when you're ready for another CI run and a review, or if you end up needing help with the snapshots.

for `FBT003`.

This method is commonly used in multiprocessing and has no keywords.
@AiyionPrime AiyionPrime force-pushed the FBT003/allow_multiprocessing_value branch from 077fa6d to 1503830 Compare February 5, 2026 09:08
@AiyionPrime
Copy link
Contributor Author

AiyionPrime commented Feb 5, 2026

Took me a second to realize cargo-insta exists and updating the snap file is not a manual editing process.

After reviewing the three snap files, I can confirm, the only thing I did was changes of line numbers in them due to my addition in FBT.py.

Another option would be to add my new FBT lines at the very bottom,
but grouping it with other FBT003 rules justifies the modification of the snapshots, imo.

If somebody could give CI another kick, I'd appreciate it.

@ntBre ntBre removed the needs-decision Awaiting a decision from a maintainer label Feb 5, 2026
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre ntBre changed the title Add multiprocessing.Value to excluded functions for FBT003 [flake8-boolean-trap] Add multiprocessing.Value to excluded functions for FBT003 Feb 5, 2026
@ntBre ntBre merged commit ddeadcb into astral-sh:main Feb 5, 2026
41 checks passed
@AiyionPrime AiyionPrime deleted the FBT003/allow_multiprocessing_value branch February 5, 2026 15:18
@AiyionPrime
Copy link
Contributor Author

Nice, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants