From d88764c5a50e0a71b4af6c151dc3a508dae826bd Mon Sep 17 00:00:00 2001 From: Yun Kim <35776586+Yun-Kim@users.noreply.github.com> Date: Tue, 27 Sep 2022 11:30:35 -0400 Subject: [PATCH] fix(typing): update types to be compatible with latest mypy (#4234) * Initial mypy fixes for CI * Ignored mypy error with type and type constructors * Fix formatting * Fixed incorrect typing * Undo initial change Co-authored-by: Kyle Verhoog (cherry picked from commit 5140de462beef227bdf0db3bf690e4a6d543e365) --- ddtrace/debugging/_debugger.py | 4 ++-- ddtrace/internal/atexit.py | 2 +- ddtrace/internal/runtime/runtime_metrics.py | 2 +- ddtrace/internal/utils/cache.py | 9 ++++----- ddtrace/internal/writer.py | 2 +- ddtrace/profiling/collector/__init__.py | 2 +- ddtrace/profiling/collector/_lock.py | 6 +++--- ddtrace/profiling/collector/asyncio.py | 2 +- ddtrace/profiling/collector/memalloc.py | 14 ++++++++++---- ddtrace/profiling/exporter/http.py | 5 ++++- ddtrace/profiling/profiler.py | 4 ++-- ddtrace/profiling/scheduler.py | 6 ++++-- 12 files changed, 34 insertions(+), 24 deletions(-) diff --git a/ddtrace/debugging/_debugger.py b/ddtrace/debugging/_debugger.py index 5dea4870176..1c634ffba1d 100644 --- a/ddtrace/debugging/_debugger.py +++ b/ddtrace/debugging/_debugger.py @@ -596,14 +596,14 @@ def _on_configuration(self, event, probes): else: raise ValueError("Unknown probe poller event %r" % event) - def _stop_service(self): # type: ignore[override] + def _stop_service(self): # type: () -> None self._function_store.restore_all() for service in self._services: service.stop() service.join() - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: () -> None for service in self._services: service.start() diff --git a/ddtrace/internal/atexit.py b/ddtrace/internal/atexit.py index d7d308cdd20..0d1e196be61 100644 --- a/ddtrace/internal/atexit.py +++ b/ddtrace/internal/atexit.py @@ -45,7 +45,7 @@ def register( return func def unregister(func): - # type: (typing.Callable[..., None]) -> None + # type: (typing.Callable[..., typing.Any]) -> None """ Unregister an exit function which was previously registered using atexit.register. diff --git a/ddtrace/internal/runtime/runtime_metrics.py b/ddtrace/internal/runtime/runtime_metrics.py index c418e56e1b0..57647a1d5b1 100644 --- a/ddtrace/internal/runtime/runtime_metrics.py +++ b/ddtrace/internal/runtime/runtime_metrics.py @@ -148,7 +148,7 @@ def flush(self): log.debug("Writing metric %s:%s", key, value) self._dogstatsd_client.distribution(key, value) - def _stop_service(self): # type: ignore[override] + def _stop_service(self): # type: (...) -> None # De-register span hook super(RuntimeWorker, self)._stop_service() diff --git a/ddtrace/internal/utils/cache.py b/ddtrace/internal/utils/cache.py index c2c1928cd8b..0b7288c85e5 100644 --- a/ddtrace/internal/utils/cache.py +++ b/ddtrace/internal/utils/cache.py @@ -9,9 +9,8 @@ miss = object() T = TypeVar("T") -S = TypeVar("S") -F = Callable[[T], S] -M = Callable[[Any, T], S] +F = Callable[[T], Any] +M = Callable[[Any, T], Any] class LFUCache(dict): @@ -29,7 +28,7 @@ def __init__(self, maxsize=256): self.lock = RLock() def get(self, key, f): # type: ignore[override] - # type: (T, F) -> S + # type: (T, F) -> Any """Get a value from the cache. If the value with the given key is not in the cache, the expensive @@ -69,7 +68,7 @@ def cached_wrapper(f): cache = LFUCache(maxsize) def cached_f(key): - # type: (T) -> S + # type: (T) -> Any return cache.get(key, f) cached_f.invalidate = cache.clear # type: ignore[attr-defined] diff --git a/ddtrace/internal/writer.py b/ddtrace/internal/writer.py index 0db28c0e97e..2ad7866d2b8 100644 --- a/ddtrace/internal/writer.py +++ b/ddtrace/internal/writer.py @@ -587,7 +587,7 @@ def flush_queue(self, raise_exc=False): def periodic(self): self.flush_queue(raise_exc=False) - def _stop_service( # type: ignore[override] + def _stop_service( self, timeout=None, # type: Optional[float] ): diff --git a/ddtrace/profiling/collector/__init__.py b/ddtrace/profiling/collector/__init__.py index 3520e0f026d..ec89d401fc8 100644 --- a/ddtrace/profiling/collector/__init__.py +++ b/ddtrace/profiling/collector/__init__.py @@ -77,5 +77,5 @@ def _create_capture_sampler(collector): @attr.s class CaptureSamplerCollector(Collector): - capture_pct = attr.ib(factory=attr_utils.from_env("DD_PROFILING_CAPTURE_PCT", 1.0, float)) + capture_pct = attr.ib(factory=attr_utils.from_env("DD_PROFILING_CAPTURE_PCT", 1.0, float)) # type: ignore[arg-type] _capture_sampler = attr.ib(default=attr.Factory(_create_capture_sampler, takes_self=True), init=False, repr=False) diff --git a/ddtrace/profiling/collector/_lock.py b/ddtrace/profiling/collector/_lock.py index dec5abbfd60..5fc087a7a3a 100644 --- a/ddtrace/profiling/collector/_lock.py +++ b/ddtrace/profiling/collector/_lock.py @@ -183,7 +183,7 @@ def __get__(self, instance, owner=None): class LockCollector(collector.CaptureSamplerCollector): """Record lock usage.""" - nframes = attr.ib(factory=attr_utils.from_env("DD_PROFILING_MAX_FRAMES", 64, int)) + nframes = attr.ib(factory=attr_utils.from_env("DD_PROFILING_MAX_FRAMES", 64, int)) # type: ignore[arg-type] endpoint_collection_enabled = attr.ib( factory=attr_utils.from_env("DD_PROFILING_ENDPOINT_COLLECTION_ENABLED", True, formats.asbool) ) @@ -203,13 +203,13 @@ def _set_original( # type: (...) -> None pass - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: (...) -> None """Start collecting lock usage.""" self.patch() super(LockCollector, self)._start_service() - def _stop_service(self): # type: ignore[override] + def _stop_service(self): # type: (...) -> None """Stop collecting lock usage.""" super(LockCollector, self)._stop_service() diff --git a/ddtrace/profiling/collector/asyncio.py b/ddtrace/profiling/collector/asyncio.py index 59199149117..e6832e0db1a 100644 --- a/ddtrace/profiling/collector/asyncio.py +++ b/ddtrace/profiling/collector/asyncio.py @@ -29,7 +29,7 @@ class AsyncioLockCollector(_lock.LockCollector): PROFILED_LOCK_CLASS = _ProfiledAsyncioLock - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: (...) -> None """Start collecting lock usage.""" try: diff --git a/ddtrace/profiling/collector/memalloc.py b/ddtrace/profiling/collector/memalloc.py index f48b4b34f9f..bbb954d2c25 100644 --- a/ddtrace/profiling/collector/memalloc.py +++ b/ddtrace/profiling/collector/memalloc.py @@ -88,12 +88,18 @@ class MemoryCollector(collector.PeriodicCollector): _interval = attr.ib(default=_DEFAULT_INTERVAL, repr=False) # TODO make this dynamic based on the 1. interval and 2. the max number of events allowed in the Recorder - _max_events = attr.ib(factory=attr_utils.from_env("_DD_PROFILING_MEMORY_EVENTS_BUFFER", _DEFAULT_MAX_EVENTS, int)) - max_nframe = attr.ib(factory=attr_utils.from_env("DD_PROFILING_MAX_FRAMES", 64, int)) + _max_events = attr.ib( + factory=attr_utils.from_env( + "_DD_PROFILING_MEMORY_EVENTS_BUFFER", + _DEFAULT_MAX_EVENTS, + int, # type: ignore[arg-type] + ) + ) + max_nframe = attr.ib(factory=attr_utils.from_env("DD_PROFILING_MAX_FRAMES", 64, int)) # type: ignore[arg-type] heap_sample_size = attr.ib(type=int, factory=_get_default_heap_sample_size) ignore_profiler = attr.ib(factory=attr_utils.from_env("DD_PROFILING_IGNORE_PROFILER", False, formats.asbool)) - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: (...) -> None """Start collecting memory profiles.""" if _memalloc is None: @@ -103,7 +109,7 @@ def _start_service(self): # type: ignore[override] super(MemoryCollector, self)._start_service() - def _stop_service(self): # type: ignore[override] + def _stop_service(self): # type: (...) -> None super(MemoryCollector, self)._stop_service() diff --git a/ddtrace/profiling/exporter/http.py b/ddtrace/profiling/exporter/http.py index d3405200a63..16ad2e10f6e 100644 --- a/ddtrace/profiling/exporter/http.py +++ b/ddtrace/profiling/exporter/http.py @@ -47,7 +47,10 @@ class PprofHTTPExporter(pprof.PprofExporter): api_key = attr.ib(default=None, type=typing.Optional[str]) # Do not use the default agent timeout: it is too short, the agent is just a unbuffered proxy and the profiling # backend is not as fast as the tracer one. - timeout = attr.ib(factory=attr_utils.from_env("DD_PROFILING_API_TIMEOUT", 10.0, float), type=float) + timeout = attr.ib( + factory=attr_utils.from_env("DD_PROFILING_API_TIMEOUT", 10.0, float), # type: ignore[arg-type] + type=float, + ) service = attr.ib(default=None, type=typing.Optional[str]) env = attr.ib(default=None, type=typing.Optional[str]) version = attr.ib(default=None, type=typing.Optional[str]) diff --git a/ddtrace/profiling/profiler.py b/ddtrace/profiling/profiler.py index 918d0f74889..92964bb652c 100644 --- a/ddtrace/profiling/profiler.py +++ b/ddtrace/profiling/profiler.py @@ -252,7 +252,7 @@ def copy(self): } ) - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: (...) -> None """Start the profiler.""" collectors = [] @@ -270,7 +270,7 @@ def _start_service(self): # type: ignore[override] if self._scheduler is not None: self._scheduler.start() - def _stop_service( # type: ignore[override] + def _stop_service( self, flush=True # type: bool ): # type: (...) -> None diff --git a/ddtrace/profiling/scheduler.py b/ddtrace/profiling/scheduler.py index 9bf2d3d139a..6bafc9a60d6 100644 --- a/ddtrace/profiling/scheduler.py +++ b/ddtrace/profiling/scheduler.py @@ -20,7 +20,9 @@ class Scheduler(periodic.PeriodicService): recorder = attr.ib() exporters = attr.ib() before_flush = attr.ib(default=None, eq=False) - _interval = attr.ib(factory=attr_utils.from_env("DD_PROFILING_UPLOAD_INTERVAL", 60.0, float)) + _interval = attr.ib( + factory=attr_utils.from_env("DD_PROFILING_UPLOAD_INTERVAL", 60.0, float) # type: ignore[arg-type] + ) _configured_interval = attr.ib(init=False) _last_export = attr.ib(init=False, default=None, eq=False) @@ -28,7 +30,7 @@ def __attrs_post_init__(self): # Copy the value to use it later since we're going to adjust the real interval self._configured_interval = self.interval - def _start_service(self): # type: ignore[override] + def _start_service(self): # type: (...) -> None """Start the scheduler.""" LOG.debug("Starting scheduler")