Skip to content

Commit

Permalink
Removed spurious warnings when assigning results name to originalText…
Browse files Browse the repository at this point in the history
…For expression (Issue #110)
  • Loading branch information
ptmcg committed Oct 31, 2021
1 parent 578ac28 commit e269b0a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Change Log
==========

Version 3.0.5 -
---------------
- Fixed bug when `warn_ungrouped_named_tokens_in_collection` warning was raised
when assigning a results name to an `original_text_for` expression.
(Issue #110, would raise warning in packaging.)


Version 3.0.4 -
---------------
- Fixed bug in which `Dict` classes did not correctly return tokens as nested
Expand Down
2 changes: 1 addition & 1 deletion pyparsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
),
"",
)[__version_info__.release_level == "final"]
__version_time__ = "30 October 2021 16:35 UTC"
__version_time__ = "31 October 2021 11:23 UTC"
version_info.__str__ = lambda *args: "pyparsing {} - {}".format(
__version__, __version_time__
)
Expand Down
4 changes: 2 additions & 2 deletions pyparsing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3660,7 +3660,7 @@ def copy(self):
def _setResultsName(self, name, listAllMatches=False):
if __diag__.warn_ungrouped_named_tokens_in_collection:
for e in self.exprs:
if isinstance(e, ParserElement) and e.resultsName:
if isinstance(e, ParserElement) and e.resultsName and not e.resultsName.startswith("_NOWARN"):
warnings.warn(
"{}: setting results name {!r} on {} expression "
"collides with {!r} on contained expression".format(
Expand Down Expand Up @@ -4701,7 +4701,7 @@ def parseImpl(self, instring, loc, doActions=True):
def _setResultsName(self, name, listAllMatches=False):
if __diag__.warn_ungrouped_named_tokens_in_collection:
for e in [self.expr] + self.expr.recurse():
if isinstance(e, ParserElement) and e.resultsName:
if isinstance(e, ParserElement) and e.resultsName and not e.resultsName.startswith("_NOWARN"):
warnings.warn(
"{}: setting results name {!r} on {} expression "
"collides with {!r} on contained expression".format(
Expand Down
7 changes: 4 additions & 3 deletions pyparsing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,14 @@ def original_text_for(
locMarker = Empty().set_parse_action(lambda s, loc, t: loc)
endlocMarker = locMarker.copy()
endlocMarker.callPreparse = False
matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
# prefix these transient names with _NOWARN to suppress ungrouped name warnings
matchExpr = locMarker("_NOWARN_original_start") + expr + endlocMarker("_NOWARN_original_end")
if asString:
extractText = lambda s, l, t: s[t._original_start : t._original_end]
extractText = lambda s, l, t: s[t._NOWARN_original_start : t._NOWARN_original_end]
else:

def extractText(s, l, t):
t[:] = [s[t.pop("_original_start") : t.pop("_original_end")]]
t[:] = [s[t.pop("_NOWARN_original_start") : t.pop("_NOWARN_original_end")]]

matchExpr.set_parse_action(extractText)
matchExpr.ignoreExprs = expr.ignoreExprs
Expand Down
16 changes: 16 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7552,6 +7552,22 @@ def testWarnUngroupedNamedTokens(self):
):
path = coord[...].setResultsName("path")

def testDontWarnUngroupedNamedTokensIfUngroupedNamesStartWithNOWARN(self):
"""
- warn_ungrouped_named_tokens_in_collection - flag to enable warnings when a results
name is defined on a containing expression with ungrouped subexpressions that also
have results names (default=True)
"""
with ppt.reset_pyparsing_context():
pp.enable_diag(pp.Diagnostics.warn_ungrouped_named_tokens_in_collection)

with self.assertDoesNotWarn(
msg="raised {} warning inner names start with '_NOWARN'".format(
pp.Diagnostics.warn_ungrouped_named_tokens_in_collection
)
):
pp.originalTextFor(pp.Word("ABC")[...])("words")

def testWarnNameSetOnEmptyForward(self):
"""
- warn_name_set_on_empty_Forward - flag to enable warnings when a Forward is defined
Expand Down

0 comments on commit e269b0a

Please sign in to comment.