Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up tree templates implementation to reduce mypy errors #1091

Merged

Conversation

plannigan
Copy link
Contributor

@plannigan plannigan commented Jan 28, 2022

Mostly type annotation changes, but also includes a few code tweaks.

Based on how Tree was used in the file, I'm fairly certain that the intended use case is Tree[str]. However, it is possible that this can be mage even more precise with ParseType (and alias for Tree[Token]).

Resolves

lark/tree_templates.py:35: error: Item "Tree[Any]" of "Union[Any, Tree[Any]]" has no attribute "startswith"  [union-attr]
lark/tree_templates.py:36: error: Item "Tree[Any]" of "Union[Any, Tree[Any]]" has no attribute "lstrip"  [union-attr]
lark/tree_templates.py:123: error: Item "str" of "Union[Tree[Any], str]" has no attribute "iter_subtrees"  [union-attr]
lark/tree_templates.py:155: error: Argument 1 to "translate" has incompatible type "Union[Tree[Any], str]"; expected "Template"  [arg-type]
lark/tree_templates.py:155: error: Argument 2 to "translate" has incompatible type "Union[Tree[Any], str]"; expected "Template"  [arg-type]

lark/tree_templates.py Show resolved Hide resolved
class _ReplaceVars(Transformer):
def __init__(self, conf, vars):
def __init__(self, conf: TemplateConf, vars: Mapping[str, Tree[str]]) -> None:
super().__init__()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Transformer has an __init__(), it is best to call it in case it does something more important in the future.

lark/tree_templates.py Show resolved Hide resolved
lark/tree_templates.py Show resolved Hide resolved
@plannigan plannigan force-pushed the mypy_fix-tree_templates_cleanup branch from 8d3c16e to a8f6da5 Compare February 10, 2022 12:09
@plannigan plannigan force-pushed the mypy_fix-tree_templates_cleanup branch from a8f6da5 to ac98ba1 Compare February 10, 2022 12:13
@@ -129,7 +136,7 @@ def search(self, tree: TreeOrCode):
if res:
yield subtree, res

def apply_vars(self, vars: Mapping[str, Tree]):
def apply_vars(self, vars: Mapping[str, Tree[str]]) -> Tree[str]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation for vars was here originally but conflicts with the annotations I've added based on how the code was written. The key issue is that TemplateConf._match_tree_template() returns a dictionary where the value could be a str or Tree[str]. This then ripples up here because translate() uses this result as the vars argument. This could be a case that the annotation on vars was not completely accurate. Or, since it was one of the few annotations used initially, that it is the intended API. If it is the second, then there would need to a second pass on the implementation to have everything line up.

@erezsh erezsh merged commit 8977de7 into lark-parser:master Feb 11, 2022
@plannigan plannigan deleted the mypy_fix-tree_templates_cleanup branch February 11, 2022 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants