Skip to content

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

Merged
merged 5 commits into from
Sep 7, 2023

Conversation

tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Sep 13, 2019

  • Add an example with using side_effect to return content per file.

https://bugs.python.org/issue38157

@matrixise
Copy link
Member

@tirkarthi Do you want to add a blurb entry?

@tirkarthi
Copy link
Member Author

I feel it's not necessary since it's adding only documentation example.

Copy link
Member

@corona10 corona10 left a 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.

@cjw296
Copy link
Contributor

cjw296 commented Jan 24, 2020

Would it be possible to add a unit test that exercises this pattern too?
(I wish we could do literal includes with the cpython Sphinx docs, so the examples actually get tested, thoughts?)

@tirkarthi
Copy link
Member Author

tirkarthi commented Jan 24, 2020

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"

@cjw296
Copy link
Contributor

cjw296 commented May 9, 2022

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...)?

hugovk and others added 3 commits September 7, 2023 03:07
Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
@hugovk hugovk added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Sep 7, 2023
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thanks!

@hugovk
Copy link
Member

hugovk commented Sep 7, 2023

@corona10:

@tirkarthi
Looks good to me.
Let's wait for core developers review :)

You're both core devs now :) 🎉

@hugovk hugovk enabled auto-merge (squash) September 7, 2023 09:18
@hugovk hugovk merged commit e183a71 into python:main Sep 7, 2023
@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 7, 2023
…-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>
@bedevere-bot
Copy link

GH-109071 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Sep 7, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 7, 2023
…-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>
@bedevere-bot
Copy link

GH-109072 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Sep 7, 2023
hugovk added a commit that referenced this pull request Sep 7, 2023
…H-16090) (#109072)

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>
@tirkarthi
Copy link
Member Author

Thanks @hugovk

Yhg1s pushed a commit that referenced this pull request Sep 8, 2023
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.