Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pydevd debugger plugin #603

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/214.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New pydevd resolver plugin for easier debugging
6 changes: 6 additions & 0 deletions pydevd_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try:
__import__("pkg_resources").declare_namespace(__name__)
except ImportError: # pragma: no cover
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore
6 changes: 6 additions & 0 deletions pydevd_plugins/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try:
__import__("pkg_resources").declare_namespace(__name__)
except ImportError: # pragma: no cover
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore
130 changes: 130 additions & 0 deletions pydevd_plugins/extensions/pydevd_plugin_omegaconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# based on https://github.com/fabioz/PyDev.Debugger/tree/main/pydevd_plugins/extensions

import sys
from functools import lru_cache
from typing import Any, Dict

from _pydevd_bundle.pydevd_extension_api import TypeResolveProvider # type: ignore


@lru_cache(maxsize=128)
def find_mod_attr(mod_name: str, attr: str) -> Any:
mod = sys.modules.get(mod_name)
return getattr(mod, attr, None)


class Wrapper(object):
def __init__(self, target: Any, desc: str) -> None:
self.target = target
self.desc = desc

def __repr__(self) -> str: # pragma: no cover
return self.desc

def __getattr__(self, attr: str) -> Any: # pragma: no cover
return getattr(self.target, attr)

def __eq__(self, other: Any) -> Any: # pragma: no cover
if isinstance(other, Wrapper):
return self.desc == other.desc and self.target == other.target
else:
return NotImplemented

def __ne__(self, other: Any) -> Any: # pragma: no cover
return not self.__eq__(other)


class OmegaConfNodeResolver(object):
def can_provide(self, type_object: Any, type_name: str) -> bool:
Node = find_mod_attr("omegaconf", "Node")

return Node is not None and issubclass(type_object, (Node, Wrapper))

def resolve(self, obj: Any, attribute: str) -> Any:
InterpolationResolutionError = find_mod_attr(
"omegaconf.errors", "InterpolationResolutionError"
)
Node = find_mod_attr("omegaconf", "Node")
DictConfig = find_mod_attr("omegaconf", "DictConfig")
ListConfig = find_mod_attr("omegaconf", "ListConfig")

if isinstance(obj, Wrapper):
obj = obj.target

if isinstance(obj, DictConfig):
field = obj.__dict__["_content"][attribute]
elif isinstance(obj, ListConfig):
field = obj.__dict__["_content"][int(attribute)]
else: # pragma: no cover
assert False

if isinstance(field, Node) and field._is_interpolation():
try:
resolved = field._dereference_node()
desc = f"{field} -> {resolved}"
field = Wrapper(field, desc)
except InterpolationResolutionError as ex:
desc = f"{field} -> ERR: {ex}"
field = Wrapper(field, desc)

return field

def get_dictionary(self, obj: Any) -> Dict[str, Any]:
Container = find_mod_attr("omegaconf", "Container")
ListConfig = find_mod_attr("omegaconf", "ListConfig")
DictConfig = find_mod_attr("omegaconf", "DictConfig")
Node = find_mod_attr("omegaconf", "Node")
ValueNode = find_mod_attr("omegaconf", "ValueNode")

if isinstance(obj, Wrapper):
obj = obj.target

assert isinstance(obj, Node)

d: Dict[Any, Any] = {}

if isinstance(obj, Node):
if obj._is_missing() or obj._is_none():
return {}
if obj._is_interpolation():
if obj._parent is not None:
resolved = obj._dereference_node(throw_on_resolution_failure=False)
else:
resolved = None
if isinstance(obj, ValueNode):
d = {}
elif isinstance(obj, Container):
if resolved is not None:
d = self.get_dictionary(resolved)
return d
else:
if isinstance(obj, ValueNode):
d["_val"] = obj._value()

if isinstance(obj, ListConfig):
assert not obj._is_interpolation()
assert not obj._is_none()
assert not obj._is_missing()
for idx, node in enumerate(obj.__dict__["_content"]):
d[str(idx)] = node
elif isinstance(obj, DictConfig):
assert not obj._is_interpolation()
assert not obj._is_none()
assert not obj._is_missing()
for key in obj.keys():
node = obj._get_node(key, throw_on_missing_value=False)
is_inter = node._is_interpolation()
if is_inter:
resolved = node._dereference_node(throw_on_resolution_failure=False)
if resolved is not None:
value = resolved
else:
value = node
else:
value = node._value()

d[key] = value
return d


TypeResolveProvider.register(OmegaConfNodeResolver)
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pytest-mock
sphinx
towncrier
twine
pydevd
Loading