From 9b08b3731d2b6b8dfb23ff17aefc50721bfe621a Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Mon, 2 Nov 2020 19:02:07 +0000 Subject: [PATCH 1/2] remove remaining checks and type comments for python3.4 --- docs/examples/opentelemetry-example-app/setup.cfg | 1 - .../opentelemetry/exporter/datadog/spanprocessor.py | 4 +--- opentelemetry-api/setup.cfg | 3 +-- .../src/opentelemetry/configuration/__init__.py | 6 +++--- .../src/opentelemetry/context/__init__.py | 6 +++--- .../src/opentelemetry/context/threadlocal_context.py | 4 +--- .../src/opentelemetry/sdk/trace/__init__.py | 10 +++++----- .../src/opentelemetry/sdk/trace/export/__init__.py | 12 +++++------- .../src/opentelemetry/sdk/util/__init__.py | 6 +++--- 9 files changed, 22 insertions(+), 30 deletions(-) diff --git a/docs/examples/opentelemetry-example-app/setup.cfg b/docs/examples/opentelemetry-example-app/setup.cfg index 8e55d321d4f..552bf7cef74 100644 --- a/docs/examples/opentelemetry-example-app/setup.cfg +++ b/docs/examples/opentelemetry-example-app/setup.cfg @@ -41,7 +41,6 @@ packages = find_namespace: zip_safe = False include_package_data = True install_requires = - typing; python_version<'3.5' opentelemetry-api == 0.16.dev0 opentelemetry-sdk == 0.16.dev0 opentelemetry-instrumentation-requests == 0.16.dev0 diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py index 3a1188e0bd3..11971bbfdb3 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py @@ -53,9 +53,7 @@ def __init__( # queue trace_ids for traces with recently ended spans for worker thread to check # for exporting - self.check_traces_queue = ( - collections.deque() - ) # type: typing.Deque[int] + self.check_traces_queue: typing.Deque[int] = (collections.deque()) self.traces_lock = threading.Lock() # dictionary of trace_ids to a list of spans where the first span is the diff --git a/opentelemetry-api/setup.cfg b/opentelemetry-api/setup.cfg index 4ad44c64519..c697e308546 100644 --- a/opentelemetry-api/setup.cfg +++ b/opentelemetry-api/setup.cfg @@ -41,8 +41,7 @@ packages=find_namespace: zip_safe = False include_package_data = True install_requires = - typing; python_version<'3.5' - aiocontextvars; python_version<'3.7' and python_version>='3.5' + aiocontextvars; python_version<'3.7' [options.packages.find] where = src diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py index bf641ca836d..55e98e4be88 100644 --- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py +++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py @@ -102,8 +102,8 @@ class Configuration: - _instance = None # type: ClassVar[Optional[Configuration]] - _config_map = {} # type: ClassVar[Dict[str, ConfigValue]] + _instance: ClassVar[Optional["Configuration"]] = None + _config_map: ClassVar[Dict[str, ConfigValue]] = {} def __new__(cls) -> "Configuration": if cls._instance is not None: @@ -118,7 +118,7 @@ def __new__(cls) -> "Configuration": if match is not None: key = match.group(2) - value = value_str # type: ConfigValue + value: ConfigValue = value_str if value_str == "True": value = True diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 22441c306b4..16f84163b87 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -23,7 +23,7 @@ from opentelemetry.context.context import Context, RuntimeContext logger = logging.getLogger(__name__) -_RUNTIME_CONTEXT = None # type: typing.Optional[RuntimeContext] +_RUNTIME_CONTEXT: typing.Optional[RuntimeContext] = None _RUNTIME_CONTEXT_LOCK = threading.Lock() _F = typing.TypeVar("_F", bound=typing.Callable[..., typing.Any]) @@ -49,9 +49,9 @@ def wrapper( # to get configuration values straight from environment variables default_context = "contextvars_context" - configured_context = environ.get( + configured_context: str = environ.get( "OTEL_CONTEXT", default_context - ) # type: str + ) try: _RUNTIME_CONTEXT = next( iter_entry_points( diff --git a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py b/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py index 43e9fb7ce9e..900d5d68b65 100644 --- a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py +++ b/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py @@ -44,9 +44,7 @@ def get_current(self) -> Context: setattr( self._current_context, self._CONTEXT_KEY, Context(), ) - context = getattr( - self._current_context, self._CONTEXT_KEY - ) # type: Context + context: Context = getattr(self._current_context, self._CONTEXT_KEY) return context def detach(self, token: object) -> None: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index c0189e807e7..593019aeba3 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -121,7 +121,7 @@ class SynchronousMultiSpanProcessor(SpanProcessor): def __init__(self): # use a tuple to avoid race conditions when adding a new span and # iterating through it on "on_start" and "on_end". - self._span_processors = () # type: Tuple[SpanProcessor, ...] + self._span_processors: Tuple[SpanProcessor, ...] = () self._lock = threading.Lock() def add_span_processor(self, span_processor: SpanProcessor) -> None: @@ -189,7 +189,7 @@ class ConcurrentMultiSpanProcessor(SpanProcessor): def __init__(self, num_threads: int = 2): # use a tuple to avoid race conditions when adding a new span and # iterating through it on "on_start" and "on_end". - self._span_processors = () # type: Tuple[SpanProcessor, ...] + self._span_processors: Tuple[SpanProcessor, ...] = () self._lock = threading.Lock() self._executor = concurrent.futures.ThreadPoolExecutor( max_workers=num_threads @@ -241,7 +241,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool: timeout, False otherwise. """ futures = [] - for sp in self._span_processors: # type: SpanProcessor + for sp in self._span_processors: future = self._executor.submit(sp.force_flush, timeout_millis) futures.append(future) @@ -465,8 +465,8 @@ def __init__( else: self.links = BoundedList.from_seq(MAX_NUM_LINKS, links) - self._end_time = None # type: Optional[int] - self._start_time = None # type: Optional[int] + self._end_time: Optional[int] = None + self._start_time: Optional[int] = None self.instrumentation_info = instrumentation_info @property diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 53f85dcbb2e..b0f04616044 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -155,12 +155,10 @@ def __init__( ) self.span_exporter = span_exporter - self.queue = collections.deque( - [], max_queue_size - ) # type: typing.Deque[Span] + self.queue: typing.Deque[Span] = collections.deque([], max_queue_size) self.worker_thread = threading.Thread(target=self.worker, daemon=True) self.condition = threading.Condition(threading.Lock()) - self._flush_request = None # type: typing.Optional[_FlushRequest] + self._flush_request: typing.Optional[_FlushRequest] = None self.schedule_delay_millis = schedule_delay_millis self.max_export_batch_size = max_export_batch_size self.max_queue_size = max_queue_size @@ -169,9 +167,9 @@ def __init__( # flag that indicates that spans are being dropped self._spans_dropped = False # precallocated list to send spans to exporter - self.spans_list = [ + self.spans_list: typing.List[typing.Optional[Span]] = [ None - ] * self.max_export_batch_size # type: typing.List[typing.Optional[Span]] + ] * self.max_export_batch_size self.worker_thread.start() def on_start( @@ -198,7 +196,7 @@ def on_end(self, span: Span) -> None: def worker(self): timeout = self.schedule_delay_millis / 1e3 - flush_request = None # type: typing.Optional[_FlushRequest] + flush_request: typing.Optional[_FlushRequest] = None while not self.done: with self.condition: if self.done: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py index 09d7283cab7..3c1f0ad9539 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py @@ -54,7 +54,7 @@ class BoundedList(Sequence): def __init__(self, maxlen): self.dropped = 0 - self._dq = deque(maxlen=maxlen) # type: deque + self._dq: deque = deque(maxlen=maxlen) self._lock = threading.Lock() def __repr__(self): @@ -110,8 +110,8 @@ def __init__(self, maxlen): raise ValueError self.maxlen = maxlen self.dropped = 0 - self._dict = OrderedDict() # type: OrderedDict - self._lock = threading.Lock() # type: threading.Lock + self._dict: OrderedDict = OrderedDict() + self._lock: threading.Lock = threading.Lock() def __repr__(self): return "{}({}, maxlen={})".format( From da360607e0ac97223567e8b21b9b9c8182dac295 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Mon, 2 Nov 2020 20:01:35 +0000 Subject: [PATCH 2/2] remove variable annotation changes, that is still not supported in python3.5 :( --- .../opentelemetry/exporter/datadog/spanprocessor.py | 4 +++- .../src/opentelemetry/configuration/__init__.py | 6 +++--- .../src/opentelemetry/context/__init__.py | 6 +++--- .../src/opentelemetry/context/threadlocal_context.py | 4 +++- .../src/opentelemetry/sdk/trace/__init__.py | 10 +++++----- .../src/opentelemetry/sdk/trace/export/__init__.py | 12 +++++++----- .../src/opentelemetry/sdk/util/__init__.py | 6 +++--- 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py index 11971bbfdb3..3a1188e0bd3 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/spanprocessor.py @@ -53,7 +53,9 @@ def __init__( # queue trace_ids for traces with recently ended spans for worker thread to check # for exporting - self.check_traces_queue: typing.Deque[int] = (collections.deque()) + self.check_traces_queue = ( + collections.deque() + ) # type: typing.Deque[int] self.traces_lock = threading.Lock() # dictionary of trace_ids to a list of spans where the first span is the diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py index 55e98e4be88..bf641ca836d 100644 --- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py +++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py @@ -102,8 +102,8 @@ class Configuration: - _instance: ClassVar[Optional["Configuration"]] = None - _config_map: ClassVar[Dict[str, ConfigValue]] = {} + _instance = None # type: ClassVar[Optional[Configuration]] + _config_map = {} # type: ClassVar[Dict[str, ConfigValue]] def __new__(cls) -> "Configuration": if cls._instance is not None: @@ -118,7 +118,7 @@ def __new__(cls) -> "Configuration": if match is not None: key = match.group(2) - value: ConfigValue = value_str + value = value_str # type: ConfigValue if value_str == "True": value = True diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 16f84163b87..22441c306b4 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -23,7 +23,7 @@ from opentelemetry.context.context import Context, RuntimeContext logger = logging.getLogger(__name__) -_RUNTIME_CONTEXT: typing.Optional[RuntimeContext] = None +_RUNTIME_CONTEXT = None # type: typing.Optional[RuntimeContext] _RUNTIME_CONTEXT_LOCK = threading.Lock() _F = typing.TypeVar("_F", bound=typing.Callable[..., typing.Any]) @@ -49,9 +49,9 @@ def wrapper( # to get configuration values straight from environment variables default_context = "contextvars_context" - configured_context: str = environ.get( + configured_context = environ.get( "OTEL_CONTEXT", default_context - ) + ) # type: str try: _RUNTIME_CONTEXT = next( iter_entry_points( diff --git a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py b/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py index 900d5d68b65..43e9fb7ce9e 100644 --- a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py +++ b/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py @@ -44,7 +44,9 @@ def get_current(self) -> Context: setattr( self._current_context, self._CONTEXT_KEY, Context(), ) - context: Context = getattr(self._current_context, self._CONTEXT_KEY) + context = getattr( + self._current_context, self._CONTEXT_KEY + ) # type: Context return context def detach(self, token: object) -> None: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 593019aeba3..c0189e807e7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -121,7 +121,7 @@ class SynchronousMultiSpanProcessor(SpanProcessor): def __init__(self): # use a tuple to avoid race conditions when adding a new span and # iterating through it on "on_start" and "on_end". - self._span_processors: Tuple[SpanProcessor, ...] = () + self._span_processors = () # type: Tuple[SpanProcessor, ...] self._lock = threading.Lock() def add_span_processor(self, span_processor: SpanProcessor) -> None: @@ -189,7 +189,7 @@ class ConcurrentMultiSpanProcessor(SpanProcessor): def __init__(self, num_threads: int = 2): # use a tuple to avoid race conditions when adding a new span and # iterating through it on "on_start" and "on_end". - self._span_processors: Tuple[SpanProcessor, ...] = () + self._span_processors = () # type: Tuple[SpanProcessor, ...] self._lock = threading.Lock() self._executor = concurrent.futures.ThreadPoolExecutor( max_workers=num_threads @@ -241,7 +241,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool: timeout, False otherwise. """ futures = [] - for sp in self._span_processors: + for sp in self._span_processors: # type: SpanProcessor future = self._executor.submit(sp.force_flush, timeout_millis) futures.append(future) @@ -465,8 +465,8 @@ def __init__( else: self.links = BoundedList.from_seq(MAX_NUM_LINKS, links) - self._end_time: Optional[int] = None - self._start_time: Optional[int] = None + self._end_time = None # type: Optional[int] + self._start_time = None # type: Optional[int] self.instrumentation_info = instrumentation_info @property diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index b0f04616044..53f85dcbb2e 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -155,10 +155,12 @@ def __init__( ) self.span_exporter = span_exporter - self.queue: typing.Deque[Span] = collections.deque([], max_queue_size) + self.queue = collections.deque( + [], max_queue_size + ) # type: typing.Deque[Span] self.worker_thread = threading.Thread(target=self.worker, daemon=True) self.condition = threading.Condition(threading.Lock()) - self._flush_request: typing.Optional[_FlushRequest] = None + self._flush_request = None # type: typing.Optional[_FlushRequest] self.schedule_delay_millis = schedule_delay_millis self.max_export_batch_size = max_export_batch_size self.max_queue_size = max_queue_size @@ -167,9 +169,9 @@ def __init__( # flag that indicates that spans are being dropped self._spans_dropped = False # precallocated list to send spans to exporter - self.spans_list: typing.List[typing.Optional[Span]] = [ + self.spans_list = [ None - ] * self.max_export_batch_size + ] * self.max_export_batch_size # type: typing.List[typing.Optional[Span]] self.worker_thread.start() def on_start( @@ -196,7 +198,7 @@ def on_end(self, span: Span) -> None: def worker(self): timeout = self.schedule_delay_millis / 1e3 - flush_request: typing.Optional[_FlushRequest] = None + flush_request = None # type: typing.Optional[_FlushRequest] while not self.done: with self.condition: if self.done: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py index 3c1f0ad9539..09d7283cab7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py @@ -54,7 +54,7 @@ class BoundedList(Sequence): def __init__(self, maxlen): self.dropped = 0 - self._dq: deque = deque(maxlen=maxlen) + self._dq = deque(maxlen=maxlen) # type: deque self._lock = threading.Lock() def __repr__(self): @@ -110,8 +110,8 @@ def __init__(self, maxlen): raise ValueError self.maxlen = maxlen self.dropped = 0 - self._dict: OrderedDict = OrderedDict() - self._lock: threading.Lock = threading.Lock() + self._dict = OrderedDict() # type: OrderedDict + self._lock = threading.Lock() # type: threading.Lock def __repr__(self): return "{}({}, maxlen={})".format(