From b2c0b3e106e61dc0852cfa07cabd1128d6b48c9e Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Thu, 28 Mar 2024 17:14:37 -0700 Subject: [PATCH] Fix type annotations --- src/debugpy/server/__init__.py | 8 ++++++-- src/debugpy/server/inspect/__init__.py | 2 +- src/debugpy/server/inspect/stdlib.py | 2 +- src/debugpy/server/tracing/__init__.py | 17 +++++++++-------- src/debugpy/server/tracing/tracer.py | 5 +++-- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/debugpy/server/__init__.py b/src/debugpy/server/__init__.py index 8691d5a0..6c0c3164 100644 --- a/src/debugpy/server/__init__.py +++ b/src/debugpy/server/__init__.py @@ -3,6 +3,10 @@ # for license information. import itertools +from typing import TYPE_CHECKING, Optional + +if TYPE_CHECKING: + import debugpy.server.adapters as adapters # Unique IDs for DAP objects such as threads, variables, breakpoints etc. These are # negative to allow for pre-existing OS-assigned IDs (which are positive) to be used @@ -10,12 +14,12 @@ import itertools _dap_ids = itertools.count(-1, -1) -def new_dap_id(): +def new_dap_id() -> int: """Returns the next unique ID.""" return next(_dap_ids) -def adapter(): +def adapter() -> Optional["adapters.Adapter"]: """ Returns the instance of Adapter corresponding to the debug adapter that is currently connected to this process, or None if there is no adapter connected. Use in lieu of diff --git a/src/debugpy/server/inspect/__init__.py b/src/debugpy/server/inspect/__init__.py index 79953d6c..c22d7cac 100644 --- a/src/debugpy/server/inspect/__init__.py +++ b/src/debugpy/server/inspect/__init__.py @@ -6,7 +6,7 @@ Object inspection: rendering values, enumerating children etc. """ -from typing import Iterable +from collections.abc import Iterable class ChildObject: diff --git a/src/debugpy/server/inspect/stdlib.py b/src/debugpy/server/inspect/stdlib.py index 1b247b94..1b1d4ca5 100644 --- a/src/debugpy/server/inspect/stdlib.py +++ b/src/debugpy/server/inspect/stdlib.py @@ -4,8 +4,8 @@ """Object inspection for builtin Python types.""" +from collections.abc import Iterable from itertools import count -from typing import Iterable from debugpy.common import log from debugpy.server.inspect import ChildObject, ObjectInspector, inspect diff --git a/src/debugpy/server/tracing/__init__.py b/src/debugpy/server/tracing/__init__.py index f7a902e2..7af2bcb1 100644 --- a/src/debugpy/server/tracing/__init__.py +++ b/src/debugpy/server/tracing/__init__.py @@ -6,6 +6,7 @@ import re import threading import traceback from collections import defaultdict +from collections.abc import Callable, Iterable from dataclasses import dataclass from debugpy import server from debugpy.common import log @@ -15,7 +16,7 @@ from enum import Enum from pathlib import Path from sys import monitoring from types import CodeType, FrameType -from typing import Callable, ClassVar, Dict, Iterable, List, Literal, Union +from typing import ClassVar, Literal, Union # Shared for all global state pertaining to breakpoints and stepping. _cvar = threading.Condition() @@ -145,7 +146,7 @@ class Thread: can exclude a specific thread from tracing. """ - _all: ClassVar[Dict[int, "Thread"]] = {} + _all: ClassVar[dict[int, "Thread"]] = {} def __init__(self, python_thread: threading.Thread): """ @@ -294,9 +295,9 @@ class StackFrame: python_frame: FrameType _source: Source | None - _scopes: List[Scope] + _scopes: list[Scope] - _all: ClassVar[Dict[int, "StackFrame"]] = {} + _all: ClassVar[dict[int, "StackFrame"]] = {} def __init__(self, thread: Thread, python_frame: FrameType): """ @@ -351,7 +352,7 @@ class StackFrame: def get(self, id: int) -> "StackFrame": return self._all.get(id, None) - def scopes(self) -> List[Scope]: + def scopes(self) -> list[Scope]: if self._scopes is None: self._scopes = [ Scope(self, "local", self.python_frame.f_locals), @@ -554,9 +555,9 @@ class Breakpoint: hit_count: int """Number of times this breakpoint has been hit.""" - _all: ClassVar[Dict[int, "Breakpoint"]] = {} + _all: ClassVar[dict[int, "Breakpoint"]] = {} - _at: ClassVar[Dict[Source, Dict[int, List["Breakpoint"]]]] = defaultdict( + _at: ClassVar[dict[Source, dict[int, list["Breakpoint"]]]] = defaultdict( lambda: defaultdict(lambda: []) ) @@ -594,7 +595,7 @@ class Breakpoint: } @classmethod - def at(self, source: Source, line: int) -> List["Breakpoint"]: + def at(self, source: Source, line: int) -> list["Breakpoint"]: """ Returns a list of all breakpoints at the specified location. """ diff --git a/src/debugpy/server/tracing/tracer.py b/src/debugpy/server/tracing/tracer.py index acbbfb24..66d91b3a 100644 --- a/src/debugpy/server/tracing/tracer.py +++ b/src/debugpy/server/tracing/tracer.py @@ -7,6 +7,7 @@ import inspect import sys import threading import traceback +from collections.abc import Iterable from debugpy import server from debugpy.server.tracing import ( Breakpoint, @@ -21,7 +22,7 @@ from debugpy.server.tracing import ( ) from sys import monitoring from types import CodeType, FrameType, TracebackType -from typing import Iterable, Literal, Type +from typing import Literal class Log: @@ -442,7 +443,7 @@ class Tracer: self._process_exception(exc, thread, "reraise") def _sys_excepthook( - self, exc_type: Type, exc: BaseException, tb: TracebackType + self, exc_type: type, exc: BaseException, tb: TracebackType ): thread = self._this_thread() if thread is None or not thread.is_traced: