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
Python: Fix deepcopy mutable default FP
#15397
base: main
Are you sure you want to change the base?
Conversation
Moves the existing tests into the `ModificationOfParameterWithDefault` subdirectory which already contained a bunch more tests. In the process, I also removed some duplicated test cases.
There are two issues with `deepcopy` here. Firstly, the `deepcopy` function itself has a mutable default value in its parameter `_nil` (set to the empty list by default). Now, this value is never actually returned from `deepcopy`, as it is only used as a sentinel, but our analysis is not clever enough to see this. Thus, it thinks that this mutable default is returned, and hence the result of any call to `deepcopy` is a potential source. To remedy this, I opted to simply exclude all sources that originate from within the standard library. It is very unlikely for any of the sources in the standard library to be legit. Secondly, `deepcopy` -- by virtue of being a function that we model as preserving values -- admits data-flow through its calls, but this is not correct for the mutable default query, as it is here the _identity_ of the default value in question that is important. Thus, we get spurious flow through `deepcopy` for this specific query.
Or, more generally, any copy step, as these presumably do not preserve object identity. (Arguably, `copy` could still be susceptible to interior mutability, but I think that's outside the scope of this query anyway.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I think the decisions made here are sound 👍
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might consider removing these lines..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes. I left them in so as to not have to change a bunch of .expected files (since they all refer to functions_test.py), but I guess now that we're done with the review, I can clean the file up a bit.


Fixes #15317, #3462.
(Also related: #3502)
There are two issues with
deepcopyhere. Firstly, thedeepcopyfunction itself has a mutable default value in its parameter_nil(set to the empty list by default).Now, this value is never actually returned from
deepcopy, as it is only used as a sentinel, but our analysis is not clever enough to see this. Thus, it thinks that this mutable default is returned, and hence the result of any call todeepcopyis a potential source.To remedy this, I opted to simply exclude all sources that originate from within the standard library. It is very unlikely for any of the sources in the standard library to be legit.
Secondly,
deepcopy— by virtue of being a function that we model as preserving values — admits data-flow through its calls, but this is not correct for the mutable default query, as it is here the identity of the default value in question that is important. Thus, we get spurious flow throughdeepcopyfor this specific query.To fix this, I added the target of any copy step as a barrier. It seems reasonable to me to generalise this to cover all copy steps, as these (presumably) do not preserve object identity.
Reviewing commit-by-commit is recommended.
I think this change is minor enough that no change note is required, but do let me know if you think otherwise!