From 82c4db11bed46ffa8c78d8c678da81ac68999ecb Mon Sep 17 00:00:00 2001 From: jiang1997 Date: Sun, 28 Aug 2022 21:08:02 +0800 Subject: [PATCH] style: deal with B024 added to flake8 recently More details about B024 here https://github.com/PyCQA/flake8-bugbear/pull/274 * ./skywalking/agent/protocol/__init__.py:22:1: B024 Protocol is an abstract base class, but it has no abstract methods. Set 'heartbeat', 'report' and 'report_log' as abstract method * ./skywalking/trace/span.py:36:1: B024 Span is an abstract base class, but it has no abstract methods. Prevent base class Span from being directly instantiated by raising error in __new__ with reference to https://stackoverflow.com/questions/7989042/preventing-a-class-from-direct-instantiation-in-python * ./tests/plugin/base.py:35:1: B024 TestPluginBase is an abstract base class Due to pytest may instantiate parent class for searching test functions, TestPluginBase has to be instantiable. --- skywalking/agent/__init__.py | 2 +- skywalking/agent/protocol/__init__.py | 5 ++++- skywalking/trace/span.py | 8 ++++++-- tests/plugin/base.py | 3 +-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/skywalking/agent/__init__.py b/skywalking/agent/__init__.py index f487e9e91..d2fd39373 100644 --- a/skywalking/agent/__init__.py +++ b/skywalking/agent/__init__.py @@ -35,7 +35,7 @@ from skywalking.trace.context import Segment __started = False -__protocol = Protocol() # type: Protocol +__protocol = None # type: Protocol __heartbeat_thread = __report_thread = __log_report_thread = __query_profile_thread = __command_dispatch_thread \ = __send_profile_thread = __queue = __log_queue = __snapshot_queue = __finished = None diff --git a/skywalking/agent/protocol/__init__.py b/skywalking/agent/protocol/__init__.py index 24d4c3d0c..ffe09ed1a 100644 --- a/skywalking/agent/protocol/__init__.py +++ b/skywalking/agent/protocol/__init__.py @@ -15,7 +15,7 @@ # limitations under the License. # -from abc import ABC +from abc import ABC, abstractmethod from queue import Queue @@ -29,12 +29,15 @@ def fork_after_in_parent(self): def fork_after_in_child(self): pass + @abstractmethod def heartbeat(self): raise NotImplementedError() + @abstractmethod def report(self, queue: Queue, block: bool = True): raise NotImplementedError() + @abstractmethod def report_log(self, queue: Queue, block: bool = True): raise NotImplementedError() diff --git a/skywalking/trace/span.py b/skywalking/trace/span.py index cea35bb98..8bf007d04 100644 --- a/skywalking/trace/span.py +++ b/skywalking/trace/span.py @@ -16,7 +16,6 @@ # import time -from abc import ABC from collections import defaultdict from typing import List, Union, DefaultDict from typing import TYPE_CHECKING @@ -33,7 +32,12 @@ @tostring -class Span(ABC): +class Span: + def __new__(cls, *args, **kwargs): + if cls is Span: + raise TypeError(f"only children of '{cls.__name__}' may be instantiated") + return object.__new__(cls, *args, **kwargs) + def __init__( self, context: 'SpanContext', diff --git a/tests/plugin/base.py b/tests/plugin/base.py index 5817b02c9..b61ece0b4 100644 --- a/tests/plugin/base.py +++ b/tests/plugin/base.py @@ -18,7 +18,6 @@ import inspect import os import sys -from abc import ABC from difflib import Differ from os.path import dirname @@ -32,7 +31,7 @@ from yaml import SafeLoader as Loader -class TestPluginBase(ABC): +class TestPluginBase: def validate(self, expected_file_name=None): # type: (str) -> Response