Skip to content

Commit

Permalink
Fix pep8 compatibility code (fixes #501) (#507)
Browse files Browse the repository at this point in the history
* Fix pep8 compatibility code (fixes #501)

* When using the `replaced_by_pep8` decorator with a different method
  signature for simplicity, as the real signature is generated by the
  decorator, some static checkers might find false positive errors when
  calling pyparsing code (see issue #501).
* Refactor the calls for the compatibility code to not use the
  `replaced_by_pep8` decorator but calling directly the
  `_make_synonym_function` function to generate the synonym
  methods/functions.
* This way the signature of the method/function is automatically copied
  and the static checkers will not report false positives.

* Fix pep8 compatibility code (fixes #501)

* Rename the `_make_synonym_function` function to `replaced_by_pep8` for
  clarity.
* Reduce the stacklevel of the commented out deprecation warnings
  accordingly.
  • Loading branch information
volans- committed Oct 2, 2023
1 parent 8d25b5f commit 4a3a62a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 169 deletions.
20 changes: 5 additions & 15 deletions pyparsing/actions.py
Expand Up @@ -199,19 +199,9 @@ def with_class(classname, namespace=""):

# pre-PEP8 compatibility symbols
# fmt: off
@replaced_by_pep8(replace_with)
def replaceWith(): ...

@replaced_by_pep8(remove_quotes)
def removeQuotes(): ...

@replaced_by_pep8(with_attribute)
def withAttribute(): ...

@replaced_by_pep8(with_class)
def withClass(): ...

@replaced_by_pep8(match_only_at_col)
def matchOnlyAtCol(): ...

replaceWith = replaced_by_pep8("replaceWith", replace_with)
removeQuotes = replaced_by_pep8("removeQuotes", remove_quotes)
withAttribute = replaced_by_pep8("withAttribute", with_attribute)
withClass = replaced_by_pep8("withClass", with_class)
matchOnlyAtCol = replaced_by_pep8("matchOnlyAtCol", match_only_at_col)
# fmt: on
139 changes: 36 additions & 103 deletions pyparsing/core.py
Expand Up @@ -2253,82 +2253,32 @@ def create_diagram(

# Compatibility synonyms
# fmt: off
@staticmethod
@replaced_by_pep8(inline_literals_using)
def inlineLiteralsUsing(): ...

@staticmethod
@replaced_by_pep8(set_default_whitespace_chars)
def setDefaultWhitespaceChars(): ...

@replaced_by_pep8(set_results_name)
def setResultsName(self): ...

@replaced_by_pep8(set_break)
def setBreak(self): ...

@replaced_by_pep8(set_parse_action)
def setParseAction(self): ...

@replaced_by_pep8(add_parse_action)
def addParseAction(self): ...

@replaced_by_pep8(add_condition)
def addCondition(self): ...

@replaced_by_pep8(set_fail_action)
def setFailAction(self): ...

@replaced_by_pep8(try_parse)
def tryParse(self): ...

@staticmethod
@replaced_by_pep8(enable_left_recursion)
def enableLeftRecursion(): ...

@staticmethod
@replaced_by_pep8(enable_packrat)
def enablePackrat(): ...

@replaced_by_pep8(parse_string)
def parseString(self): ...

@replaced_by_pep8(scan_string)
def scanString(self): ...

@replaced_by_pep8(transform_string)
def transformString(self): ...

@replaced_by_pep8(search_string)
def searchString(self): ...

@replaced_by_pep8(ignore_whitespace)
def ignoreWhitespace(self): ...

@replaced_by_pep8(leave_whitespace)
def leaveWhitespace(self): ...

@replaced_by_pep8(set_whitespace_chars)
def setWhitespaceChars(self): ...

@replaced_by_pep8(parse_with_tabs)
def parseWithTabs(self): ...

@replaced_by_pep8(set_debug_actions)
def setDebugActions(self): ...

@replaced_by_pep8(set_debug)
def setDebug(self): ...

@replaced_by_pep8(set_name)
def setName(self): ...

@replaced_by_pep8(parse_file)
def parseFile(self): ...

@replaced_by_pep8(run_tests)
def runTests(self): ...

inlineLiteralsUsing = replaced_by_pep8("inlineLiteralsUsing", inline_literals_using)
setDefaultWhitespaceChars = replaced_by_pep8(
"setDefaultWhitespaceChars", set_default_whitespace_chars
)
setResultsName = replaced_by_pep8("setResultsName", set_results_name)
setBreak = replaced_by_pep8("setBreak", set_break)
setParseAction = replaced_by_pep8("setParseAction", set_parse_action)
addParseAction = replaced_by_pep8("addParseAction", add_parse_action)
addCondition = replaced_by_pep8("addCondition", add_condition)
setFailAction = replaced_by_pep8("setFailAction", set_fail_action)
tryParse = replaced_by_pep8("tryParse", try_parse)
enableLeftRecursion = replaced_by_pep8("enableLeftRecursion", enable_left_recursion)
enablePackrat = replaced_by_pep8("enablePackrat", enable_packrat)
parseString = replaced_by_pep8("parseString", parse_string)
scanString = replaced_by_pep8("scanString", scan_string)
transformString = replaced_by_pep8("transformString", transform_string)
searchString = replaced_by_pep8("searchString", search_string)
ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
setWhitespaceChars = replaced_by_pep8("setWhitespaceChars", set_whitespace_chars)
parseWithTabs = replaced_by_pep8("parseWithTabs", parse_with_tabs)
setDebugActions = replaced_by_pep8("setDebugActions", set_debug_actions)
setDebug = replaced_by_pep8("setDebug", set_debug)
setName = replaced_by_pep8("setName", set_name)
parseFile = replaced_by_pep8("parseFile", parse_file)
runTests = replaced_by_pep8("runTests", run_tests)
canParseNext = can_parse_next
resetCache = reset_cache
defaultName = default_name
Expand Down Expand Up @@ -3911,11 +3861,8 @@ def _setResultsName(self, name, listAllMatches=False):

# Compatibility synonyms
# fmt: off
@replaced_by_pep8(leave_whitespace)
def leaveWhitespace(self): ...

@replaced_by_pep8(ignore_whitespace)
def ignoreWhitespace(self): ...
leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on


Expand Down Expand Up @@ -4635,11 +4582,8 @@ def _generateDefaultName(self) -> str:

# Compatibility synonyms
# fmt: off
@replaced_by_pep8(leave_whitespace)
def leaveWhitespace(self): ...

@replaced_by_pep8(ignore_whitespace)
def ignoreWhitespace(self): ...
leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on


Expand Down Expand Up @@ -5666,11 +5610,8 @@ def _setResultsName(self, name, list_all_matches=False):

# Compatibility synonyms
# fmt: off
@replaced_by_pep8(leave_whitespace)
def leaveWhitespace(self): ...

@replaced_by_pep8(ignore_whitespace)
def ignoreWhitespace(self): ...
leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on


Expand Down Expand Up @@ -6144,16 +6085,8 @@ def autoname_elements() -> None:
lineEnd = line_end
stringStart = string_start
stringEnd = string_end

@replaced_by_pep8(null_debug_action)
def nullDebugAction(): ...

@replaced_by_pep8(trace_parse_action)
def traceParseAction(): ...

@replaced_by_pep8(condition_as_parse_action)
def conditionAsParseAction(): ...

@replaced_by_pep8(token_map)
def tokenMap(): ...
nullDebugAction = replaced_by_pep8("nullDebugAction", null_debug_action)
traceParseAction = replaced_by_pep8("traceParseAction", trace_parse_action)
conditionAsParseAction = replaced_by_pep8("conditionAsParseAction", condition_as_parse_action)
tokenMap = replaced_by_pep8("tokenMap", token_map)
# fmt: on
3 changes: 1 addition & 2 deletions pyparsing/exceptions.py
Expand Up @@ -244,8 +244,7 @@ def explain(self, depth=16) -> str:
return self.explain_exception(self, depth)

# fmt: off
@replaced_by_pep8(mark_input_line)
def markInputline(self): ...
markInputline = replaced_by_pep8("markInputline", mark_input_line)
# fmt: on


Expand Down
52 changes: 13 additions & 39 deletions pyparsing/helpers.py
Expand Up @@ -1058,43 +1058,17 @@ def delimited_list(
cppStyleComment = cpp_style_comment
javaStyleComment = java_style_comment
pythonStyleComment = python_style_comment

@replaced_by_pep8(DelimitedList)
def delimitedList(): ...

@replaced_by_pep8(DelimitedList)
def delimited_list(): ...

@replaced_by_pep8(counted_array)
def countedArray(): ...

@replaced_by_pep8(match_previous_literal)
def matchPreviousLiteral(): ...

@replaced_by_pep8(match_previous_expr)
def matchPreviousExpr(): ...

@replaced_by_pep8(one_of)
def oneOf(): ...

@replaced_by_pep8(dict_of)
def dictOf(): ...

@replaced_by_pep8(original_text_for)
def originalTextFor(): ...

@replaced_by_pep8(nested_expr)
def nestedExpr(): ...

@replaced_by_pep8(make_html_tags)
def makeHTMLTags(): ...

@replaced_by_pep8(make_xml_tags)
def makeXMLTags(): ...

@replaced_by_pep8(replace_html_entity)
def replaceHTMLEntity(): ...

@replaced_by_pep8(infix_notation)
def infixNotation(): ...
delimitedList = replaced_by_pep8("delimitedList", DelimitedList)
delimited_list = replaced_by_pep8("delimited_list", DelimitedList)
countedArray = replaced_by_pep8("countedArray", counted_array)
matchPreviousLiteral = replaced_by_pep8("matchPreviousLiteral", match_previous_literal)
matchPreviousExpr = replaced_by_pep8("matchPreviousExpr", match_previous_expr)
oneOf = replaced_by_pep8("oneOf", one_of)
dictOf = replaced_by_pep8("dictOf", dict_of)
originalTextFor = replaced_by_pep8("originalTextFor", original_text_for)
nestedExpr = replaced_by_pep8("nestedExpr", nested_expr)
makeHTMLTags = replaced_by_pep8("makeHTMLTags", make_html_tags)
makeXMLTags = replaced_by_pep8("makeXMLTags", make_xml_tags)
replaceHTMLEntity = replaced_by_pep8("replaceHTMLEntity", replace_html_entity)
infixNotation = replaced_by_pep8("infixNotation", infix_notation)
# fmt: on
13 changes: 3 additions & 10 deletions pyparsing/util.py
Expand Up @@ -237,7 +237,7 @@ def _flatten(ll: list) -> list:
return ret


def _make_synonym_function(compat_name: str, fn: C) -> C:
def replaced_by_pep8(compat_name: str, fn: C) -> C:
# In a future version, uncomment the code in the internal _inner() functions
# to begin emitting DeprecationWarnings.

Expand All @@ -251,7 +251,7 @@ def _make_synonym_function(compat_name: str, fn: C) -> C:
@wraps(fn)
def _inner(self, *args, **kwargs):
# warnings.warn(
# f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=3
# f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=2
# )
return fn(self, *args, **kwargs)

Expand All @@ -260,7 +260,7 @@ def _inner(self, *args, **kwargs):
@wraps(fn)
def _inner(*args, **kwargs):
# warnings.warn(
# f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=3
# f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=2
# )
return fn(*args, **kwargs)

Expand All @@ -275,10 +275,3 @@ def _inner(*args, **kwargs):
_inner.__kwdefaults__ = None
_inner.__qualname__ = fn.__qualname__
return cast(C, _inner)


def replaced_by_pep8(fn: C) -> Callable[[Callable], C]:
"""
Decorator for pre-PEP8 compatibility synonyms, to link them to the new function.
"""
return lambda other: _make_synonym_function(other.__name__, fn)

0 comments on commit 4a3a62a

Please sign in to comment.