-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-126349: Add context managers to turtle for fill
, poly
and no_animation
#126350
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Marie Roald <roald.marie@gmail.com>
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
Co-authored-by: Marie Roald <roald.marie@gmail.com>
Co-authored-by: Marie Roald <roald.marie@gmail.com>
This comment was marked as outdated.
This comment was marked as outdated.
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
LGTM |
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.
Could you please add a note about these additions to the Doc/whatsnew/3.14.rst
?
Doc/library/turtle.rst
Outdated
Automatically begin and end filling | ||
----------------------------------- | ||
|
||
If you have Python 3.14 or later, you don't need to call :func:`begin_fill` and |
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.
We don't usually include things like "If you have Python 3.14 or later" because each set of docs relates to one specific Python version.
But perhaps it's okay here because turtle is often used by beginners who might read these docs but have 3.12 or 3.13 installed?
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.
That makes a lot of sense, but in this specific case I think it's useful. Many learners are at universities or schools with old versions of Python, and they might not be aware that there are several versions of Python. So I think it's useful to make it very clear that this is a new feature.
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.
Sorry to reopen, but IMO this reads a little bit unexpected. How about something in the lines of:
Starting with Python 3.14, you can use [...] instead of [...].
What do you think?
Lib/turtle.py
Outdated
@@ -110,27 +110,30 @@ | |||
from copy import deepcopy | |||
from tkinter import simpledialog | |||
|
|||
from contextlib import contextmanager |
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.
Why the extra newline between this and the previous import statement?
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Daniel Hollas <danekhollas@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Daniel Hollas <danekhollas@gmail.com>
We have tried to address the review comments now 🙂 |
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.
Some final minor nitpicks. Otherwise looks great!
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Marie Roald <roald.marie@gmail.com>
Co-authored-by: Marie Roald <roald.marie@gmail.com>
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.
Last one and I am good! (modulo Hugo's comment)
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
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.
I promise there really shouldn't have anything left for me. Thank you for your patience! (my reviews tend to have multiple parts depending on the hour when I do the review).
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
No worries, thanks a lot for the thorough review! |
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.
This looks good, but I have some remarks and nits. PTAL.
Doc/library/turtle.rst
Outdated
Automatically begin and end filling | ||
----------------------------------- | ||
|
||
If you have Python 3.14 or later, you don't need to call :func:`begin_fill` and |
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.
Sorry to reopen, but IMO this reads a little bit unexpected. How about something in the lines of:
Starting with Python 3.14, you can use [...] instead of [...].
What do you think?
Doc/library/turtle.rst
Outdated
... dist = 2 | ||
... for i in range(200): | ||
... fd(dist) | ||
... rt(90) | ||
... dist += 2 |
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.
This can also be written more compact. Though, I'm not sure it is more readable, so feel free to ignore this :)
... dist = 2 | |
... for i in range(200): | |
... fd(dist) | |
... rt(90) | |
... dist += 2 | |
... for dist in range(0, 400, 2): | |
... fd(dist+2) | |
... rt(90) |
Lib/turtle.py
Outdated
"""Record the vertices of a polygon. | ||
|
||
No argument. | ||
|
||
Record the vertices of a polygon. Current turtle position is first point | ||
of polygon. This function sets up a context manager that will | ||
automatically end recording once exited. |
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.
Ditto.
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
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!
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
(Conflict resolved.) @erlend-aasland Any more comments or good to merge? |
Lib/test/test_turtle.py
Outdated
s = turtle.TurtleScreen(cv=unittest.mock.MagicMock()) | ||
|
||
with s.no_animation(): | ||
assert s.tracer() == 0 |
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.
Please use the unittest
assert methods iso. the assert
keyword. Ditto for the rest of the added tests.
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.
Good catch! We have fixed that for the two tests we added in this commit. Should we also clean up the two assertions from #123617 here or should we have that in a separate PR?
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.
I'm fine with including them here.
fill
, poly
and no_animation
fill
, poly
and no_animation
Co-authored-by: Yngve Mardal Moe <yngve.m.moe@gmail.com>
patch = unittest.mock.patch('turtle._Screen.__new__', return_value=m) | ||
try: | ||
yield patch.__enter__() | ||
finally: | ||
patch.__exit__(*sys.exc_info()) |
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.
It's a bit unusual to call the context manager protocol manually, also part of the context manager protocol handling is missing here. (processing the return value of __exit__
)
Also because unittest.mock.patch is already a cmgr you can just return it
def patch_screen():
"""
Patch turtle._Screen for testing without a display.
We must patch the _Screen class itself instead of the _Screen
instance because instantiating it requires a display.
"""
return unittest.mock.patch(
"turtle._Screen.__new__",
**{
"return_value.__class__": turtle._Screen,
"return_value.mode.return_value": "standard",
},
)
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.
the mock.patch context manager handling in the tests could be improved
Adds
fill()
,poly()
andno_animation()
context managers to turtle.py.Co-authored-by: Yngve Mardal Moe 3531982+yngvem@users.noreply.github.com
fill
,poly
andtracer
#126349📚 Documentation preview 📚: https://cpython-previews--126350.org.readthedocs.build/en/126350/library/turtle.html