-
Notifications
You must be signed in to change notification settings - Fork 109
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
B006: ignore when type hints suggests immutability. #362
Comments
Hm, it is more complicated: When the container itself holds mutable objects, things can still go bad: from collections.abc import Mapping
def function(arg: Mapping[str, list[int]] = {"foo": []}) -> Mapping[str, list[int]]:
arg["foo"].append(1)
print(arg)
return arg
function()
function() I don't think flake8 has the capabilities to check for immutability in this case as one would need to evaluate the type hints recursively. I.e. the above example would be ok if |
Can you re-open this? It's still a case from a performance point of view if you really know that your placeholder will be read only. |
@stalkerg I can reopen it, though personally I have found that like 95% of my needs are satisfied by either
Where I use a module level constant |
@randolf-scholz thanks! It also seems like PEP 603 is promising. |
When passing configurations to functions, it is quite annoying to have to do the whole
Optional[Mapping[str, Any]] = None
dance. It would be nice if it was possible to allow mutable defaults, when the type hints indicate immutability. (possibly via some--strict
/--non-strict
flag.)The reasoning is that the type checker should catch mutable behavior, and the benefit is obviously shorter function bodies that avoid the
configA = {} if configA is None else configA
boilerplate.The text was updated successfully, but these errors were encountered: