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

Syntax error when docstring proceeds comment with type annotation #36

Open
JakubTesarek opened this issue May 18, 2021 · 3 comments
Open

Comments

@JakubTesarek
Copy link

When a function in source file contains both type annotation comment and docstring, but in wrong order, com2ann fails with SyntaxError.

Expected behaviour:

  • When the type comment is placed after docstring, it should be considered to be a basic inline comment and skipped.
  • Com2ann should skip the function and process the rest of the file.
  • The comment should be left in place so users can fix it themselves
  • Ideally there should an warning in the output informing user there was a function that was skipped due to misplaced type comment.

How to reproduce:
Create file test.py:

class Klass:
    def function(self, parameter):
        """Comment"""
        # type: (str) -> str
        return parameter

Call com2ann: com2ann test.py

Output:

File: test.py
SyntaxError in test.py
@JakubTesarek
Copy link
Author

I'm willing to implement this

@ilevkivskyi
Copy link
Owner

Yeah, skipping such cases with a warning makes sense. Please go ahead with a PR.

@JakubTesarek
Copy link
Author

Unfortunately it seems I won't be able to fix it. There's a quirk in the Python tokenizer that makes it that any comment starting with # type is considered to be a type comment: python/typed_ast#62

The only option to make ast.parse to work with this is to disable parsing with type comments which obviously is not an options. At this point I think there are two options:

  1. Start reporting the line on which the syntax error was found. That would mean reworking the com2ann function so it reports the errors itself. It would mean refactoring of majority of the tests.
  2. Since we know on which line the syntax error was found, we can look if it's caused by a misplaced type comment. If it is, we can replace it with something else that would not trigger the error and try again. This would fix only one type of error and I was hoping to fix all the false syntax errors at once.

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

No branches or pull requests

2 participants