mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-30 18:13:36 +07:00
A class containing at least one method declared with `abstractmethod` decorator that hasn’t been overridden yet cannot be instantiated. Also report instantiation of classes that directly inherit ABC or have metaclass set to ABCMeta. GitOrigin-RevId: 55cb4dc90c55ddc63991a4c3f6b50b4e34a3b4bd
120 lines
1.7 KiB
Python
120 lines
1.7 KiB
Python
from typing import Protocol
|
|
from abc import abstractmethod
|
|
|
|
|
|
# ------------------
|
|
class MP1(Protocol):
|
|
name: str
|
|
|
|
|
|
class CMP1(MP1): # ok
|
|
pass
|
|
|
|
|
|
def test_mp1(mp1: MP1):
|
|
pass
|
|
|
|
|
|
test_mp1(CMP1())
|
|
|
|
|
|
# ------------------
|
|
class MP2(Protocol):
|
|
name: str = "name"
|
|
|
|
|
|
class CMP2(MP2): # ok
|
|
pass
|
|
|
|
|
|
def test_mp2(mp2: MP2):
|
|
pass
|
|
|
|
|
|
test_mp2(CMP2())
|
|
|
|
|
|
# ------------------
|
|
class MP3(Protocol):
|
|
def foo(self) -> int:
|
|
return 42
|
|
|
|
|
|
class CMP3(MP3): # ok
|
|
pass
|
|
|
|
|
|
def test_mp3(mp3: MP3):
|
|
pass
|
|
|
|
|
|
test_mp3(CMP3())
|
|
|
|
|
|
# ------------------
|
|
class MP4(Protocol):
|
|
def foo(self) -> int:
|
|
pass
|
|
|
|
|
|
class CMP4(MP4): # ok
|
|
pass
|
|
|
|
|
|
def test_mp4(mp4: MP4):
|
|
pass
|
|
|
|
|
|
test_mp4(CMP4())
|
|
|
|
|
|
# ------------------
|
|
class MP5(Protocol):
|
|
@abstractmethod
|
|
def foo(self) -> int:
|
|
raise NotImplementedError
|
|
|
|
|
|
class <weak_warning descr="Class CMP5 must implement all abstract methods">CMP5</weak_warning>(MP5): # fail
|
|
pass
|
|
|
|
|
|
def test_mp5(mp5: MP5):
|
|
pass
|
|
|
|
|
|
test_mp5(<warning descr="Cannot instantiate abstract class 'CMP5'">CMP5()</warning>)
|
|
|
|
|
|
# ------------------
|
|
class MP6(Protocol):
|
|
@abstractmethod
|
|
def foo(self) -> int:
|
|
raise NotImplementedError
|
|
|
|
|
|
class CMP6(MP6): # ok
|
|
@abstractmethod
|
|
def bar(self) -> int:
|
|
raise NotImplementedError
|
|
|
|
|
|
def test_mp6(mp6: MP6):
|
|
pass
|
|
|
|
|
|
test_mp6(<warning descr="Cannot instantiate abstract class 'CMP6'">CMP6()</warning>)
|
|
|
|
|
|
# ------------------
|
|
class MP7(Protocol):
|
|
@abstractmethod
|
|
def foo(self) -> int:
|
|
raise NotImplementedError
|
|
|
|
|
|
class PMP7(MP7, Protocol): # ok
|
|
def bar(self) -> int:
|
|
pass
|
|
|
|
<warning descr="Cannot instantiate abstract class 'PMP7'">PMP7()</warning> |