Skip to content

Commit a8f6638

Browse files
author
Dan Hertz
committed
Fix type annotations for older python
1 parent 2e1ee9f commit a8f6638

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ jobs:
3131
NIGHTFALL_API_KEY: ${{ secrets.NIGHTFALL_API_KEY }}
3232
WEBHOOK_ENDPOINT: ${{ secrets.WEBHOOK_ENDPOINT }}
3333
run: |
34-
pytest --cov=nightfall --cov-report term-missing tests
34+
pytest -m "not filetest" --cov=nightfall --cov-report term-missing tests

nightfall/api.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import json
1111
import logging
1212
import os
13+
from typing import List
14+
1315
import requests
1416

1517
from nightfall.detection_rules import DetectionRule
@@ -48,8 +50,8 @@ def __init__(self, key: str = None, signing_secret: str = None):
4850
self.signing_secret = signing_secret
4951
self.logger = logging.getLogger(__name__)
5052

51-
def scan_text(self, texts: list[str], detection_rule_uuids: list[str] = None,
52-
detection_rules: list[DetectionRule] = None) -> [list[list[Finding]], list[str]]:
53+
def scan_text(self, texts: List[str], detection_rule_uuids: List[str] = None,
54+
detection_rules: List[DetectionRule] = None) -> [List[List[Finding]], List[str]]:
5355
"""Scan text with Nightfall.
5456
5557
This method takes the specified config and then makes
@@ -58,12 +60,12 @@ def scan_text(self, texts: list[str], detection_rule_uuids: list[str] = None,
5860
5961
6062
:param texts: List of strings to scan.
61-
:type texts: list[str]
63+
:type texts: List[str]
6264
:param detection_rule_uuids: List of detection rule UUIDs to scan each text with.
6365
These can be created in the Nightfall UI.
64-
:type detection_rule_uuids: list[str] or None
66+
:type detection_rule_uuids: List[str] or None
6567
:param detection_rules: List of detection rules to scan each text with.
66-
:type detection_rules: list[DetectionRule] or None
68+
:type detection_rules: List[DetectionRule] or None
6769
:returns: list of findings, list of redacted input texts
6870
"""
6971

@@ -109,7 +111,7 @@ def _scan_text_v3(self, data):
109111
# File Scan
110112

111113
def scan_file(self, location: str, webhook_url: str, policy_uuid: str = None,
112-
detection_rule_uuids: list[str] = None, detection_rules: list[DetectionRule] = None,
114+
detection_rule_uuids: List[str] = None, detection_rules: List[DetectionRule] = None,
113115
) -> [str, str]:
114116
"""Scan file with Nightfall.
115117
At least one of policy_uuid, detection_rule_uuids or detection_rules is required.
@@ -119,9 +121,9 @@ def scan_file(self, location: str, webhook_url: str, policy_uuid: str = None,
119121
:param policy_uuid: policy UUID.
120122
:type policy_uuid: str or None
121123
:param detection_rule_uuids: list of detection rule UUIDs.
122-
:type detection_rule_uuids: list[str] or None
124+
:type detection_rule_uuids: List[str] or None
123125
:param detection_rules: list of detection rules.
124-
:type detection_rules: list[DetectionRule] or None
126+
:type detection_rules: List[DetectionRule] or None
125127
:returns: (scan_id, message)
126128
"""
127129

@@ -198,7 +200,7 @@ def _file_scan_finalize(self, session_id):
198200
return response
199201

200202
def _file_scan_scan(self, session_id: str, webhook_url: str, policy_uuid: str,
201-
detection_rule_uuids: str, detection_rules: list[DetectionRule]):
203+
detection_rule_uuids: str, detection_rules: List[DetectionRule]):
202204
if policy_uuid:
203205
data = {"policyUUID": policy_uuid}
204206
else:

nightfall/detection_rules.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from enum import Enum
2+
from typing import List
23

34
from nightfall.exceptions import NightfallUserError
45

@@ -21,10 +22,10 @@ def as_dict(self):
2122

2223
class WordList:
2324
"""A list of words that can be used to customize the behavior of a detector while Nightfall performs a scan."""
24-
def __init__(self, word_list: list[str], is_case_sensitive: bool):
25+
def __init__(self, word_list: List[str], is_case_sensitive: bool):
2526
"""Instantiate a WordList object
2627
:param word_list: The list of words to use.
27-
:type word_list: list[str]
28+
:type word_list: List[str]
2829
:param is_case_sensitive: Whether to make matches have the same case as each word given
2930
:type is_case_sensitive: bool
3031
"""
@@ -106,7 +107,7 @@ def as_dict(self):
106107
class MaskConfig:
107108
"""An object that specifies how findings should be masked when returned by the API."""
108109
def __init__(self, masking_char: chr, num_chars_to_leave_unmasked: int = 0,
109-
mask_right_to_left: bool = False, chars_to_ignore: list[chr] = []):
110+
mask_right_to_left: bool = False, chars_to_ignore: List[chr] = []):
110111
"""Instantiate a MaskConfig object
111112
:param masking_char: character that will be repeated to replace the finding.
112113
This character may be a multi-byte character, but it must be exactly one codepoint.
@@ -118,7 +119,7 @@ def __init__(self, masking_char: chr, num_chars_to_leave_unmasked: int = 0,
118119
:type mask_right_to_left: bool
119120
:param chars_to_ignore: the set of characters to leave unmasked when the finding is returned. These characters
120121
may be multi-byte characters, but each entry in the array must be exactly one codepoint.
121-
:type chars_to_ignore: list[chr]
122+
:type chars_to_ignore: List[chr]
122123
"""
123124
self.masking_char = masking_char
124125
self.num_chars_to_leave_unmasked = num_chars_to_leave_unmasked
@@ -204,8 +205,8 @@ def __init__(
204205
word_list: WordList = None,
205206
uuid: str = None,
206207
display_name: str = None,
207-
context_rules: list[ContextRule] = None,
208-
exclusion_rules: list[ExclusionRule] = None,
208+
context_rules: List[ContextRule] = None,
209+
exclusion_rules: List[ExclusionRule] = None,
209210
redaction_config: RedactionConfig = None
210211
):
211212
"""Instantiate a Detector object.
@@ -226,9 +227,9 @@ def __init__(
226227
:param display_name: A display name for this detector.
227228
:type display_name: str or None
228229
:param context_rules: The context rules to use to customize the behavior of this detector.
229-
:type context_rules: list[ContextRule] or None
230+
:type context_rules: List[ContextRule] or None
230231
:param exclusion_rules: The exclusion rules to use to customize the behavior of this detector.
231-
:type exclusion_rules: list[ExclusionRule] or None
232+
:type exclusion_rules: List[ExclusionRule] or None
232233
:param redaction_config: Sets the redaction configuration to-be-applied to this detector. This configuration is
233234
currently only supported for scanning plaintext, not for file scanning.
234235
:type redaction_config: RedactionConfig or None
@@ -284,10 +285,10 @@ class LogicalOp(Enum):
284285

285286
class DetectionRule:
286287
"""An object that contains a set of detectors to be used when scanning content."""
287-
def __init__(self, detectors: list[Detector], logical_op: LogicalOp = LogicalOp.ANY):
288+
def __init__(self, detectors: List[Detector], logical_op: LogicalOp = LogicalOp.ANY):
288289
"""Instantiate a DetectionRule
289290
:param detectors: A list of Detectors to scan content with.
290-
:type detectors: list[Detector]
291+
:type detectors: List[Detector]
291292
:param logical_op: The method for combining the detectors. One of:
292293
- LogicalOp.ANY (logical or, i.e. a finding is emitted only if any of the provided detectors match)
293294
- LogicalOp.ALL (logical and, i.e. a finding is emitted only if all provided detectors match)

nightfall/findings.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
from nightfall.detection_rules import Confidence
24

35

@@ -26,8 +28,8 @@ def __init__(self,
2628
confidence: Confidence,
2729
byte_range: Range,
2830
codepoint_range: Range,
29-
matched_detection_rule_uuids: list[str],
30-
matched_detection_rules: list[str]):
31+
matched_detection_rule_uuids: List[str],
32+
matched_detection_rules: List[str]):
3133
"""Instantiate a Finding object.
3234
:param finding: The data that triggered a detector match.
3335
@@ -51,10 +53,10 @@ def __init__(self,
5153
:type codepoint_range: Range
5254
:param matched_detection_rule_uuids: The list of detection rule UUIDs that contained a detector that triggered a
5355
match.
54-
:type matched_detection_rule_uuids: list[str]
56+
:type matched_detection_rule_uuids: List[str]
5557
:param matched_detection_rules: The list of inline detection rules that contained a detector that triggered a
5658
match.
57-
:type matched_detection_rules: list[str]
59+
:type matched_detection_rules: List[str]
5860
"""
5961
self.finding = finding
6062
self.redacted_finding = redacted_finding

tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_scan_text_detection_rules_v3(nightfall):
2121
assert len(result) == 1
2222

2323

24+
@pytest.mark.filetest
2425
def test_scan_file_detection_rules(nightfall):
2526
file = "file.txt"
2627

0 commit comments

Comments
 (0)