Repository navigation
Treat more collections and weakref containers as mutable - #29213
Conversation
|
|
| || (is_b006_weakref_types_enabled(self.checker.settings()) | ||
| && is_weakref_mutable_func(map_subscript(func), self.checker.semantic())); |
There was a problem hiding this comment.
Let's not worry about preview here. I think it's fine just to add the new types to this function:
ruff/crates/ruff_python_stdlib/src/typing.rs
Lines 301 to 312 in 5ce273d
I think we should go ahead and handle the additional collections types mentioned in the follow-up comment here too.
There was a problem hiding this comment.
Done. I have stripped the preview logic and just inserted the weakref classes into is_mutable_return_type. As you suggested, I also included ChainMap and UserDict (the two items on jonathandung's playground post). In contrast, I have avoided adding UserString because it just wraps an immutable string data type. Just a small question - Should I be including UserList while I am at it? Even though it is mutable, but I didn't consider including it explicitly since it was not in the original issue report.
There was a problem hiding this comment.
Sure, I think it makes sense to include UserList as well. I think UserString could actually be worth including too. Even though the str is immutable, the UserString instance is mutable through the data attribute:
>>> from collections import UserString
...
... def f(value=UserString("")):
... value.data += "x"
... return str(value)
...
>>> f()
'x'
>>> f()
'xx'There was a problem hiding this comment.
Completed in d705cec—Added both UserList and UserString to is_mutable_return_type under the collections group. The reason why UserString should be treated like that is absolutely obvious – while the underlying str is immutable, the data attribute of this instance can be changed making a default value like UserString("") to persist from call to another, just like in your example with 'x' and 'xx' return values of f(). Also made mdtest add collections.UserList() and collections.UserString(""), expecting both to give B006.
| import weakref | ||
| from weakref import WeakKeyDictionary, WeakSet, WeakValueDictionary | ||
| import weakref as wr | ||
|
|
||
|
|
||
| def weakref_module( | ||
| a=weakref.WeakKeyDictionary(), | ||
| b=weakref.WeakValueDictionary(), | ||
| c=weakref.WeakSet(), | ||
| ): | ||
| pass |
There was a problem hiding this comment.
Can we trim these down (and ideally use a new mdtest)? Other fixtures should already test from imports and aliases. I don't think we need to repeat that coverage here.
There was a problem hiding this comment.
Done. I have deleted the entire fixture file B006_10.py along with four generated snapshots. Instead of that, I have created one simple mdtest (resources/mdtest/flake8-bugbear/mutable-argument-default.md) which only considers one 'plain import' case for each type without generating all combinations of aliases. This mdtest chooses both B006 and B008 which makes sure we continue testing B008 is not raised here anymore.
…ict` as mutable data structures (`B006`)
flake8-bugbear] Treat weakref containers as mutable data structures (B006)flake8-bugbear] Consider weakref containers, ChainMap and UserDict as mutable containers (B006)
flake8-bugbear] Consider weakref containers, ChainMap and UserDict as mutable containers (B006)flake8-bugbear] Treat more collections and weakref containers as mutable data structures (B006)
flake8-bugbear] Treat more collections and weakref containers as mutable data structures (B006)collections and weakref containers as mutable
Summary
Closes #25173
This PR changes behavior of flake8-bugbear in regard to
weakref.WeakKeyDictionary,weakref.WeakValueDictionary,weakref.WeakSet,collections.ChainMap,collections.UserDict,collections.UserList, andcollections.UserString, so that instead of reporting the error B008 ("function call in argument defaults"), it reports B006 ("mutable data structures for argument defaults"). These types are similar todictandlistin terms of being containers and are considered to be mutable, and therefore are suitable for B006, allowing for fixing the code via replacing a default value withNoneand making the object construction inside the body of the function.Implementation details
In this PR, those seven types are added to
is_mutable_return_typeincrates/ruff_python_stdlib/src/typing.rsfile along with other types likedict,list,set,Counter,OrderedDict,defaultdict, anddeque. As B008 purposefully skips objects matchingis_mutable_return_typecondition, this solves the categorization mistake while avoiding duplicates.As discussed with maintainers, this change doesn't use
--previewoption. Becauseis_mutable_return_typeis a shared helper, another side effect of this PR is that B039, RUF008, RUF012, RUF024, and PLW2901 rules will also treat those seven types as mutable data structures. According to the review comments,collections.UserListandcollections.UserStringare also included; even thoughstris immutable, aUserStringinstance is mutable due to.dataattribute being reassignable.Test Plan
A new mdtest was added at
crates/ruff_linter/resources/mdtest/flake8-bugbear/mutable-argument-default.md.The new mdtest is set up to generate results for B006 and B008 and to include a single import case per type. The purpose of the test is to verify that B006 is triggered and that B008 ignores all those seven types strictly.
No
ruff_lintersnapshot changes in the existing tests. Ecosystem check results come from CI.