Skip to content

Commit ef9b2df

Browse files
committed
Rename invalid_keys to invalid_symbols #56
* We keep track of invalid license symbols from syntax errors Signed-off-by: Jono Yang <[email protected]>
1 parent 8d86c4b commit ef9b2df

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/license_expression/__init__.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,31 @@ class ExpressionInfo:
141141
- errors: list
142142
- If there were errors validating a license expression,
143143
the error messages will be appended here.
144-
- invalid_keys: list
144+
- invalid_symbols: list
145145
- If the license expression that has been passed into `validate()` has
146146
license keys that are invalid (either that they are unknown or not used
147-
in the right context), then those keys will be appended here.
147+
in the right context), or the syntax is incorrect because an invalid
148+
symbol was used, then those symbols will be appended here.
148149
"""
149150
def __init__(
150151
self,
151152
original_expression,
152153
normalized_expression=None,
153154
errors=None,
154-
invalid_keys=None,
155+
invalid_symbols=None,
155156
):
156157
self.original_expression = original_expression
157158
self.normalized_expression = normalized_expression
158159
self.errors = errors or []
159-
self.invalid_keys = invalid_keys or []
160+
self.invalid_symbols = invalid_symbols or []
160161

161162
def __repr__(self):
162163
return (
163164
'ExpressionInfo(\n'
164165
f' original_expression={self.original_expression!r},\n'
165166
f' normalized_expression={self.normalized_expression!r},\n'
166167
f' errors={self.errors!r},\n'
167-
f' invalid_keys={self.invalid_keys!r}\n'
168+
f' invalid_symbols={self.invalid_symbols!r}\n'
168169
')'
169170
)
170171

@@ -684,8 +685,8 @@ def validate(self, expression, strict=True, **kwargs):
684685
If an error was encountered when validating `expression`,
685686
`ExpressionInfo.errors` will be populated with strings containing the
686687
error message that has occured. If an error has occured due to unknown
687-
license keys, the offending keys will be present in
688-
`ExpressionInfo.invalid_keys`
688+
license keys or an invalid license symbol, the offending keys or symbols
689+
will be present in `ExpressionInfo.invalid_symbols`
689690
690691
If `strict` is True, validation error messages will be included if in a "WITH"
691692
expression such as "XXX with ZZZ" if the XXX symbol has `is_exception`
@@ -701,11 +702,7 @@ def validate(self, expression, strict=True, **kwargs):
701702
parsed_expression = self.parse(expression, strict=strict)
702703
except ExpressionError as e:
703704
expression_info.errors.append(str(e))
704-
error_code = e.error_code
705-
# If we have these error codes, then we have a problematic license
706-
# key that we need to append to `invalid_keys`
707-
if error_code and error_code in (PARSE_INVALID_EXCEPTION, PARSE_INVALID_SYMBOL_AS_EXCEPTION):
708-
expression_info.invalid_keys.append(e.token_string)
705+
expression_info.invalid_symbols.append(e.token_string)
709706
return expression_info
710707

711708
# Check `expression` keys (validate)
@@ -714,13 +711,13 @@ def validate(self, expression, strict=True, **kwargs):
714711
except ExpressionError as e:
715712
expression_info.errors.append(str(e))
716713
unknown_keys = self.unknown_license_keys(expression)
717-
expression_info.invalid_keys.extend(unknown_keys)
714+
expression_info.invalid_symbols.extend(unknown_keys)
718715
return expression_info
719716

720717
# If we have not hit an exception, set `normalized_expression` in
721718
# `expression_info` only if we did not encounter any errors
722719
# along the way
723-
if not expression_info.errors and not expression_info.invalid_keys:
720+
if not expression_info.errors and not expression_info.invalid_symbols:
724721
expression_info.normalized_expression = str(parsed_expression)
725722
return expression_info
726723

tests/test_license_expression.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,56 +2242,56 @@ def test_validate_simple(self):
22422242
assert result.original_expression == 'GPL-2.0-or-later AND MIT'
22432243
assert result.normalized_expression == 'GPL-2.0-or-later AND MIT'
22442244
assert result.errors == []
2245-
assert result.invalid_keys == []
2245+
assert result.invalid_symbols == []
22462246

22472247
def test_validation_invalid_license_key(self):
22482248
result = self.licensing.validate('cool-license')
22492249
assert result.original_expression == 'cool-license'
22502250
assert not result.normalized_expression
22512251
assert result.errors == ['Unknown license key(s): cool-license']
2252-
assert result.invalid_keys == ['cool-license']
2252+
assert result.invalid_symbols == ['cool-license']
22532253

22542254
def test_validate_exception(self):
22552255
result = self.licensing.validate('GPL-2.0-or-later WITH WxWindows-exception-3.1')
22562256
assert result.original_expression == 'GPL-2.0-or-later WITH WxWindows-exception-3.1'
22572257
assert result.normalized_expression == 'GPL-2.0-or-later WITH WxWindows-exception-3.1'
22582258
assert result.errors == []
2259-
assert result.invalid_keys == []
2259+
assert result.invalid_symbols == []
22602260

22612261
def test_validation_exception_with_choice(self):
22622262
result = self.licensing.validate('GPL-2.0-or-later WITH WxWindows-exception-3.1 OR MIT')
22632263
assert result.original_expression == 'GPL-2.0-or-later WITH WxWindows-exception-3.1 OR MIT'
22642264
assert result.normalized_expression == 'GPL-2.0-or-later WITH WxWindows-exception-3.1 OR MIT'
22652265
assert result.errors == []
2266-
assert result.invalid_keys == []
2266+
assert result.invalid_symbols == []
22672267

22682268
def test_validation_exception_as_regular_key(self):
22692269
result = self.licensing.validate('GPL-2.0-or-later AND WxWindows-exception-3.1')
22702270
assert result.original_expression == 'GPL-2.0-or-later AND WxWindows-exception-3.1'
22712271
assert not result.normalized_expression
22722272
assert result.errors == ['A license exception symbol can only be used as an exception in a "WITH exception" statement. for token: "WxWindows-exception-3.1" at position: 21']
2273-
assert result.invalid_keys == ['WxWindows-exception-3.1']
2273+
assert result.invalid_symbols == ['WxWindows-exception-3.1']
22742274

22752275
def test_validation_bad_syntax(self):
22762276
result = self.licensing.validate('Apache-2.0 + MIT')
22772277
assert result.original_expression == 'Apache-2.0 + MIT'
22782278
assert not result.normalized_expression
22792279
assert result.errors == ['Invalid symbols sequence such as (A B) for token: "+" at position: 11']
2280-
assert result.invalid_keys == []
2280+
assert result.invalid_symbols == ['+']
22812281

22822282
def test_validation_invalid_license_exception(self):
22832283
result = self.licensing.validate('Apache-2.0 WITH MIT')
22842284
assert result.original_expression == 'Apache-2.0 WITH MIT'
22852285
assert not result.normalized_expression
22862286
assert result.errors == ["A plain license symbol cannot be used as an exception in a \"WITH symbol\" statement. for token: \"MIT\" at position: 16"]
2287-
assert result.invalid_keys == ['MIT']
2287+
assert result.invalid_symbols == ['MIT']
22882288

22892289
def test_validation_invalid_license_exception_strict_false(self):
22902290
result = self.licensing.validate('Apache-2.0 WITH MIT', strict=False)
22912291
assert result.original_expression == 'Apache-2.0 WITH MIT'
22922292
assert result.normalized_expression == 'Apache-2.0 WITH MIT'
22932293
assert result.errors == []
2294-
assert result.invalid_keys == []
2294+
assert result.invalid_symbols == []
22952295

22962296

22972297
class UtilTest(TestCase):

0 commit comments

Comments
 (0)