Skip to content

Commit

Permalink
make protocols be defined as abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Nov 24, 2022
1 parent ed404d3 commit 27e56c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7209.false_positive
@@ -0,0 +1,3 @@
Fixes false positive ``abstract-method`` on Protocol classes.

Closes #7209
4 changes: 4 additions & 0 deletions pylint/checkers/utils.py
Expand Up @@ -1167,6 +1167,10 @@ def class_is_abstract(node: nodes.ClassDef) -> bool:
"""Return true if the given class node should be considered as an abstract
class.
"""
# Protocol classes are considered "abstract"
if is_protocol_class(node):
return True

# Only check for explicit metaclass=ABCMeta on this specific class
meta = node.declared_metaclass()
if meta is not None:
Expand Down
36 changes: 36 additions & 0 deletions tests/functional/p/protocol_classes_abstract.py
@@ -0,0 +1,36 @@
"""Test that classes inheriting from protocols should not warn about abstract-method."""
# pylint: disable=too-few-public-methods,disallowed-name,invalid-name
from abc import abstractmethod
from typing import Protocol, Literal


class FooProtocol(Protocol):
"""Foo Protocol"""

@abstractmethod
def foo(self) -> Literal["foo"]:
"""foo method"""

def foo_no_abstract(self) -> Literal["foo"]:
"""foo not abstract method"""

class BarProtocol(Protocol):
"""Bar Protocol"""
@abstractmethod
def bar(self) -> Literal["bar"]:
"""bar method"""



class FooBarProtocol(FooProtocol, BarProtocol, Protocol):
"""FooBar Protocol"""


class FooBar(FooBarProtocol):
"""FooBar object"""

def bar(self) -> Literal["bar"]:
return "bar"

def foo(self) -> Literal["foo"]:
return "foo"

0 comments on commit 27e56c4

Please sign in to comment.