This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author josh.r
Recipients Matthew Drago, ezio.melotti, josh.r, malin, mrabarnett, steven.daprano, xtreak
Date 2019-03-04.17:28:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1551720533.74.0.660792278575.issue36158@roundup.psfhosted.org>
In-reply-to
Content
Sounds like at least one such entity's trigger attribute doesn't match the regex. In the spelled out loop, you'd still get the exception on a failed match, but you'd store the results for however many entities matched before then (so catching the exception and continuing on would work). List comprehensions are all or nothing; if an exception is raised before it finishes, the list in progress is thrown away.

While wasteful, this should work just fine:

    named_entities = [name_regex.match(entity.trigger).group(1) for entity in entities[0] if name_regex.match(entity.trigger)]

or in 3.8 with assignment expression to avoid repetitive work:

    named_entities = [match.group(1) for entity in entities[0] if match := name_regex.match(entity.trigger)]

The former is wasteful, but works in any Python version; the latter is directly equivalent to:

    named_entities = []
    for entity in entities[0]:
        match = name_regex.match(entity.trigger)
        if match:
            named_entities.append(match.group(1))

The ultimate problem is your regex isn't always matching; list comprehensions just change whether or no you store the partial results.
History
Date User Action Args
2019-03-04 17:28:53josh.rsetrecipients: + josh.r, ezio.melotti, mrabarnett, steven.daprano, malin, xtreak, Matthew Drago
2019-03-04 17:28:53josh.rsetmessageid: <1551720533.74.0.660792278575.issue36158@roundup.psfhosted.org>
2019-03-04 17:28:53josh.rlinkissue36158 messages
2019-03-04 17:28:53josh.rcreate