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

fix(types): some typing updates #1149

Closed
wants to merge 2 commits into from

Conversation

henryiii
Copy link
Contributor

@henryiii henryiii commented May 24, 2022

Added mypy configuration, and fixing all but one of the typing issues.

The remaining one I'm not sure about:

lark/tree_templates.py:150: error: Argument 1 to "apply_vars" of "Template" has incompatible type "Dict[str, Union[Tree[str], str]]"; expected "Mapping[str, Tree[str]]"  [arg-type]

I'm using tox since that's already set up. I personally prefer nox; better defaults, no custom language, explains what it's doing while it's doing it. I've also updated the CI config. And for mypy, I prefer pre-commit (the tool); easy to update pins, ultra fast to run due to excellent caching, single place for all style checks & fixers.

  • fix(types): address several type issues, add type check job
  • ci: update and use tox for mypy

@henryiii
Copy link
Contributor Author

henryiii commented May 24, 2022

@erezsh Need input on the best path to take to solve the last remaining mypy error. Tree[str] is being given Tree[str] | str, basically.

@erezsh
Copy link
Member

erezsh commented May 24, 2022

I think all the return types should just be Tree, not TreeOrCode. It was only supposed to be used for function arguments, for convenience.

@henryiii henryiii marked this pull request as ready for review May 24, 2022 13:22
lark/lark.py Show resolved Hide resolved
- name: Lint with mypy
run: mypy -p lark || true
run: pipx run tox -e type
Copy link
Member

Choose a reason for hiding this comment

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

Magic!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I love that GHA supports pipx as a first-class package manager!


[testenv]
whitelist_externals = git
deps =
-rtest-requirements.txt
passenv =
TERM
Copy link
Member

Choose a reason for hiding this comment

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

What does this do?

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 allows mypy to detect that color is supported by the running terminal (well, assuming it is), and causes it to print colored output instead of uncolored output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nox doesn't filter environment variables, but Tox does. TERM shouldn't ever be filtered, since tools might check it to see what you are running in (like mypy does).

@erezsh
Copy link
Member

erezsh commented May 24, 2022

Looks good to me!

@MegaIng Might be nice to have another pair of eyes.

Copy link
Member

@MegaIng MegaIng left a comment

Choose a reason for hiding this comment

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

Looks really nice for the most part, just the annotations in tree_templates aren't quite correct I think.

@@ -55,7 +55,7 @@ def _get_tree(self, template: TreeOrCode) -> Tree[str]:
def __call__(self, template: Tree[str]) -> 'Template':
return Template(template, conf=self)

def _match_tree_template(self, template: TreeOrCode, tree: TreeOrCode) -> Optional[Dict[str, TreeOrCode]]:
def _match_tree_template(self, template: TreeOrCode, tree: Tree) -> Optional[Dict[str, Tree]]:
Copy link
Member

Choose a reason for hiding this comment

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

The annotation for the argument tree isn't quite correct based on the rest of the function, or the test template == tree in the if isinstance(template, str) case below is not needed.

Copy link
Member

Choose a reason for hiding this comment

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

I actually also think that this function will happily correctly return a Dict[str, Token]. It's not really TreeOrCode, it's TreeOrToken.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, the problem wasn't with these functions returning TreeOrCode (although it's a bit of a misnomer), it's the problem of everything else not accepting TreeOrToken as an input in the type signature.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, only the return types needed to change, the argument should have stayed TreeOrCode. I missed it.

It's actually not Token, because it accepts regular strings. But tokens are included in it.

Copy link
Member

Choose a reason for hiding this comment

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

@henryiii I think the only thing needed it to change back tree: Tree into tree: TreeOrCode

Copy link
Member

Choose a reason for hiding this comment

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

@MegaIng Hmm you may have a point!

Copy link
Member

Choose a reason for hiding this comment

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

I think that would mean this has to change instead:

    def apply_vars(self, vars: Mapping[str, Tree[str]]) -> Tree[str]:

into

    def apply_vars(self, vars: Mapping[str, TreeOrCode]) -> TreeOrCode:

And same with the _ReplaceVars class.

Copy link
Member

Choose a reason for hiding this comment

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

(@henryiii I don't mind fixing it up myself, if you prefer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it might need to be a bound TypeVar in at least a few places, since the input matches the output. translate does seem to require a tree, at least by the time it starts operating on .data and .children. Translate is also missing a return annotation.

I don't mind fixing it up myself, if you prefer

That might be best, I'll revert my change here, then you can edit the PR (or I can type ignore it, then you can follow up, whatever you like).

Copy link
Member

Choose a reason for hiding this comment

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

No problem, fixed it in #1150.

Sorry for the mixup! It's definitely tricky code, and I wasn't the one who originally added types to it. And in fact, it caught a real edge case!

@erezsh
Copy link
Member

erezsh commented May 24, 2022

Fixed tree_templates.py, see PR #1150

@erezsh
Copy link
Member

erezsh commented May 24, 2022

@henryiii You can merge my branch here, and then I'll merge this PR. Or you can close this PR and I'll just merge the new one. Whatever works for you.

@henryiii henryiii closed this May 24, 2022
@henryiii
Copy link
Contributor Author

You can merge yours, happy either way and not at a keyboard ATM.

erezsh added a commit that referenced this pull request May 24, 2022
@erezsh
Copy link
Member

erezsh commented May 24, 2022

No problem. Thanks for the contribution!

@henryiii henryiii deleted the henryiii/fix/types branch May 24, 2022 19:14
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.

None yet

3 participants