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

"inline" instruction inside docstring/comments #6991

Closed
gantsevdenis opened this issue Jun 13, 2019 · 2 comments
Closed

"inline" instruction inside docstring/comments #6991

gantsevdenis opened this issue Jun 13, 2019 · 2 comments

Comments

@gantsevdenis
Copy link
Contributor

Current behavior is following:

from typing import Optional


def is_not_none(x)-> bool:
    """
    mypy:inline
    """
    return x is not None  # mypy:inline


# sample.py:16: error: Unsupported left operand type for + ("None")
# sample.py:16: note: Left operand is of type "Optional[str]"

def being_tested(x: Optional[str]) -> str:
    if is_not_none(x):
        return x + "a"
    return ""

The variable "x" can't be None, because the check is performed outside.

I don't know how difficult it would be to retrieve the instruction "inline" inside the docstring, and perform on-the fly inlining of the function body, whenever it is encountered. I
The "inline" instruction can be in comments.
So here the modified version would look like:

def being_tested(x: Optional[str]) -> str:
    if (x is not None):
        return x + "a"
    return ""

Only one-liners would be candidates for inlining, the first and the only expression after the return statement.
This would solve the false error above.
I am not sure however how line numbers would be handled, when reporting in the console.

Does this suggestion even makes sens?

@ilevkivskyi
Copy link
Member

I think a decorator like @inline would be a better syntax. Anyway, I think this may be tricky and I rather prefer better support for literal types + NoReturn, see e.g. #5206 and #4063

@ilevkivskyi ilevkivskyi changed the title [Feature Request] "inline" instruction inside docstring/comments "inline" instruction inside docstring/comments Jun 17, 2019
@hauntsaninja
Copy link
Collaborator

PEP 647 can also do things like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants