admin管理员组

文章数量:1122846

I have an abstract method which just includes a pass statement, as the method implementation is in another class that inherits from this abstract class. Here is the method:

@abstractmethod
def parse_sun_times(self, times_as_strings: Dict[str, str]) -> SunTimes:
    """
    Parses a dictionary of time strings into a SunTimes object.

    Args:
        times_as_strings (Dict[str, str]): A dictionary containing time strings as values and
        sun phases as keys

    Returns:
        SunTimes: An object representing parsed sunrise, sunset, and twilight times.
    """
    pass

Pylint is giving the warning "unnecessary pass statement". Is there some way I can signify to pylint that this is an abstract method, or do I just have to disable the warning manually?

I have an abstract method which just includes a pass statement, as the method implementation is in another class that inherits from this abstract class. Here is the method:

@abstractmethod
def parse_sun_times(self, times_as_strings: Dict[str, str]) -> SunTimes:
    """
    Parses a dictionary of time strings into a SunTimes object.

    Args:
        times_as_strings (Dict[str, str]): A dictionary containing time strings as values and
        sun phases as keys

    Returns:
        SunTimes: An object representing parsed sunrise, sunset, and twilight times.
    """
    pass

Pylint is giving the warning "unnecessary pass statement". Is there some way I can signify to pylint that this is an abstract method, or do I just have to disable the warning manually?

Share Improve this question edited Nov 22, 2024 at 18:02 imad97 asked Nov 22, 2024 at 17:32 imad97imad97 2661 silver badge12 bronze badges 8
  • 1 Does the same error occur if you use ... instead of pass? – Alex Duchnowski Commented Nov 22, 2024 at 17:41
  • Yes, I get the unnecessary ellipses constant warning – imad97 Commented Nov 22, 2024 at 17:44
  • 1 The docs appear to assume you already have a docstring as the first element after the signature (which would explain the recommendation to simply remove it, definition would still be syntactically complete) – Mathias R. Jessen Commented Nov 22, 2024 at 17:50
  • 1 And I presume that the pylint warning only comes when you have that docstring. I can't believe it would recommend invalid syntax. – Barmar Commented Nov 22, 2024 at 17:52
  • 1 Yes, this is correct! Removing the docstring removed the warning, and vice versa with the pass statement! – imad97 Commented Nov 22, 2024 at 18:00
 |  Show 3 more comments

1 Answer 1

Reset to default 3

As mentioned in Mathias' comment, because there is a docstring as the first element after the signature, the definition is syntactically complete without the need for a pass statement. To remove the warning, either remove the docstring or the pass statement, as it is unnecessary to include both.

本文标签: pythonPylint unnecessary pass statement for abstract methodStack Overflow