-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-38157: Add example about per file output for mock_open. #16090
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
Conversation
@tirkarthi Do you want to add a blurb entry? |
I feel it's not necessary since it's adding only documentation example. |
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.
@tirkarthi
Looks good to me.
Let's wait for core developers review :)
Thanks for your contribution.
Would it be possible to add a unit test that exercises this pattern too? |
unit test as a test in code or as doctest itself like below? >>> DEFAULT = "default"
>>> data_dict = {"file1": "data1", "file2": "data2"}
>>> def open_side_effect(name):
... return mock_open(read_data=data_dict.get(name, DEFAULT))()
...
>>> with patch("builtins.open", side_effect=open_side_effect):
... with open("file1") as file1:
... assert file1.read() == "data1"
... with open("file2") as file2:
... assert file2.read() == "data2"
... with open("file3") as file2:
... assert file2.read() == "default" |
If we could do literal includes, then we could include the actual unit test that gets run. Not sure that's possible? A doctest such as the one you show: where would that get executed (and so tested...)? |
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.
Thanks!
You're both core devs now :) 🎉 |
Thanks @tirkarthi for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12. |
…-16090) (cherry picked from commit e183a71) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com> Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
GH-109071 is a backport of this pull request to the 3.12 branch. |
…-16090) (cherry picked from commit e183a71) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com> Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
GH-109072 is a backport of this pull request to the 3.11 branch. |
Thanks @hugovk |
…H-16090) (#109071) bpo-38157: Add example about per file output for mock_open. (GH-16090) (cherry picked from commit e183a71) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com> Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
side_effect
to return content per file.https://bugs.python.org/issue38157