Skip to content

mypy and pylint disagree about uselessness of return statements #6530

Closed
@clo-vis

Description

@clo-vis

Bug description

from contextlib import AbstractContextManager
from types import TracebackType
from typing import Optional, Type


class Playground(AbstractContextManager["Playground"]):
    def __exit__(
        self,
        exc_type: Optional[Type[BaseException]],
        exc_value: Optional[BaseException],
        traceback: Optional[TracebackType],
    ) -> Optional[bool]:
        print("closing what needs to be closed")
        return None

Configuration

No response

Command used

pylint playground.py

Pylint output

src\playground.py:7:4: R1711: Useless return at end of function or method (useless-return)

Expected behavior

No error message, because without return None mypy complains:

error: Missing return statement [return]

And mypy swears that they are right, see for example python/mypy#3974

(By the way, the signature from exit was copied from https://mypy.readthedocs.io/en/stable/protocols.html#contextmanager-t)

Pylint version

pylint 2.13.8
astroid 2.11.4
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions