Skip to content

Use a TypedDict for turtle._PenState #13152

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 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions stdlib/turtle.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from collections.abc import Callable, Sequence
from tkinter import Canvas, Frame, Misc, PhotoImage, Scrollbar
from typing import Any, ClassVar, overload
from typing import Any, ClassVar, Literal, TypedDict, overload
from typing_extensions import Self, TypeAlias

__all__ = [
Expand Down Expand Up @@ -141,8 +141,18 @@ if sys.version_info < (3, 13):
_Color: TypeAlias = str | tuple[float, float, float]
_AnyColor: TypeAlias = Any

# TODO: Replace this with a TypedDict once it becomes standardized.
_PenState: TypeAlias = dict[str, Any]
class _PenState(TypedDict):
shown: bool
pendown: bool
pencolor: _Color
fillcolor: _Color
Comment on lines +147 to +148
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are always returned as strings. For example:

>>> import turtle
>>> turtle.pen(pencolor=(1,0,0))
>>> turtle.pen()
{'shown': True, 'pendown': True, 'pencolor': '#ff0000', ...

However, using str here is not any better, because it is not wrong to have tuples in pen state dicts:

>>> p = turtle.pen().copy()
>>> p['pencolor'] = (0,1,0)
>>> turtle.pen(p)
>>> turtle.pen()['pencolor']
'#00ff00'

pensize: int
speed: int
resizemode: Literal["auto", "user", "noresize"]
stretchfactor: tuple[float, float]
shearfactor: float
outline: int
tilt: float

_Speed: TypeAlias = str | float
_PolygonCoords: TypeAlias = Sequence[tuple[float, float]]
Expand Down