Skip to content

Commit f53023a

Browse files
committed
refactor: type-hint poll_for_status with TeamsAsyncOperationStatus, Callable, and Self
1 parent e2bbb10 commit f53023a

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

office365/teams/operations/async_operation.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
from __future__ import annotations
2+
13
import time
4+
from collections.abc import Callable
25
from datetime import datetime
36
from typing import Optional
47

8+
from typing_extensions import Self
9+
510
from office365.entity import Entity
611
from office365.runtime.types.odata_property import odata
12+
from office365.teams.operations.async_status import TeamsAsyncOperationStatus
713
from office365.teams.operations.error import OperationError
814
from office365.teams.operations.type import TeamsAsyncOperationType
915

@@ -23,30 +29,30 @@ class TeamsAsyncOperation(Entity):
2329

2430
def poll_for_status(
2531
self,
26-
status_type="succeeded",
27-
max_polling_count=5,
28-
polling_interval_secs=15,
29-
success_callback=None,
30-
failure_callback=None,
31-
):
32+
status_type: TeamsAsyncOperationStatus = TeamsAsyncOperationStatus.succeeded,
33+
max_polling_count: int = 5,
34+
polling_interval_secs: int = 15,
35+
success_callback: Callable[[TeamsAsyncOperation], None] | None = None,
36+
failure_callback: Callable[[TeamsAsyncOperation], None] | None = None,
37+
) -> Self:
3238
"""Poll to check for completion of an async Teams create call
3339
3440
Args:
3541
polling_interval_secs (int):
3642
max_polling_count (int):
37-
status_type (str): The status of a teamsAsyncOperation
38-
success_callback ((TeamsAsyncOperation)-> None): A callback to call if the request executes successfully.
39-
failure_callback ((TeamsAsyncOperation)-> None): A callback to call if the request fails to execute
43+
status_type (TeamsAsyncOperationStatus): The status to wait for
44+
success_callback ((TeamsAsyncOperation)-> None): Called on success with the operation
45+
failure_callback ((TeamsAsyncOperation)-> None): Called on timeout
4046
"""
4147

4248
def _poll_for_status(polling_number: int) -> None:
4349
if polling_number > max_polling_count:
4450
if callable(failure_callback):
4551
failure_callback(self)
4652
else:
47-
raise TypeError("The maximum polling count has been reached")
53+
raise TimeoutError("The maximum polling count has been reached")
4854

49-
def _verify_status(return_type):
55+
def _verify_status(return_type: TeamsAsyncOperation):
5056
if return_type.status != status_type:
5157
time.sleep(polling_interval_secs)
5258
_poll_for_status(polling_number + 1)
@@ -66,31 +72,31 @@ def attempts_count(self) -> Optional[int]:
6672

6773
@odata(name="createdDateTime")
6874
@property
69-
def created_date_time(self) -> Optional[datetime]:
75+
def created_date_time(self) -> datetime:
7076
"""Date and time when the operation was created."""
71-
return self.properties.get("createdDateTime", None)
77+
return self.properties.get("createdDateTime", datetime.min)
7278

7379
@property
74-
def error(self) -> Optional[OperationError]:
80+
def error(self) -> OperationError:
7581
"""Error information if the operation failed."""
76-
return self.properties.get("error", None)
82+
return self.properties.get("error", OperationError())
7783

7884
@odata(name="lastActionDateTime")
7985
@property
80-
def last_action_date_time(self) -> Optional[datetime]:
86+
def last_action_date_time(self) -> datetime:
8187
"""Date and time when the operation was last updated."""
82-
return self.properties.get("lastActionDateTime", None)
88+
return self.properties.get("lastActionDateTime", datetime.min)
8389

8490
@odata(name="operationType")
8591
@property
86-
def operation_type(self) -> Optional[TeamsAsyncOperationType]:
92+
def operation_type(self) -> TeamsAsyncOperationType:
8793
"""The type of the operation."""
88-
return self.properties.get("operationType", None)
94+
return self.properties.get("operationType", TeamsAsyncOperationType.unknown)
8995

9096
@property
91-
def status(self) -> Optional[str]:
97+
def status(self) -> TeamsAsyncOperationStatus:
9298
"""Operation status."""
93-
return self.properties.get("status", None)
99+
return self.properties.get("status", TeamsAsyncOperationStatus.invalid)
94100

95101
@property
96102
def target_resource_id(self) -> Optional[str]:

office365/teams/operations/type.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
class TeamsAsyncOperationType(Enum):
55
"""The type of long-running operation for a team."""
66

7+
unknown = "unknown"
8+
79
invalid = "invalid"
810
cloneTeam = "cloneTeam"
911
archiveTeam = "archiveTeam"

office365/teams/team.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from office365.teams.members.settings import TeamMemberSettings
2525
from office365.teams.messaging_settings import TeamMessagingSettings
2626
from office365.teams.operations.async_operation import TeamsAsyncOperation
27+
from office365.teams.operations.async_status import TeamsAsyncOperationStatus
2728
from office365.teams.schedule.schedule import Schedule
2829
from office365.teams.specialization import TeamSpecialization
2930
from office365.teams.summary import TeamSummary
@@ -53,7 +54,7 @@ def execute_query_and_wait(self) -> Self:
5354
def _loaded():
5455
if self._pending_operation is None:
5556
raise RuntimeError("No pending async operation to wait for. Call clone() or create() first.")
56-
self._pending_operation.poll_for_status(status_type="succeeded")
57+
self._pending_operation.poll_for_status(status_type=TeamsAsyncOperationStatus.succeeded)
5758

5859
self.ensure_property("id").after_execute(lambda _: _loaded())
5960
self.execute_query()

0 commit comments

Comments
 (0)