Skip to content

Commit 86e4ffc

Browse files
committed
refactor(core,components,examples): add annotations; tighten mypy
1 parent 6962d38 commit 86e4ffc

25 files changed

+137
-107
lines changed

examples/hello-world/app/main_page.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
import pythonnative as pn
24

35
try:
@@ -10,16 +12,16 @@
1012

1113

1214
class MainPage(pn.Page):
13-
def __init__(self, native_instance):
15+
def __init__(self, native_instance: Any) -> None:
1416
super().__init__(native_instance)
1517

16-
def on_create(self):
18+
def on_create(self) -> None:
1719
super().on_create()
1820
stack = pn.StackView().set_axis("vertical").set_spacing(12).set_alignment("fill").set_padding(all=16)
1921
stack.add_view(pn.Label("Hello from PythonNative Demo!").set_text_size(18))
2022
button = pn.Button("Go to Second Page").set_padding(vertical=10, horizontal=14)
2123

22-
def on_next():
24+
def on_next() -> None:
2325
# Visual confirmation that tap worked (iOS only)
2426
try:
2527
if UIColor is not None:
@@ -36,26 +38,26 @@ def on_next():
3638
stack.add_view(button)
3739
self.set_root_view(stack.wrap_in_scroll())
3840

39-
def on_start(self):
41+
def on_start(self) -> None:
4042
super().on_start()
4143

42-
def on_resume(self):
44+
def on_resume(self) -> None:
4345
super().on_resume()
4446

45-
def on_pause(self):
47+
def on_pause(self) -> None:
4648
super().on_pause()
4749

48-
def on_stop(self):
50+
def on_stop(self) -> None:
4951
super().on_stop()
5052

51-
def on_destroy(self):
53+
def on_destroy(self) -> None:
5254
super().on_destroy()
5355

54-
def on_restart(self):
56+
def on_restart(self) -> None:
5557
super().on_restart()
5658

57-
def on_save_instance_state(self):
59+
def on_save_instance_state(self) -> None:
5860
super().on_save_instance_state()
5961

60-
def on_restore_instance_state(self):
62+
def on_restore_instance_state(self) -> None:
6163
super().on_restore_instance_state()

examples/hello-world/app/second_page.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
import pythonnative as pn
24

35
try:
@@ -10,10 +12,10 @@
1012

1113

1214
class SecondPage(pn.Page):
13-
def __init__(self, native_instance):
15+
def __init__(self, native_instance: Any) -> None:
1416
super().__init__(native_instance)
1517

16-
def on_create(self):
18+
def on_create(self) -> None:
1719
super().on_create()
1820
stack_view = pn.StackView()
1921
# Read args passed from MainPage
@@ -30,7 +32,7 @@ def on_create(self):
3032
except Exception:
3133
pass
3234

33-
def on_next():
35+
def on_next() -> None:
3436
# Visual confirmation that tap worked (iOS only)
3537
try:
3638
if UIColor is not None:
@@ -47,26 +49,26 @@ def on_next():
4749
stack_view.add_view(back_btn)
4850
self.set_root_view(stack_view)
4951

50-
def on_start(self):
52+
def on_start(self) -> None:
5153
super().on_start()
5254

53-
def on_resume(self):
55+
def on_resume(self) -> None:
5456
super().on_resume()
5557

56-
def on_pause(self):
58+
def on_pause(self) -> None:
5759
super().on_pause()
5860

59-
def on_stop(self):
61+
def on_stop(self) -> None:
6062
super().on_stop()
6163

62-
def on_destroy(self):
64+
def on_destroy(self) -> None:
6365
super().on_destroy()
6466

65-
def on_restart(self):
67+
def on_restart(self) -> None:
6668
super().on_restart()
6769

68-
def on_save_instance_state(self):
70+
def on_save_instance_state(self) -> None:
6971
super().on_save_instance_state()
7072

71-
def on_restore_instance_state(self):
73+
def on_restore_instance_state(self) -> None:
7274
super().on_restore_instance_state()

examples/hello-world/app/third_page.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
import pythonnative as pn
24

35
try:
@@ -10,10 +12,10 @@
1012

1113

1214
class ThirdPage(pn.Page):
13-
def __init__(self, native_instance):
15+
def __init__(self, native_instance: Any) -> None:
1416
super().__init__(native_instance)
1517

16-
def on_create(self):
18+
def on_create(self) -> None:
1719
super().on_create()
1820
stack = pn.StackView()
1921
stack.add_view(pn.Label("This is the Third Page"))

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ strict_optional = False
88
pretty = True
99
files = src, tests, examples
1010
exclude = (^build/|^examples/.*/build/)
11+
disallow_untyped_defs = True
12+
disallow_incomplete_defs = True
1113

1214
[mypy-pythonnative.*]
1315
implicit_reexport = True

src/pythonnative/button.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Callable, Optional
2+
from typing import Any, Callable, Optional
33

44
from .utils import IS_ANDROID, get_android_context
55
from .view import ViewBase
@@ -15,15 +15,15 @@ def __init__(self) -> None:
1515
super().__init__()
1616

1717
@abstractmethod
18-
def set_title(self, title: str) -> None:
18+
def set_title(self, title: str) -> "ButtonBase":
1919
pass
2020

2121
@abstractmethod
2222
def get_title(self) -> str:
2323
pass
2424

2525
@abstractmethod
26-
def set_on_click(self, callback: Callable[[], None]) -> None:
26+
def set_on_click(self, callback: Callable[[], None]) -> "ButtonBase":
2727
pass
2828

2929

@@ -43,20 +43,20 @@ def __init__(self, title: str = "") -> None:
4343
self.native_instance = self.native_class(context)
4444
self.set_title(title)
4545

46-
def set_title(self, title: str):
46+
def set_title(self, title: str) -> "Button":
4747
self.native_instance.setText(title)
4848
return self
4949

5050
def get_title(self) -> str:
5151
return self.native_instance.getText().toString()
5252

53-
def set_on_click(self, callback: Callable[[], None]):
53+
def set_on_click(self, callback: Callable[[], None]) -> "Button":
5454
class OnClickListener(dynamic_proxy(jclass("android.view.View").OnClickListener)):
55-
def __init__(self, callback):
55+
def __init__(self, callback: Callable[[], None]) -> None:
5656
super().__init__()
5757
self.callback = callback
5858

59-
def onClick(self, view):
59+
def onClick(self, view: Any) -> None:
6060
self.callback()
6161

6262
listener = OnClickListener(callback)
@@ -79,7 +79,7 @@ class _PNButtonHandler(NSObject): # type: ignore[valid-type]
7979
_callback: Optional[Callable[[], None]] = None
8080

8181
@objc_method
82-
def onTap_(self, sender) -> None:
82+
def onTap_(self, sender: Any) -> None:
8383
try:
8484
callback = self._callback
8585
if callback is not None:
@@ -95,14 +95,14 @@ def __init__(self, title: str = "") -> None:
9595
self.native_instance = self.native_class.alloc().init()
9696
self.set_title(title)
9797

98-
def set_title(self, title: str):
98+
def set_title(self, title: str) -> "Button":
9999
self.native_instance.setTitle_forState_(title, 0)
100100
return self
101101

102102
def get_title(self) -> str:
103103
return self.native_instance.titleForState_(0)
104104

105-
def set_on_click(self, callback: Callable[[], None]):
105+
def set_on_click(self, callback: Callable[[], None]) -> "Button":
106106
# Create a handler object with an Objective-C method `onTap:` and attach the Python callback
107107
handler = _PNButtonHandler.new()
108108
# Keep strong references to the handler and callback

src/pythonnative/date_picker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ def get_date(self) -> tuple:
2828
# https://developer.android.com/reference/android/widget/DatePicker
2929
# ========================================
3030

31+
from typing import Any
32+
3133
from java import jclass
3234

3335
class DatePicker(DatePickerBase, ViewBase):
34-
def __init__(self, context, year: int = 0, month: int = 0, day: int = 0) -> None:
36+
def __init__(self, context: Any, year: int = 0, month: int = 0, day: int = 0) -> None:
3537
super().__init__()
3638
self.native_class = jclass("android.widget.DatePicker")
3739
self.native_instance = self.native_class(context)

src/pythonnative/label.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ def __init__(self) -> None:
1515
super().__init__()
1616

1717
@abstractmethod
18-
def set_text(self, text: str) -> None:
18+
def set_text(self, text: str) -> "LabelBase":
1919
pass
2020

2121
@abstractmethod
2222
def get_text(self) -> str:
2323
pass
2424

2525
@abstractmethod
26-
def set_text_color(self, color: Any) -> None:
26+
def set_text_color(self, color: Any) -> "LabelBase":
2727
pass
2828

2929
@abstractmethod
30-
def set_text_size(self, size: float) -> None:
30+
def set_text_size(self, size: float) -> "LabelBase":
3131
pass
3232

3333

@@ -47,14 +47,14 @@ def __init__(self, text: str = "") -> None:
4747
self.native_instance = self.native_class(context)
4848
self.set_text(text)
4949

50-
def set_text(self, text: str):
50+
def set_text(self, text: str) -> "Label":
5151
self.native_instance.setText(text)
5252
return self
5353

5454
def get_text(self) -> str:
5555
return self.native_instance.getText().toString()
5656

57-
def set_text_color(self, color: Any):
57+
def set_text_color(self, color: Any) -> "Label":
5858
# Accept int ARGB or hex string
5959
if isinstance(color, str):
6060
c = color.strip()
@@ -71,7 +71,7 @@ def set_text_color(self, color: Any):
7171
pass
7272
return self
7373

74-
def set_text_size(self, size_sp: float):
74+
def set_text_size(self, size_sp: float) -> "Label":
7575
try:
7676
self.native_instance.setTextSize(float(size_sp))
7777
except Exception:
@@ -93,14 +93,14 @@ def __init__(self, text: str = "") -> None:
9393
self.native_instance = self.native_class.alloc().init()
9494
self.set_text(text)
9595

96-
def set_text(self, text: str):
96+
def set_text(self, text: str) -> "Label":
9797
self.native_instance.setText_(text)
9898
return self
9999

100100
def get_text(self) -> str:
101101
return self.native_instance.text()
102102

103-
def set_text_color(self, color: Any):
103+
def set_text_color(self, color: Any) -> "Label":
104104
# Accept int ARGB or hex string
105105
if isinstance(color, str):
106106
c = color.strip()
@@ -123,7 +123,7 @@ def set_text_color(self, color: Any):
123123
pass
124124
return self
125125

126-
def set_text_size(self, size: float):
126+
def set_text_size(self, size: float) -> "Label":
127127
try:
128128
UIFont = ObjCClass("UIFont")
129129
font = UIFont.systemFontOfSize_(float(size))

src/pythonnative/list_view.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABC, abstractmethod
2+
from typing import Any
23

34
from .utils import IS_ANDROID
45
from .view import ViewBase
@@ -14,7 +15,7 @@ def __init__(self) -> None:
1415
super().__init__()
1516

1617
@abstractmethod
17-
def set_data(self, data: list) -> None:
18+
def set_data(self, data: list) -> "ListViewBase":
1819
pass
1920

2021
@abstractmethod
@@ -31,14 +32,14 @@ def get_data(self) -> list:
3132
from java import jclass
3233

3334
class ListView(ListViewBase, ViewBase):
34-
def __init__(self, context, data: list = []) -> None:
35+
def __init__(self, context: Any, data: list = []) -> None:
3536
super().__init__()
3637
self.context = context
3738
self.native_class = jclass("android.widget.ListView")
3839
self.native_instance = self.native_class(context)
3940
self.set_data(data)
4041

41-
def set_data(self, data: list):
42+
def set_data(self, data: list) -> "ListView":
4243
adapter = jclass("android.widget.ArrayAdapter")(
4344
self.context, jclass("android.R$layout").simple_list_item_1, data
4445
)
@@ -64,7 +65,7 @@ def __init__(self, data: list = []) -> None:
6465
self.native_instance = self.native_class.alloc().init()
6566
self.set_data(data)
6667

67-
def set_data(self, data: list):
68+
def set_data(self, data: list) -> "ListView":
6869
# Note: This is a simplified representation. Normally, you would need to create a UITableViewDataSource.
6970
self.native_instance.reloadData()
7071
return self

src/pythonnative/material_activity_indicator_view.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ def stop_animating(self) -> None:
2828
# https://developer.android.com/reference/com/google/android/material/progressindicator/CircularProgressIndicator
2929
# ========================================
3030

31+
from typing import Any
32+
3133
from java import jclass
3234

3335
class MaterialActivityIndicatorView(MaterialActivityIndicatorViewBase, ViewBase):
34-
def __init__(self, context) -> None:
36+
def __init__(self, context: Any) -> None:
3537
super().__init__()
3638
self.native_class = jclass("com.google.android.material.progressindicator.CircularProgressIndicator")
3739
self.native_instance = self.native_class(context)

src/pythonnative/material_button.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ def get_title(self) -> str:
2828
# https://developer.android.com/reference/com/google/android/material/button/MaterialButton
2929
# ========================================
3030

31+
from typing import Any
32+
3133
from java import jclass
3234

3335
class MaterialButton(MaterialButtonBase, ViewBase):
34-
def __init__(self, context, title: str = "") -> None:
36+
def __init__(self, context: Any, title: str = "") -> None:
3537
super().__init__()
3638
self.native_class = jclass("com.google.android.material.button.MaterialButton")
3739
self.native_instance = self.native_class(context)

0 commit comments

Comments
 (0)