sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.55k stars 70 forks source link

Code highlighted in wrong place after saving a file #272

Closed ghost closed 2 years ago

ghost commented 2 years ago

Checklist

Description

I added a custom rule in .sourcery.yaml which looks like this:

rules:
  - id: do-not-use-print-in-production-code
    description: Do not use print statements in production code.
    pattern: print

When I now start VSCode, the squiggly blue line is underneath the printstatement. But when I now save the file, the squiggly blue line is right after the class definition:

Bildschirmfoto vom 2022-09-04 19-49-42

Debug Information

IDE Version: VSCode 1.71.0

Sourcery Version: 0.12.7

Operating system and Version: Ubuntu 22.04

reka commented 2 years ago

Hi @Ascii67 ,

Thanks a lot for reporting this :bug: We're working on a fix.

I managed to reproduce this :bug: for classes that have a multiline docstring. Did you observe it in other scenarios as well?

The 2 cases I've found so far:

Case A) There's an empty line between the class definition and the docstring => This empty line gets underlined.

class Example:

    """
    Example class to reproduce the bug. 

    https://github.com/sourcery-ai/sourcery/issues/272
    """

    def placeholder(self):
        print("anything")

Case B) No empty line between the class definition and the docstring, but an empty line between the docstring and the 1st method => The empty line between the docstring and the 1st method gets underlined.

class Example:
    """
    Example class to reproduce the bug. 

    https://github.com/sourcery-ai/sourcery/issues/272
    """

    def placeholder(self):
        print("anything")
reka commented 2 years ago

We're glad to hear that you're creating custom rules. :smiley: Do you have some feedback for us about them (besides this :bug:)?

For detecting calls to print in production code, another possibility is:

pattern: print(...)

This flags only calls to print, but doesn't flag other occurrences of print, e.g.:

alias_for_print = print

See also the Flag print Statements recipe in our docs.

ghost commented 2 years ago

Thanks for your answer, @reka!

I managed to reproduce this bug for classes that have a multiline docstring. Did you observe it in other scenarios as well?

No, I just observed this in scenarios where I have a class with a multiline docstring with this structure:

class ClassName:

    """
    This is the docstring.
    """

    def __init__(self) -> None:
        ...

Do you have some feedback for us about them (besides this bug)?

Since I've only just started with custom rules, I don't have any feedback yet but I didn't encounter any problems regarding custom rules and had a great experience with it!

Hellebore commented 2 years ago

Should be resolved with release of 0.12.8 - let us know if there are cases which aren't.