Skip to content

Commit 48fc24b

Browse files
committed
improve error messages
1 parent 33ae76e commit 48fc24b

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/blurb/blurb.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -930,21 +930,17 @@ def _extract_issue_number(issue):
930930
_section_special_patterns['Tools/Demos'].add(re.compile('^dem(?:o|os)?$', re.I))
931931

932932

933-
def _extract_section_name(section):
934-
if section is None:
935-
return None
936-
937-
section = raw_section = section.strip()
938-
if not section:
939-
sys.exit("Empty section name!")
933+
def _find_smart_matches(section):
934+
# '_', '-' and ' ' are the allowed (user) whitespace separators
935+
sanitized = re.sub(r'[_\- ]', ' ', section).strip()
936+
if not sanitized:
937+
return []
940938

941939
matches = []
942-
943-
# '_', '-', ' ' and '/' are the allowed (user) separators
944-
sanitized = re.sub(r'[_\- /]', ' ', section)
945940
section_words = re.split(r'\s+', sanitized)
946941
# ' ' and '/' are the separators used by known sections
947942
section_pattern = r'[ /]'.join(map(re.escape, section_words))
943+
948944
section_pattern = re.compile(section_pattern, re.I)
949945
for section_name in sections:
950946
# try to use the input as the pattern to match against known names
@@ -959,7 +955,7 @@ def _extract_section_name(section):
959955
break
960956

961957
if not matches:
962-
# try to use the input as the prefix of a known section name
958+
# try to use the input as the prefix of a flattened section name
963959
normalized_prefix = ''.join(section_words).lower()
964960
for section_name, normalized in _section_names_lower_nosep.items():
965961
if (
@@ -968,6 +964,24 @@ def _extract_section_name(section):
968964
):
969965
matches.append(section_name)
970966

967+
return matches
968+
969+
def _extract_section_name(section):
970+
if section is None:
971+
return None
972+
973+
section = raw_section = section.strip()
974+
if not section:
975+
sys.exit("Empty section name!")
976+
977+
matches = []
978+
# try a simple exact match
979+
for section_name in sections:
980+
if section_name.lower().startswith(section.lower()):
981+
matches.append(section_name)
982+
# try a more complex algorithm if we are unlucky
983+
matches = matches or _find_smart_matches(section)
984+
971985
if not matches:
972986
sys.exit(f"Invalid section name: {raw_section!r}\n\n"
973987
f"Choose from the following table:\n\n"

tests/test_blurb_add.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,17 @@ def test_exact_names_uppercase(self, section, expect):
204204
def test_empty_section_name(section):
205205
error_message = re.escape('Empty section name!')
206206
with pytest.raises(SystemExit, match=error_message):
207-
blurb._extract_section_name('')
207+
blurb._extract_section_name(section)
208208

209209
with pytest.raises(SystemExit, match=error_message):
210-
blurb._update_blurb_template(issue=None, section='')
210+
blurb._update_blurb_template(issue=None, section=section)
211211

212212

213213
@pytest.mark.parametrize('section', [
214214
# invalid
215+
'_',
216+
'-',
217+
'/',
215218
'invalid',
216219
'Not a section',
217220
# non-special names

0 commit comments

Comments
 (0)