Skip to content

Commit 1d7c662

Browse files
authored
Add some more typing for the public api (#1619)
* Add some more typing for the public api * Review feedback
1 parent cfe3fef commit 1d7c662

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = '''
1616
[tool.pyright]
1717
pythonVersion = "3.8"
1818
include = ["src/**", "tests/**" ]
19-
extraPaths = ["src/debugpy/_vendored/pydevd"]
19+
extraPaths = ["src/debugpy/_vendored/pydevd", "src/debugpy/_vendored/pydevd/pydevd_attach_to_process"]
2020
ignore = ["src/debugpy/_vendored/pydevd", "src/debugpy/_version.py"]
2121
executionEnvironments = [
2222
{ root = "src" }, { root = "." }

src/debugpy/public_api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ def cancel(*args, **kwargs):
3838
wrapped = getattr(api, f.__name__)
3939
return wrapped.cancel(*args, **kwargs)
4040

41-
wrapper.cancel = cancel
41+
wrapper.cancel = cancel # pyright: ignore
4242

4343
return wrapper
4444

4545
return apply
4646

4747

4848
@_api()
49-
def log_to(__path: str) -> None:
49+
def log_to(__path: str | typing.TextIO) -> None:
5050
"""Generate detailed debugpy logs in the specified directory.
5151
5252
The directory must already exist. Several log files are generated,
@@ -117,7 +117,7 @@ def listen(
117117
adapter that it starts. Use `wait_for_client` to block execution
118118
until the client connects.
119119
"""
120-
120+
...
121121

122122
@_api()
123123
def connect(__endpoint: Endpoint | int, *, access_token: str | None = None) -> Endpoint:
@@ -135,7 +135,7 @@ def connect(__endpoint: Endpoint | int, *, access_token: str | None = None) -> E
135135
adapter that it connects to. Use `wait_for_client` to block
136136
execution until the client connects.
137137
"""
138-
138+
...
139139

140140
@_api(cancelable=True)
141141
def wait_for_client() -> None:
@@ -153,6 +153,7 @@ def is_client_connected() -> bool:
153153
"""True if a client is connected to the debug adapter that is
154154
debugging this process.
155155
"""
156+
...
156157

157158

158159
@_api()

src/debugpy/server/cli.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import sys
99
from importlib.util import find_spec
10+
from typing import Any
1011

1112
# debugpy.__main__ should have preloaded pydevd properly before importing this module.
1213
# Otherwise, some stdlib modules above might have had imported threading before pydevd
@@ -18,6 +19,7 @@
1819
from _pydevd_bundle import pydevd_runpy as runpy
1920

2021
import debugpy
22+
import debugpy.server
2123
from debugpy.common import log
2224
from debugpy.server import api
2325

@@ -41,13 +43,14 @@
4143

4244
class Options(object):
4345
mode = None
44-
address = None
46+
address: "tuple[str, int] | None" = None
4547
log_to = None
4648
log_to_stderr = False
47-
target = None
48-
target_kind = None
49+
target: str | None = None
50+
target_kind: str | None = None
4951
wait_for_client = False
5052
adapter_access_token = None
53+
config: "dict[str, Any]" = {}
5154

5255

5356
options = Options()
@@ -139,7 +142,7 @@ def set_config(arg, it):
139142
options.config[name] = value
140143

141144

142-
def set_target(kind, parser=(lambda x: x), positional=False):
145+
def set_target(kind: str, parser=(lambda x: x), positional=False):
143146
def do(arg, it):
144147
options.target_kind = kind
145148
target = parser(arg if positional else next(it))
@@ -252,9 +255,9 @@ def start_debugging(argv_0):
252255

253256
debugpy.configure(options.config)
254257

255-
if options.mode == "listen":
258+
if options.mode == "listen" and options.address is not None:
256259
debugpy.listen(options.address)
257-
elif options.mode == "connect":
260+
elif options.mode == "connect" and options.address is not None:
258261
debugpy.connect(options.address, access_token=options.adapter_access_token)
259262
else:
260263
raise AssertionError(repr(options.mode))
@@ -272,7 +275,7 @@ def run_file():
272275
# parent directory to sys.path. Thus, importing other modules from the
273276
# same directory is broken unless sys.path is patched here.
274277

275-
if os.path.isfile(target):
278+
if target is not None and os.path.isfile(target):
276279
dir = os.path.dirname(target)
277280
sys.path.insert(0, dir)
278281
else:
@@ -293,7 +296,7 @@ def run_module():
293296
# actually invoking it.
294297
argv_0 = sys.argv[0]
295298
try:
296-
spec = find_spec(options.target)
299+
spec = None if options.target is None else find_spec(options.target)
297300
if spec is not None:
298301
argv_0 = spec.origin
299302
except Exception:
@@ -318,16 +321,19 @@ def run_module():
318321

319322

320323
def run_code():
321-
# Add current directory to path, like Python itself does for -c.
322-
sys.path.insert(0, str(""))
323-
code = compile(options.target, str("<string>"), str("exec"))
324+
if options.target is not None:
325+
# Add current directory to path, like Python itself does for -c.
326+
sys.path.insert(0, str(""))
327+
code = compile(options.target, str("<string>"), str("exec"))
324328

325-
start_debugging(str("-c"))
329+
start_debugging(str("-c"))
326330

327-
log.describe_environment("Pre-launch environment:")
328-
log.info("Running code:\n\n{0}", options.target)
331+
log.describe_environment("Pre-launch environment:")
332+
log.info("Running code:\n\n{0}", options.target)
329333

330-
eval(code, {})
334+
eval(code, {})
335+
else:
336+
log.error("No target to run.")
331337

332338

333339
def attach_to_pid():
@@ -421,13 +427,14 @@ def main():
421427
)
422428

423429
try:
424-
run = {
425-
"file": run_file,
426-
"module": run_module,
427-
"code": run_code,
428-
"pid": attach_to_pid,
429-
}[options.target_kind]
430-
run()
430+
if options.target_kind is not None:
431+
run = {
432+
"file": run_file,
433+
"module": run_module,
434+
"code": run_code,
435+
"pid": attach_to_pid,
436+
}[options.target_kind]
437+
run()
431438
except SystemExit as exc:
432439
log.reraise_exception(
433440
"Debuggee exited via SystemExit: {0!r}", exc.code, level="debug"

0 commit comments

Comments
 (0)