Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e8925d

Browse files
authored
↪️ Merge pull request #293 from Yelp/various_small_improvements
Various small improvements
2 parents ca266b1 + 74d3787 commit 0e8925d

43 files changed

Lines changed: 243 additions & 153 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ repos:
44
hooks:
55
- id: check-builtin-literals
66
args: ['--no-allow-dict-kwargs']
7+
exclude: bumpity.py$
78
- id: check-docstring-first
89
- id: debug-statements
10+
exclude: bumpity.py$
911
- id: double-quote-string-fixer
1012
- id: end-of-file-fixer
1113
- id: name-tests-test
1214
- id: flake8
1315
args: ['--max-line-length', '100']
14-
exclude: ^test_data/
16+
exclude: ^test_data/|bumpity.py$
1517
- id: trailing-whitespace
1618
- repo: https://github.com/asottile/reorder_python_imports
1719
rev: v1.6.1
1820
hooks:
1921
- id: reorder-python-imports
2022
language_version: python3
23+
exclude: bumpity.py$
2124
- repo: https://github.com/asottile/add-trailing-comma
2225
rev: v1.4.1
2326
hooks:

detect_secrets/core/bidirectional_iterator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class BidirectionalIterator(object):
1+
class BidirectionalIterator:
22
def __init__(self, collection):
33
self.collection = collection
44
self.index = -1 # Starts on -1, as index is increased _before_ getting result

detect_secrets/core/code_snippet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_code_snippet(self, file_lines, line_number, lines_of_context=5):
4343
)
4444

4545

46-
class CodeSnippet(object):
46+
class CodeSnippet:
4747

4848
def __init__(self, snippet, start_line, target_index):
4949
"""

detect_secrets/core/potential_secret.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hashlib
22

33

4-
class PotentialSecret(object):
4+
class PotentialSecret:
55
"""This custom data type represents a string found, matching the
66
plugin rules defined in SecretsCollection, that has the potential
77
to be a secret that we actually care about.

detect_secrets/core/secrets_collection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from detect_secrets.util import build_automaton
1414

1515

16-
class SecretsCollection(object):
16+
class SecretsCollection:
1717

1818
def __init__(
1919
self,
@@ -156,6 +156,8 @@ def scan_diff(
156156
at incremental differences, rather than re-scanning the codebase every time.
157157
This function supports this, and adds information to self.data.
158158
159+
Note that this is only called by detect-secrets-server.
160+
159161
:type diff: str
160162
:param diff: diff string.
161163
e.g. The output of `git diff <fileA> <fileB>`
@@ -338,6 +340,7 @@ def _extract_secrets_from_patch(self, f, plugin, filename):
338340
"""Extract secrets from a given patch file object.
339341
340342
Note that we only want to capture incoming secrets (so added lines).
343+
Note that this is only called by detect-secrets-server.
341344
342345
:type f: unidiff.patch.PatchedFile
343346
:type plugin: detect_secrets.plugins.base.BasePlugin

detect_secrets/core/usage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def add_no_verify_flag(parser):
4242
)
4343

4444

45-
class ParserBuilder(object):
45+
class ParserBuilder:
4646

4747
def __init__(self):
4848
self.parser = argparse.ArgumentParser()
@@ -133,7 +133,7 @@ def _add_no_verify_flag(self):
133133
return self
134134

135135

136-
class ScanOptions(object):
136+
class ScanOptions:
137137

138138
def __init__(self, subparser):
139139
self.parser = subparser.add_parser(
@@ -209,7 +209,7 @@ def _add_adhoc_scanning_argument(self):
209209
)
210210

211211

212-
class AuditOptions(object):
212+
class AuditOptions:
213213

214214
def __init__(self, subparser):
215215
self.parser = subparser.add_parser(
@@ -320,7 +320,7 @@ def get_disabled_help_text(plugin):
320320
return 'Disables {}'.format(line)
321321

322322

323-
class PluginOptions(object):
323+
class PluginOptions:
324324

325325
all_plugins = [
326326
PluginDescriptor.from_plugin_class(plugin, name)

detect_secrets/plugins/base.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __get__(self, cls, owner):
2424
return classmethod(self.fget).__get__(None, owner)()
2525

2626

27-
class BasePlugin(object):
27+
class BasePlugin:
2828
"""
2929
This is an abstract class to define Plugins API.
3030
@@ -70,17 +70,15 @@ def __init__(
7070
:param false_positive_heuristics: List of fp-heuristic functions
7171
applicable to this plugin
7272
"""
73-
self.exclude_lines_regex = None
74-
if exclude_lines_regex:
75-
self.exclude_lines_regex = re.compile(exclude_lines_regex)
73+
self.exclude_lines_regex = (
74+
re.compile(exclude_lines_regex)
75+
if exclude_lines_regex
76+
else None
77+
)
7678

7779
self.should_verify = should_verify
7880

79-
self.false_positive_heuristics = (
80-
false_positive_heuristics
81-
if false_positive_heuristics
82-
else []
83-
)
81+
self.false_positive_heuristics = false_positive_heuristics or []
8482

8583
@classproperty
8684
def disable_flag_text(cls):
@@ -101,6 +99,19 @@ def disable_flag_text(cls):
10199
def default_options(cls):
102100
return {}
103101

102+
def _is_excluded_line(self, line):
103+
return (
104+
any(
105+
allowlist_regex.search(line)
106+
for allowlist_regex in ALLOWLIST_REGEXES
107+
)
108+
or
109+
(
110+
self.exclude_lines_regex and
111+
self.exclude_lines_regex.search(line)
112+
)
113+
)
114+
104115
def analyze(self, file, filename):
105116
"""
106117
:param file: The File object itself.
@@ -114,6 +125,13 @@ def analyze(self, file, filename):
114125
file_lines = tuple(file.readlines())
115126
for line_num, line in enumerate(file_lines, start=1):
116127
results = self.analyze_line(line, line_num, filename)
128+
if (
129+
not results
130+
or
131+
self._is_excluded_line(line)
132+
):
133+
continue
134+
117135
if not self.should_verify:
118136
potential_secrets.update(results)
119137
continue
@@ -146,18 +164,6 @@ def analyze_line(self, string, line_num, filename):
146164
147165
NOTE: line_num and filename are used for PotentialSecret creation only.
148166
"""
149-
if (
150-
any(
151-
allowlist_regex.search(string) for allowlist_regex in ALLOWLIST_REGEXES
152-
)
153-
154-
or (
155-
self.exclude_lines_regex and
156-
self.exclude_lines_regex.search(string)
157-
)
158-
):
159-
return {}
160-
161167
return self.analyze_string_content(
162168
string,
163169
line_num,

detect_secrets/plugins/common/ini_file_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def append(self, lineno, line):
1717
configparser.ParsingError = EfficientParsingError
1818

1919

20-
class IniFileParser(object):
20+
class IniFileParser:
2121

2222
_comment_regex = re.compile(r'\s*[;#]')
2323

detect_secrets/plugins/common/initialize.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ def from_plugin_classname(
165165
try:
166166
klass = import_plugins()[plugin_classname]
167167
except KeyError:
168-
log.warning('No such plugin to initialize.')
168+
log.error('Error: No such `{}` plugin to initialize.'.format(plugin_classname))
169+
log.error('Chances are you should run `pre-commit autoupdate`.')
170+
log.error(
171+
'This error occurs when using a baseline that was made by '
172+
'a newer detect-secrets version than the one running.',
173+
)
169174
raise TypeError
170175

171176
try:

detect_secrets/plugins/common/yaml_file_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .constants import ALLOWLIST_REGEX
44

55

6-
class YamlFileParser(object):
6+
class YamlFileParser:
77
"""
88
Yaml config files are interesting, because they don't necessarily conform
99
to our basic regex for detecting HighEntropyStrings as strings don't

0 commit comments

Comments
 (0)