Skip to content

Commit 00ee7d2

Browse files
committed
refactor: update exception classes to use AttributeError and improve error messages
1 parent 60aa624 commit 00ee7d2

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/tppt/exception.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,22 @@ def message(self) -> str:
5454
return f"The slide master attribute must be a slide layout. The {self.slide_layout_name} layout is not a slide layout."
5555

5656

57-
class SlideMasterDoesNotHaveAttributesError(TpptException, ValueError):
57+
class SlideMasterDoesNotHaveAttributesError(TpptException, AttributeError):
5858
"""Slide master does not have an attribute."""
5959

60-
def __init__(self, slide_layout: "type") -> None:
61-
self.slide_layout_name = slide_layout.__name__
62-
6360
@property
6461
def message(self) -> str:
65-
return f"The slide master does not have an attribute for the {self.slide_layout_name} layout"
62+
return "The slide master does not have an attributes"
6663

6764

68-
class SlideMasterAttributeNotFoundError(TpptException, ValueError):
65+
class SlideMasterAttributeNotFoundError(TpptException, AttributeError):
6966
"""Slide master attribute not found."""
7067

71-
def __init__(self, slide_layout_name: str) -> None:
72-
self.slide_layout_name = slide_layout_name
68+
def __init__(self, attribute_name: str) -> None:
69+
self.attribute_name = attribute_name
7370

7471
@property
7572
def message(self) -> str:
76-
return f"The slide master does not have an attribute for the {self.slide_layout_name} layout"
73+
return (
74+
f"The slide master does not have an attribute of the {self.attribute_name}"
75+
)

src/tppt/slide_master.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
DefaultTitleSlide,
2222
DefaultTwoContentSlide,
2323
DefaultVerticalTitleAndTextSlide,
24-
Placeholder,
2524
SlideLayout,
2625
)
2726

@@ -57,10 +56,14 @@ def __getattr__(self, key: str) -> "type[SlideLayout]":
5756
return annotation
5857
else:
5958
return annotation
59+
elif (attributes := getattr(self, "__annotations__", None)) and hasattr(
60+
attributes, key
61+
):
62+
return getattr(attributes, key)
6063
else:
6164
raise SlideMasterAttributeNotFoundError(key)
6265
else:
63-
raise SlideMasterDoesNotHaveAttributesError(self)
66+
raise SlideMasterDoesNotHaveAttributesError()
6467

6568

6669
@dataclass_transform(
@@ -82,9 +85,6 @@ def __class_getitem__(cls, item: AnyType) -> AnyType:
8285

8386

8487
class DefaultSlideMaster(SlideMaster):
85-
title: Placeholder[str]
86-
text: Placeholder[str]
87-
8888
Title: Layout[DefaultTitleSlide]
8989
TitleAndContent: Layout[DefaultTitleAndContentSlide]
9090
SectionHeader: Layout[DefaultSectionHeaderSlide]

tests/test_slide_master.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,31 @@
1010
DefaultTitleSlide,
1111
DefaultTwoContentSlide,
1212
DefaultVerticalTitleAndTextSlide,
13+
Placeholder,
1314
)
14-
from tppt.slide_master import DefaultSlideMaster, get_layouts
15+
from tppt.slide_master import DefaultSlideMaster, Layout, get_layouts
16+
17+
18+
class TestSlideMaster(DefaultSlideMaster):
19+
title: Placeholder[str]
20+
content: Placeholder[str]
21+
22+
Title: Layout[DefaultTitleSlide]
23+
TitleAndContent: Layout[DefaultTitleAndContentSlide]
24+
SectionHeader: Layout[DefaultSectionHeaderSlide]
25+
TwoContent: Layout[DefaultTwoContentSlide]
26+
Comparison: Layout[DefaultComparisonSlide]
27+
TitleOnly: Layout[DefaultTitleOnlySlide]
28+
Blank: Layout[DefaultBlankSlide]
29+
ContentWithCaption: Layout[DefaultContentWithCaptionSlide]
30+
PictureWithCaption: Layout[DefaultPictureWithCaptionSlide]
31+
TitleAndVerticalText: Layout[DefaultTitleAndVerticalTextSlide]
32+
VerticalTitleAndText: Layout[DefaultVerticalTitleAndTextSlide]
1533

1634

1735
def test_get_layouts():
1836
"""Test that get_layouts function correctly retrieves the list of Layouts"""
19-
layouts = get_layouts(DefaultSlideMaster)
37+
layouts = get_layouts(TestSlideMaster)
2038

2139
# Verify that all Layouts are included
2240
expected_layouts = [

0 commit comments

Comments
 (0)