Skip to content

Commit d24fa0b

Browse files
gnufedebrettlangdon
authored andcommitted
chore: explicit optionals everywhere (#14535)
Code style: This PR changes implicit optionals to explicit ones (in type annotations) Read more here: https://github.com/hauntsaninja/no_implicit_optional?tab=readme-ov-file#whats-going-on ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 9571f6d commit d24fa0b

File tree

13 files changed

+34
-23
lines changed

13 files changed

+34
-23
lines changed

ddtrace/appsec/_ddwaf/ddwaf_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ddwaf_object(ctypes.Structure):
105105

106106
def __init__(
107107
self,
108-
struct: DDWafRulesType = None,
108+
struct: Optional[DDWafRulesType] = None,
109109
observator: _observator = _observator(), # noqa : B008
110110
max_objects: int = DDWAF_MAX_CONTAINER_SIZE,
111111
max_depth: int = DDWAF_MAX_CONTAINER_DEPTH,

ddtrace/appsec/_ddwaf/waf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def run(
172172
self,
173173
ctx: ddwaf_context_capsule,
174174
data: DDWafRulesType,
175-
ephemeral_data: DDWafRulesType = None,
175+
ephemeral_data: Optional[DDWafRulesType] = None,
176176
timeout_ms: float = DEFAULT.WAF_TIMEOUT,
177177
) -> DDWaf_result:
178178
start = time.monotonic()

ddtrace/appsec/_ddwaf/waf_mock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22
from typing import Dict
3+
from typing import Optional
34
from typing import Sequence
45
from typing import Tuple
56

@@ -41,7 +42,7 @@ def run(
4142
self,
4243
ctx: ddwaf_context_capsule,
4344
data: DDWafRulesType,
44-
ephemeral_data: DDWafRulesType = None,
45+
ephemeral_data: Optional[DDWafRulesType] = None,
4546
timeout_ms: float = DEFAULT.WAF_TIMEOUT,
4647
) -> DDWaf_result:
4748
LOGGER.debug("DDWaf features disabled. dry run")

ddtrace/appsec/_ddwaf/waf_stubs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def run(
110110
self,
111111
ctx: ddwaf_context_capsule,
112112
data: DDWafRulesType,
113-
ephemeral_data: DDWafRulesType = None,
113+
ephemeral_data: Optional[DDWafRulesType] = None,
114114
timeout_ms: float = DEFAULT.WAF_TIMEOUT,
115115
) -> DDWaf_result:
116116
pass

ddtrace/contrib/internal/unittest/patch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import os
33
from typing import Dict
4+
from typing import Optional
45
from typing import Union
56
import unittest
67

@@ -464,7 +465,7 @@ def unpatch():
464465
_CIVisibility.disable()
465466

466467

467-
def _set_test_span_status(test_item, status: str, exc_info: str = None, skip_reason: str = None):
468+
def _set_test_span_status(test_item, status: str, exc_info: Optional[str] = None, skip_reason: Optional[str] = None):
468469
span = _extract_span(test_item)
469470
if not span:
470471
log.debug("Tried setting test result for test but could not find span for %s", test_item)

ddtrace/internal/telemetry/metrics_namespaces.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ class MetricNamespace:
2020
namespace: TELEMETRY_NAMESPACE,
2121
name: str,
2222
value: float = 1.0,
23-
tags: MetricTagType = None,
23+
tags: Optional[MetricTagType] = None,
2424
) -> None: ...

ddtrace/internal/telemetry/writer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,9 @@ def _format_file_path(self, filename: str) -> str:
560560
except ValueError:
561561
return filename
562562

563-
def add_gauge_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: MetricTagType = None):
563+
def add_gauge_metric(
564+
self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: Optional[MetricTagType] = None
565+
):
564566
"""
565567
Queues gauge metric
566568
"""
@@ -573,7 +575,9 @@ def add_gauge_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: flo
573575
tags,
574576
)
575577

576-
def add_rate_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: MetricTagType = None):
578+
def add_rate_metric(
579+
self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: Optional[MetricTagType] = None
580+
):
577581
"""
578582
Queues rate metric
579583
"""
@@ -586,7 +590,9 @@ def add_rate_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: floa
586590
tags,
587591
)
588592

589-
def add_count_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: int = 1, tags: MetricTagType = None):
593+
def add_count_metric(
594+
self, namespace: TELEMETRY_NAMESPACE, name: str, value: int = 1, tags: Optional[MetricTagType] = None
595+
):
590596
"""
591597
Queues count metric
592598
"""
@@ -600,7 +606,7 @@ def add_count_metric(self, namespace: TELEMETRY_NAMESPACE, name: str, value: int
600606
)
601607

602608
def add_distribution_metric(
603-
self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: MetricTagType = None
609+
self, namespace: TELEMETRY_NAMESPACE, name: str, value: float, tags: Optional[MetricTagType] = None
604610
):
605611
"""
606612
Queues distributions metric

mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ scripts_are_modules = true
77
show_error_codes = true
88
warn_unused_ignores = true
99
warn_unused_configs = true
10-
no_implicit_optional = true
1110
ignore_missing_imports = true
1211
namespace_packages = true
1312
plugins = envier.mypy

tests/appsec/integrations/fastapi_tests/test_fastapi_appsec_iast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ async def test_route(request: Request):
283283
@pytest.mark.skipif(fastapi_version < (0, 95, 0), reason="Header annotation doesn't work on fastapi 94 or lower")
284284
def test_header_value_source_typing_param(fastapi_application, client, tracer, test_spans):
285285
@fastapi_application.get("/index.html")
286-
async def test_route(iast_header: typing.Annotated[str, Header()] = None):
286+
async def test_route(iast_header: typing.Annotated[str, Header()]):
287287
from ddtrace.appsec._iast._taint_tracking import origin_to_str
288288
from ddtrace.appsec._iast._taint_tracking._taint_objects_base import get_tainted_ranges
289289

tests/ci_visibility/api_client/_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _get_test_client(
176176
self,
177177
itr_skipping_level: ITR_SKIPPING_LEVEL = ITR_SKIPPING_LEVEL.TEST,
178178
requests_mode: REQUESTS_MODE = _AGENTLESS,
179-
git_data: GitData = None,
179+
git_data: t.Optional[GitData] = None,
180180
api_key: t.Optional[str] = "my_api_key",
181181
dd_site: t.Optional[str] = None,
182182
agentless_url: t.Optional[str] = None,
@@ -215,7 +215,7 @@ def _get_test_client(
215215
def _get_expected_do_request_setting_payload(
216216
self,
217217
itr_skipping_level: ITR_SKIPPING_LEVEL = ITR_SKIPPING_LEVEL.TEST,
218-
git_data: GitData = None,
218+
git_data: t.Optional[GitData] = None,
219219
dd_service: t.Optional[str] = None,
220220
dd_env: t.Optional[str] = None,
221221
):
@@ -246,7 +246,7 @@ def _get_expected_do_request_setting_payload(
246246
def _get_expected_do_request_skippable_payload(
247247
self,
248248
itr_skipping_level: ITR_SKIPPING_LEVEL = ITR_SKIPPING_LEVEL.TEST,
249-
git_data: GitData = None,
249+
git_data: t.Optional[GitData] = None,
250250
dd_service: t.Optional[str] = None,
251251
dd_env: t.Optional[str] = None,
252252
):
@@ -275,7 +275,7 @@ def _get_expected_do_request_skippable_payload(
275275

276276
def _get_expected_do_request_tests_payload(
277277
self,
278-
repository_url: str = None,
278+
repository_url: t.Optional[str] = None,
279279
dd_service: t.Optional[str] = None,
280280
dd_env: t.Optional[str] = None,
281281
):

0 commit comments

Comments
 (0)