Skip to content
Merged
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
16 changes: 13 additions & 3 deletions comm/base_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import typing as t
import uuid

from traitlets.utils.importstring import import_item

import comm

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -212,7 +210,19 @@ def register_target(self, target_name: str, f: CommTargetCallback | str) -> None
f can be a Python callable or an import string for one.
"""
if isinstance(f, str):
f = import_item(f)
parts = f.rsplit(".", 1)
if len(parts) == 2:
# called with 'foo.bar....'
package, obj = parts
module = __import__(package, fromlist=[obj])
try:
f = getattr(module, obj)
except AttributeError as e:
error_msg = f"No module named {obj}"
raise ImportError(error_msg) from e
else:
# called with un-dotted string
f = __import__(parts[0])

self.targets[target_name] = t.cast(CommTargetCallback, f)

Expand Down
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
]
dependencies = [
"traitlets>=4",
]

[project.optional-dependencies]
test = [
Expand Down