Skip to content

Commit a0a8fc1

Browse files
committed
Make Not and NotIn namedtuples
1 parent bc9344b commit a0a8fc1

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

pre_commit/clientlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def validate_manifest_main(argv=None):
124124
schema.Conditional(
125125
'sha', schema.check_string,
126126
condition_key='repo',
127-
condition_value=schema.NotIn((_LOCAL_SENTINEL, _META_SENTINEL)),
127+
condition_value=schema.NotIn(_LOCAL_SENTINEL, _META_SENTINEL),
128128
ensure_absent=True,
129129
),
130130
)

pre_commit/schema.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,14 @@ def remove_defaults(self, v):
201201
return [remove_defaults(val, self.of) for val in v]
202202

203203

204-
class Not(object):
205-
def __init__(self, val):
206-
self.val = val
207-
204+
class Not(collections.namedtuple('Not', ('val',))):
208205
def __eq__(self, other):
209206
return other is not MISSING and other != self.val
210207

211208

212-
class NotIn(object):
213-
def __init__(self, values):
214-
self.values = values
209+
class NotIn(collections.namedtuple('NotIn', ('values',))):
210+
def __new__(cls, *values):
211+
return super(NotIn, cls).__new__(cls, values=values)
215212

216213
def __eq__(self, other):
217214
return other is not MISSING and other not in self.values

tests/schema_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_not(val, expected):
113113
(('bar', True), ('foo', False), (MISSING, False)),
114114
)
115115
def test_not_in(values, expected):
116-
compared = NotIn(('baz', 'foo'))
116+
compared = NotIn('baz', 'foo')
117117
assert (values == compared) is expected
118118
assert (compared == values) is expected
119119

@@ -211,7 +211,7 @@ def test_optional_key_missing(schema):
211211
'foo', 'key',
212212
Conditional(
213213
'key2', check_bool,
214-
condition_key='key', condition_value=NotIn((1, 2)), ensure_absent=True,
214+
condition_key='key', condition_value=NotIn(1, 2), ensure_absent=True,
215215
),
216216
)
217217

0 commit comments

Comments
 (0)