Skip to content

Commit 52ad81d

Browse files
authored
[Monitor Ingestion] Improve samples typing (#33721)
Regenerated using the tox `generate` environment with the `autorest-post-process` setting enabled. This allows mypy to be happy with methods found in _patch.py but not _operations.py. Samples were also updated to make pyright and mypy happy in the typing checks. Signed-off-by: Paul Van Eck <[email protected]>
1 parent e5f4a1d commit 52ad81d

File tree

12 files changed

+37
-39
lines changed

12 files changed

+37
-39
lines changed

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
from ._client import LogsIngestionClient
9+
from ._patch import LogsIngestionClient
1010

11-
try:
12-
from ._patch import __all__ as _patch_all
13-
from ._patch import * # pylint: disable=unused-wildcard-import
14-
except ImportError:
15-
_patch_all = []
11+
12+
from ._patch import LogsUploadError
1613
from ._patch import patch_sdk as _patch_sdk
1714

1815
__all__ = [
16+
"LogsUploadError",
1917
"LogsIngestionClient",
2018
]
21-
__all__.extend([p for p in _patch_all if p not in __all__])
19+
2220

2321
_patch_sdk()

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_operations/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
from ._operations import LogsIngestionClientOperationsMixin
9+
from ._patch import LogsIngestionClientOperationsMixin
10+
1011

11-
from ._patch import __all__ as _patch_all
12-
from ._patch import * # pylint: disable=unused-wildcard-import
1312
from ._patch import patch_sdk as _patch_sdk
1413

1514
__all__ = [
1615
"LogsIngestionClientOperationsMixin",
1716
]
18-
__all__.extend([p for p in _patch_all if p not in __all__])
17+
1918
_patch_sdk()

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
from ._client import LogsIngestionClient
9+
from ._patch import LogsIngestionClient
10+
1011

11-
try:
12-
from ._patch import __all__ as _patch_all
13-
from ._patch import * # pylint: disable=unused-wildcard-import
14-
except ImportError:
15-
_patch_all = []
1612
from ._patch import patch_sdk as _patch_sdk
1713

1814
__all__ = [
1915
"LogsIngestionClient",
2016
]
21-
__all__.extend([p for p in _patch_all if p not in __all__])
17+
2218

2319
_patch_sdk()

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_operations/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
from ._operations import LogsIngestionClientOperationsMixin
9+
from ._patch import LogsIngestionClientOperationsMixin
10+
1011

11-
from ._patch import __all__ as _patch_all
12-
from ._patch import * # pylint: disable=unused-wildcard-import
1312
from ._patch import patch_sdk as _patch_sdk
1413

1514
__all__ = [
1615
"LogsIngestionClientOperationsMixin",
1716
]
18-
__all__.extend([p for p in _patch_all if p not in __all__])
17+
1918
_patch_sdk()
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
[tool.azure-sdk-build]
2-
type_check_samples = false
32
strict_sphinx = true
3+
4+
[tool.generate]
5+
autorest-post-process = true

sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_custom_error_callback_async.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import asyncio
3030
import os
31+
from typing import List, MutableMapping, cast
3132

3233
from azure.core.exceptions import HttpResponseError
3334
from azure.identity.aio import DefaultAzureCredential
@@ -38,18 +39,18 @@
3839
async def send_logs():
3940
endpoint = os.environ["DATA_COLLECTION_ENDPOINT"]
4041
rule_id = os.environ["LOGS_DCR_RULE_ID"]
41-
body = [
42-
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "sabhyrav-2"},
43-
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "sabhyrav"},
42+
body: List[MutableMapping[str, str]] = [
43+
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "context-2"},
44+
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "context"},
4445
]
4546
credential = DefaultAzureCredential()
4647

47-
failed_logs = []
48+
failed_logs: List[MutableMapping[str, str]] = []
4849

4950
# Sample callback that stores the logs that failed to upload.
5051
async def on_error_save(error: LogsUploadError) -> None:
5152
print("Log chunk failed to upload with error: ", error.error)
52-
failed_logs.extend(error.failed_logs)
53+
failed_logs.extend(cast(List[MutableMapping[str, str]], error.failed_logs))
5354

5455
# Sample callback that just ignores the error.
5556
async def on_error_pass(_) -> None:
@@ -58,7 +59,7 @@ async def on_error_pass(_) -> None:
5859
# Sample callback that raises the error if it corresponds to a specific HTTP error code.
5960
# This aborts the rest of the upload.
6061
async def on_error_abort(error: LogsUploadError) -> None:
61-
if isinstance(error.error, HttpResponseError) and error.error.status_code in (400, 401, 403):
62+
if isinstance(error.error, HttpResponseError) and cast(HttpResponseError, error.error).status_code in (400, 401, 403):
6263
print("Aborting upload...")
6364
raise error.error
6465

sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_send_small_logs_async.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import asyncio
3030
import os
31+
from typing import List, MutableMapping
3132

3233
from azure.core.exceptions import HttpResponseError
3334
from azure.identity.aio import DefaultAzureCredential
@@ -37,9 +38,9 @@
3738
async def send_logs():
3839
endpoint = os.environ["DATA_COLLECTION_ENDPOINT"]
3940
rule_id = os.environ["LOGS_DCR_RULE_ID"]
40-
body = [
41-
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "sabhyrav-2"},
42-
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "sabhyrav"},
41+
body: List[MutableMapping[str, str]] = [
42+
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "context-2"},
43+
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "context"},
4344
]
4445
credential = DefaultAzureCredential()
4546

sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_upload_file_contents_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def upload_file_contents() -> None:
4949
async with client:
5050
# Option 1: Upload the file contents by passing in the file stream. With this option, no chunking is done, and the
5151
# file contents are uploaded as is through one request. Subject to size service limits.
52-
with open(FILE_PATH, "r") as f:
52+
with open(FILE_PATH, "rb") as f:
5353
try:
5454
await client.upload(
5555
rule_id=os.environ["LOGS_DCR_RULE_ID"], stream_name=os.environ["LOGS_DCR_STREAM_NAME"], logs=f

sdk/monitor/azure-monitor-ingestion/samples/sample_custom_error_callback.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828

2929
import os
30+
from typing import List, MutableMapping, cast
3031

3132
from azure.core.exceptions import HttpResponseError
3233
from azure.identity import DefaultAzureCredential
@@ -39,17 +40,17 @@
3940
client = LogsIngestionClient(endpoint=endpoint, credential=credential, logging_enable=True)
4041

4142
rule_id = os.environ["LOGS_DCR_RULE_ID"]
42-
body = [
43+
body: List[MutableMapping[str, str]] = [
4344
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "context-2"},
4445
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "context"},
4546
]
4647

47-
failed_logs = []
48+
failed_logs: List[MutableMapping[str, str]] = []
4849

4950
# Sample callback that stores the logs that failed to upload.
5051
def on_error_save(error: LogsUploadError) -> None:
5152
print("Log chunk failed to upload with error: ", error.error)
52-
failed_logs.extend(error.failed_logs)
53+
failed_logs.extend(cast(List[MutableMapping[str, str]], error.failed_logs))
5354

5455

5556
# Sample callback that just ignores the error.
@@ -60,7 +61,7 @@ def on_error_pass(_) -> None:
6061
# Sample callback that raises the error if it corresponds to a specific HTTP error code.
6162
# This aborts the rest of the upload.
6263
def on_error_abort(error: LogsUploadError) -> None:
63-
if isinstance(error.error, HttpResponseError) and error.error.status_code in (400, 401, 403):
64+
if isinstance(error.error, HttpResponseError) and cast(HttpResponseError, error.error).status_code in (400, 401, 403):
6465
print("Aborting upload...")
6566
raise error.error
6667

sdk/monitor/azure-monitor-ingestion/samples/sample_send_small_logs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""
2828

2929
import os
30+
from typing import List, MutableMapping
3031

3132
from azure.core.exceptions import HttpResponseError
3233
from azure.identity import DefaultAzureCredential
@@ -39,7 +40,7 @@
3940
client = LogsIngestionClient(endpoint=endpoint, credential=credential, logging_enable=True)
4041

4142
rule_id = os.environ["LOGS_DCR_RULE_ID"]
42-
body = [
43+
body: List[MutableMapping[str, str]] = [
4344
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", "AdditionalContext": "context-2"},
4445
{"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "context"},
4546
]

0 commit comments

Comments
 (0)