Skip to content

Commit 64f7191

Browse files
ndonkoHenriFeodorFitsnerCopilot
authored
Replace more assertions with if-raise conditions (#5721)
* initial commit * Replace assertions with appropriate exceptions for better error handling. * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * improve docstrings --------- Co-authored-by: Feodor Fitsner <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 5eda17d commit 64f7191

File tree

58 files changed

+503
-463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+503
-463
lines changed

packages/flet/lib/src/utils/drawing.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ List<double>? parsePaintStrokeDashPattern(dynamic value,
4040
[List<double>? defaultValue]) {
4141
if (value == null) return defaultValue;
4242

43-
return value["stroke_dash_pattern"] != null
44-
? (value["stroke_dash_pattern"] as List)
45-
.map((e) => parseDouble(e))
43+
return (value["stroke_dash_pattern"] as List?)
44+
?.map((e) => parseDouble(e))
4645
.nonNulls
47-
.toList()
48-
: null;
46+
.toList() ??
47+
defaultValue;
4948
}
5049

5150
ui.Gradient? parsePaintGradient(Map<dynamic, dynamic>? value, ThemeData? theme,

sdk/python/packages/flet-ads/src/flet_ads/banner_ad.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class BannerAd(BaseAd):
1111
Displays a banner ad.
1212
1313
Raises:
14-
AssertionError: When this control is used on a web and/or non-mobile platform.
14+
FletUnsupportedPlatformException: When this control is used on a web
15+
and/or non-mobile platform.
1516
"""
1617

1718
on_will_dismiss: Optional[ft.ControlEventHandler["BannerAd"]] = None

sdk/python/packages/flet-ads/src/flet_ads/base_ad.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class BaseAd(ft.Control):
1111
Base class for all ad controls in Flet Ads package.
1212
1313
Raises:
14-
AssertionError: When using this control on a web and/or non-mobile platform.
14+
FletUnsupportedPlatformException: When using this control on a web
15+
and/or non-mobile platform.
1516
"""
1617

1718
unit_id: str
@@ -63,6 +64,8 @@ class BaseAd(ft.Control):
6364
"""
6465

6566
def before_update(self):
66-
assert not self.page.web and self.page.platform.is_mobile(), (
67-
f"{self.__class__.__name__} is only supported on Mobile (Android and iOS)"
68-
)
67+
if self.page.web or not self.page.platform.is_mobile():
68+
raise ft.FletUnsupportedPlatformException(
69+
f"{self.__class__.__name__} is only supported on "
70+
f"Mobile (Android and iOS)"
71+
)

sdk/python/packages/flet-ads/src/flet_ads/interstitial_ad.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class InterstitialAd(BaseAd):
88
Displays a full-screen interstitial ad.
99
1010
Raises:
11-
AssertionError: When using this control on a web and/or non-mobile platform.
11+
FletUnsupportedPlatformException: When using this control on a
12+
web and/or non-mobile platform.
1213
"""
1314

1415
async def show(self):

sdk/python/packages/flet-ads/src/flet_ads/native_ad.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@
99
class NativeAd(BannerAd):
1010
"""
1111
Renders a native ad.
12-
13-
Raises:
14-
AssertionError: When neither [`factory_id`][(c).] nor
15-
[`template_style`][(c).] is set.
1612
"""
1713

1814
factory_id: str = None
1915
"""
2016
An identifier for the factory that creates the Platform view.
17+
18+
Raises:
19+
ValueError: When neither `factory_id` nor [`template_style`][(c).] is set.
2120
"""
2221

2322
template_style: NativeAdTemplateStyle = None
2423
"""
2524
A style for the native ad template.
25+
26+
Raises:
27+
ValueError: When neither [`factory_id`][(c).] nor `template_style` is set.
2628
"""
2729

2830
def before_update(self):
2931
super().before_update()
30-
assert self.factory_id is not None or self.template_style is not None, (
31-
"factory_id or template_style must be set"
32-
)
32+
if self.factory_id is None and self.template_style is None:
33+
raise ValueError("factory_id or template_style must be set")

sdk/python/packages/flet-audio-recorder/src/flet_audio_recorder/audio_recorder.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ async def start_recording(
5454
5555
Returns:
5656
`True` if recording was successfully started, `False` otherwise.
57+
58+
Raises:
59+
ValueError: If `output_path` is not provided on platforms other than web.
5760
"""
58-
assert self.page.web or output_path, (
59-
"output_path must be provided on platforms other than web"
60-
)
61+
if not (self.page.web or output_path):
62+
raise ValueError("output_path must be provided on platforms other than web")
6163
return await self._invoke_method(
6264
method_name="start_recording",
6365
arguments={

sdk/python/packages/flet-audio/src/flet_audio/audio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Audio(ft.Service):
2727
is a list of supported audio formats.
2828
2929
Raises:
30-
AssertionError: If both [`src`][(c).] and [`src_base64`][(c).] are `None`.
30+
ValueError: If both `src` and [`src_base64`][(c).] are `None`.
3131
"""
3232

3333
src_base64: Optional[str] = None
@@ -41,7 +41,7 @@ class Audio(ft.Service):
4141
is a list of supported audio formats.
4242
4343
Raises:
44-
AssertionError: If both [`src`][(c).] and [`src_base64`][(c).] are `None`.
44+
ValueError: If both `src_base64` and [`src`][(c).] are `None`.
4545
"""
4646

4747
autoplay: bool = False
@@ -118,7 +118,8 @@ class Audio(ft.Service):
118118

119119
def before_update(self):
120120
super().before_update()
121-
assert self.src or self.src_base64, "either src or src_base64 must be provided"
121+
if not (self.src or self.src_base64):
122+
raise ValueError("either src or src_base64 must be provided")
122123

123124
async def play(self, position: ft.DurationValue = 0):
124125
"""

sdk/python/packages/flet-charts/src/flet_charts/bar_chart.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ class BarChart(ft.LayoutControl):
175175

176176
group_spacing: ft.Number = 16.0
177177
"""
178-
A amount of space between bar [`groups`][..].
178+
An amount of space between bar [`groups`][(c).].
179179
"""
180180

181181
group_alignment: ft.MainAxisAlignment = ft.MainAxisAlignment.SPACE_EVENLY
182182
"""
183-
The alignment of the bar [`groups`][..] within this chart.
183+
The alignment of the bar [`groups`][(c).] within this chart.
184184
185185
If set to [`MainAxisAlignment.CENTER`][flet.MainAxisAlignment.CENTER],
186-
the space between the `groups` can be specified using [`group_spacing`][..].
186+
the space between the `groups` can be specified using [`group_spacing`][(c).].
187187
"""
188188

189189
animation: ft.AnimationValue = field(

sdk/python/packages/flet-charts/src/flet_charts/chart_axis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ChartAxisLabel(ft.BaseControl):
1919

2020
label: Optional[ft.StrOrControl] = None
2121
"""
22-
The label to display for the specified [`value`][..].
22+
The label to display for the specified [`value`][(c).].
2323
"""
2424

2525

@@ -41,7 +41,7 @@ class ChartAxis(ft.BaseControl):
4141

4242
show_labels: bool = True
4343
"""
44-
Whether to display the [`labels`][..] along the axis.
44+
Whether to display the [`labels`][(c).] along the axis.
4545
If `labels` is empty then automatic labels are displayed.
4646
"""
4747

@@ -61,7 +61,7 @@ class ChartAxis(ft.BaseControl):
6161

6262
label_size: ft.Number = 22
6363
"""
64-
The maximum space for each label in [`labels`][..].
64+
The maximum space for each label in [`labels`][(c).].
6565
6666
Each label will stretch to fit this space.
6767
"""

sdk/python/packages/flet-charts/src/flet_charts/line_chart_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class LineChartData(ft.BaseControl):
4848

4949
prevent_curve_over_shooting_threshold: ft.Number = 10.0
5050
"""
51-
Threshold for [`prevent_curve_over_shooting`][..] algorithm.
51+
Threshold for [`prevent_curve_over_shooting`][(c).] algorithm.
5252
"""
5353

5454
dash_pattern: Optional[list[int]] = None
@@ -131,7 +131,7 @@ class LineChartData(ft.BaseControl):
131131
curve_smoothness: ft.Number = 0.35
132132
"""
133133
Defines the smoothness of a curve line,
134-
when [`curved`][..] is set to `True`.
134+
when [`curved`][(c).] is set to `True`.
135135
"""
136136

137137
rounded_stroke_join: bool = False

0 commit comments

Comments
 (0)