Skip to content

Commit 5e3603b

Browse files
committed
fix(lib): correctly format enums on Python>=3.11
Closes #256 fix(tests): update tests and fix chore(lib): simplify fix and more tests chore(docs): document patch
1 parent d056d8d commit 5e3603b

File tree

2 files changed

+146
-21
lines changed

2 files changed

+146
-21
lines changed

manim_slides/convert.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -140,43 +140,48 @@ def __get_pydantic_core_schema__(
140140
def __str__(self) -> str:
141141
"""Ensures that the string is correctly quoted."""
142142
if self in ["true", "false", "null"]:
143-
return super().__str__()
143+
return self
144144
else:
145145
return f"'{super().__str__()}'"
146146

147147

148+
class StrEnum(Enum):
149+
def __str__(self) -> str:
150+
return str(self.value)
151+
152+
148153
Function = str # Basically, anything
149154

150155

151-
class JsTrue(str, Enum):
156+
class JsTrue(str, StrEnum):
152157
true = "true"
153158

154159

155-
class JsFalse(str, Enum):
160+
class JsFalse(str, StrEnum):
156161
false = "false"
157162

158163

159-
class JsBool(Str, Enum): # type: ignore
164+
class JsBool(Str, StrEnum): # type: ignore
160165
true = "true"
161166
false = "false"
162167

163168

164-
class JsNull(Str, Enum): # type: ignore
169+
class JsNull(Str, StrEnum): # type: ignore
165170
null = "null"
166171

167172

168-
class ControlsLayout(Str, Enum): # type: ignore
173+
class ControlsLayout(Str, StrEnum): # type: ignore
169174
edges = "edges"
170175
bottom_right = "bottom-right"
171176

172177

173-
class ControlsBackArrows(Str, Enum): # type: ignore
178+
class ControlsBackArrows(Str, StrEnum): # type: ignore
174179
faded = "faded"
175180
hidden = "hidden"
176181
visibly = "visibly"
177182

178183

179-
class SlideNumber(Str, Enum): # type: ignore
184+
class SlideNumber(Str, StrEnum): # type: ignore
180185
true = "true"
181186
false = "false"
182187
hdotv = "h.v"
@@ -185,24 +190,24 @@ class SlideNumber(Str, Enum): # type: ignore
185190
candt = "c/t"
186191

187192

188-
class ShowSlideNumber(Str, Enum): # type: ignore
193+
class ShowSlideNumber(Str, StrEnum): # type: ignore
189194
all = "all"
190195
print = "print"
191196
speaker = "speaker"
192197

193198

194-
class KeyboardCondition(Str, Enum): # type: ignore
199+
class KeyboardCondition(Str, StrEnum): # type: ignore
195200
null = "null"
196201
focused = "focused"
197202

198203

199-
class NavigationMode(Str, Enum): # type: ignore
204+
class NavigationMode(Str, StrEnum): # type: ignore
200205
default = "default"
201206
linear = "linear"
202207
grid = "grid"
203208

204209

205-
class AutoPlayMedia(Str, Enum): # type: ignore
210+
class AutoPlayMedia(Str, StrEnum): # type: ignore
206211
null = "null"
207212
true = "true"
208213
false = "false"
@@ -211,25 +216,25 @@ class AutoPlayMedia(Str, Enum): # type: ignore
211216
PreloadIframes = AutoPlayMedia
212217

213218

214-
class AutoAnimateMatcher(Str, Enum): # type: ignore
219+
class AutoAnimateMatcher(Str, StrEnum): # type: ignore
215220
null = "null"
216221

217222

218-
class AutoAnimateEasing(Str, Enum): # type: ignore
223+
class AutoAnimateEasing(Str, StrEnum): # type: ignore
219224
ease = "ease"
220225

221226

222227
AutoSlide = Union[PositiveInt, JsFalse]
223228

224229

225-
class AutoSlideMethod(Str, Enum): # type: ignore
230+
class AutoSlideMethod(Str, StrEnum): # type: ignore
226231
null = "null"
227232

228233

229234
MouseWheel = Union[JsNull, float]
230235

231236

232-
class Transition(Str, Enum): # type: ignore
237+
class Transition(Str, StrEnum): # type: ignore
233238
none = "none"
234239
fade = "fade"
235240
slide = "slide"
@@ -238,13 +243,13 @@ class Transition(Str, Enum): # type: ignore
238243
zoom = "zoom"
239244

240245

241-
class TransitionSpeed(Str, Enum): # type: ignore
246+
class TransitionSpeed(Str, StrEnum): # type: ignore
242247
default = "default"
243248
fast = "fast"
244249
slow = "slow"
245250

246251

247-
class BackgroundSize(Str, Enum): # type: ignore
252+
class BackgroundSize(Str, StrEnum): # type: ignore
248253
# From: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
249254
# TODO: support more background size
250255
contain = "contain"
@@ -254,11 +259,11 @@ class BackgroundSize(Str, Enum): # type: ignore
254259
BackgroundTransition = Transition
255260

256261

257-
class Display(Str, Enum): # type: ignore
262+
class Display(Str, StrEnum): # type: ignore
258263
block = "block"
259264

260265

261-
class RevealTheme(str, Enum):
266+
class RevealTheme(str, StrEnum):
262267
black = "black"
263268
white = "white"
264269
league = "league"

tests/test_convert.py

+121-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,126 @@
1+
from enum import EnumMeta
2+
13
import pytest
24

3-
from manim_slides.convert import PDF, Converter, PowerPoint, RevealJS
5+
from manim_slides.convert import (
6+
PDF,
7+
AutoAnimateEasing,
8+
AutoAnimateMatcher,
9+
AutoPlayMedia,
10+
AutoSlideMethod,
11+
BackgroundSize,
12+
BackgroundTransition,
13+
ControlsBackArrows,
14+
ControlsLayout,
15+
Converter,
16+
Display,
17+
JsBool,
18+
JsFalse,
19+
JsNull,
20+
JsTrue,
21+
KeyboardCondition,
22+
NavigationMode,
23+
PowerPoint,
24+
PreloadIframes,
25+
RevealJS,
26+
RevealTheme,
27+
ShowSlideNumber,
28+
SlideNumber,
29+
Transition,
30+
TransitionSpeed,
31+
)
32+
33+
34+
@pytest.mark.parametrize(
35+
("enum_type",),
36+
[
37+
(JsTrue,),
38+
(JsFalse,),
39+
(JsBool,),
40+
(JsNull,),
41+
(ControlsLayout,),
42+
(ControlsBackArrows,),
43+
(SlideNumber,),
44+
(ShowSlideNumber,),
45+
(KeyboardCondition,),
46+
(NavigationMode,),
47+
(AutoPlayMedia,),
48+
(PreloadIframes,),
49+
(AutoAnimateMatcher,),
50+
(AutoAnimateEasing,),
51+
(AutoSlideMethod,),
52+
(Transition,),
53+
(TransitionSpeed,),
54+
(BackgroundSize,),
55+
(BackgroundTransition,),
56+
(Display,),
57+
(RevealTheme,),
58+
],
59+
)
60+
def test_format_enum(enum_type: EnumMeta) -> None:
61+
for enum in enum_type: # type: ignore[var-annotated]
62+
expected = str(enum)
63+
got = f"{enum}"
64+
65+
assert expected == got
66+
67+
got = "{enum}".format(enum=enum)
68+
69+
assert expected == got
70+
71+
got = format(enum, "")
72+
73+
assert expected == got
74+
75+
76+
@pytest.mark.parametrize(
77+
("enum_type",),
78+
[
79+
(ControlsLayout,),
80+
(ControlsBackArrows,),
81+
(SlideNumber,),
82+
(ShowSlideNumber,),
83+
(KeyboardCondition,),
84+
(NavigationMode,),
85+
(AutoPlayMedia,),
86+
(PreloadIframes,),
87+
(AutoAnimateMatcher,),
88+
(AutoAnimateEasing,),
89+
(AutoSlideMethod,),
90+
(Transition,),
91+
(TransitionSpeed,),
92+
(BackgroundSize,),
93+
(BackgroundTransition,),
94+
(Display,),
95+
],
96+
)
97+
def test_quoted_enum(enum_type: EnumMeta) -> None:
98+
for enum in enum_type: # type: ignore[var-annotated]
99+
if enum in ["true", "false", "null"]:
100+
continue
101+
102+
expected = "'" + enum.value + "'"
103+
got = str(enum)
104+
105+
assert expected == got
106+
107+
108+
@pytest.mark.parametrize(
109+
("enum_type",),
110+
[
111+
(JsTrue,),
112+
(JsFalse,),
113+
(JsBool,),
114+
(JsNull,),
115+
(RevealTheme,),
116+
],
117+
)
118+
def test_unquoted_enum(enum_type: EnumMeta) -> None:
119+
for enum in enum_type: # type: ignore[var-annotated]
120+
expected = enum.value
121+
got = str(enum)
122+
123+
assert expected == got
4124

5125

6126
class TestConverter:

0 commit comments

Comments
 (0)