Skip to content

Commit 56b6665

Browse files
Fix mypy errors: Use Optional types with assertions
The attributes are initialized to None in child __init__, then set to actual values by initialize() method. Using Optional types with assertions at usage points correctly reflects this lifecycle and satisfies mypy Co-authored-by: mitsha-microsoft <58204159+mitsha-microsoft@users.noreply.github.com>
1 parent 83ca311 commit 56b6665

File tree

2 files changed

+22
-8
lines changed
  • sdk/loadtesting/azure-developer-loadtesting/azure/developer/loadtesting

2 files changed

+22
-8
lines changed

sdk/loadtesting/azure-developer-loadtesting/azure/developer/loadtesting/_operations/_patch.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
class LoadTestingPollingMethod(PollingMethod):
2828
"""Base class for custom sync polling methods."""
2929

30-
_status: str
30+
_status: Optional[str]
3131
_termination_statuses: List[str]
3232
_polling_interval: int
33-
_resource: JSON
34-
_command: Any
35-
_initial_response: JSON
33+
_resource: Optional[JSON]
34+
_command: Optional[Any]
35+
_initial_response: Optional[JSON]
3636

3737
def _update_status(self) -> None:
3838
raise NotImplementedError("This method needs to be implemented")
3939

4040
def _update_resource(self) -> None:
41+
assert self._command is not None
4142
self._resource = self._command()
4243

4344
def initialize(self, client, initial_response, deserialization_callback) -> None:
@@ -46,12 +47,15 @@ def initialize(self, client, initial_response, deserialization_callback) -> None
4647
self._resource = initial_response
4748

4849
def status(self) -> str:
50+
assert self._status is not None
4951
return self._status
5052

5153
def finished(self) -> bool:
54+
assert self._status is not None
5255
return self._status in self._termination_statuses
5356

5457
def resource(self) -> JSON:
58+
assert self._resource is not None
5559
return self._resource
5660

5761
def run(self) -> None:
@@ -84,6 +88,7 @@ def __init__(self, interval=5) -> None:
8488
]
8589

8690
def _update_status(self) -> None:
91+
assert self._resource is not None
8792
self._status = self._resource["validationStatus"]
8893

8994

@@ -99,6 +104,7 @@ def __init__(self, interval=5) -> None:
99104
self._termination_statuses = ["DONE", "FAILED", "CANCELLED"]
100105

101106
def _update_status(self) -> None:
107+
assert self._resource is not None
102108
self._status = self._resource["status"]
103109

104110

@@ -114,6 +120,7 @@ def __init__(self, interval=5) -> None:
114120
self._termination_statuses = ["DONE", "FAILED", "CANCELLED"]
115121

116122
def _update_status(self):
123+
assert self._resource is not None
117124
self._status = self._resource["status"]
118125

119126

sdk/loadtesting/azure-developer-loadtesting/azure/developer/loadtesting/aio/_operations/_patch.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
class AsyncLoadTestingPollingMethod(AsyncPollingMethod):
2929
"""Base class for custom async polling methods."""
3030

31-
_status: str
31+
_status: Optional[str]
3232
_termination_statuses: List[str]
3333
_polling_interval: int
34-
_resource: JSON
35-
_command: Any
36-
_initial_response: JSON
34+
_resource: Optional[JSON]
35+
_command: Optional[Any]
36+
_initial_response: Optional[JSON]
3737

3838
def _update_status(self) -> None:
3939
raise NotImplementedError("This method needs to be implemented")
4040

4141
async def _update_resource(self) -> None:
42+
assert self._command is not None
4243
self._resource = await self._command()
4344

4445
def initialize(self, client, initial_response, deserialization_callback) -> None:
@@ -47,12 +48,15 @@ def initialize(self, client, initial_response, deserialization_callback) -> None
4748
self._resource = initial_response
4849

4950
def status(self) -> str:
51+
assert self._status is not None
5052
return self._status
5153

5254
def finished(self) -> bool:
55+
assert self._status is not None
5356
return self._status in self._termination_statuses
5457

5558
def resource(self) -> JSON:
59+
assert self._resource is not None
5660
return self._resource
5761

5862
async def run(self) -> None:
@@ -83,6 +87,7 @@ def __init__(self, interval=5) -> None:
8387
]
8488

8589
def _update_status(self) -> None:
90+
assert self._resource is not None
8691
self._status = self._resource["validationStatus"]
8792

8893

@@ -96,6 +101,7 @@ def __init__(self, interval=5) -> None:
96101
self._termination_statuses = ["DONE", "FAILED", "CANCELLED"]
97102

98103
def _update_status(self) -> None:
104+
assert self._resource is not None
99105
self._status = self._resource["status"]
100106

101107

@@ -109,6 +115,7 @@ def __init__(self, interval=5) -> None:
109115
self._termination_statuses = ["DONE", "FAILED", "CANCELLED"]
110116

111117
def _update_status(self) -> None:
118+
assert self._resource is not None
112119
self._status = self._resource["status"]
113120

114121

0 commit comments

Comments
 (0)