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

Make semantic model aware of docstring #9960

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Feb 12, 2024

Summary

This PR introduces a new semantic model flag DOCSTRING which suggests that the model is currently in a module / class / function docstring. This is the first step in eliminating the docstring detection state machine which is prone to bugs as stated in #7595.

Test Plan

TODO: Is there a way to add a test case for this?

I tested this using the following code snippet and adding a print statement in the string_like analyzer to print if we're currently in a docstring or not.

Test code snippet:

"Docstring" ", still a docstring"
"Not a docstring"


def foo():
    "Docstring"
    "Not a docstring"
    if foo:
        "Not a docstring"
        pass


class Foo:
    "Docstring"
    "Not a docstring"

    foo: int
    "Unofficial variable docstring"

    def method():
        "Docstring"
        "Not a docstring"
        pass


def bar():
    "Not a docstring".strip()


def baz():
    _something_else = 1
    """Not a docstring"""

@dhruvmanila dhruvmanila added the docstring Related to docstring linting or formatting label Feb 12, 2024
@dhruvmanila dhruvmanila changed the title Rename semantic model flag to MODULE_DOCSTRING_BOUNDARY Make semantic model aware of docstring Feb 12, 2024
Base automatically changed from dhruv/rename-model-flag to main February 12, 2024 19:17
@dhruvmanila dhruvmanila force-pushed the dhruv/semantic-model-docstring branch from 378d3ad to f8dead6 Compare February 12, 2024 19:17
Copy link
Contributor

github-actions bot commented Feb 12, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dhruvmanila dhruvmanila force-pushed the dhruv/semantic-model-docstring branch from f8dead6 to 7a93265 Compare February 13, 2024 04:14
@dhruvmanila dhruvmanila enabled auto-merge (squash) February 13, 2024 04:15
@dhruvmanila dhruvmanila disabled auto-merge February 13, 2024 04:18
@dhruvmanila dhruvmanila force-pushed the dhruv/semantic-model-docstring branch from 7a93265 to b9a4c51 Compare February 13, 2024 04:19
@dhruvmanila dhruvmanila enabled auto-merge (squash) February 13, 2024 04:19
@dhruvmanila dhruvmanila merged commit 180920f into main Feb 13, 2024
16 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/semantic-model-docstring branch February 13, 2024 04:26
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

This PR introduces a new semantic model flag `DOCSTRING` which suggests
that the model is currently in a module / class / function docstring.
This is the first step in eliminating the docstring detection state
machine which is prone to bugs as stated in astral-sh#7595.

## Test Plan

~TODO: Is there a way to add a test case for this?~

I tested this using the following code snippet and adding a print
statement in the `string_like` analyzer to print if we're currently in a
docstring or not.

<details><summary>Test code snippet:</summary>
<p>

```python
"Docstring" ", still a docstring"
"Not a docstring"


def foo():
    "Docstring"
    "Not a docstring"
    if foo:
        "Not a docstring"
        pass


class Foo:
    "Docstring"
    "Not a docstring"

    foo: int
    "Unofficial variable docstring"

    def method():
        "Docstring"
        "Not a docstring"
        pass


def bar():
    "Not a docstring".strip()


def baz():
    _something_else = 1
    """Not a docstring"""
```

</p>
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docstring Related to docstring linting or formatting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants