Unverified Commit 8175f280 authored by ZHANG Zhi's avatar ZHANG Zhi Committed by GitHub
Browse files

Fix 'NoneType' Error on jupyter notebooks (#3337)

parent 1dfda7aa
...@@ -2,6 +2,7 @@ import inspect ...@@ -2,6 +2,7 @@ import inspect
import warnings import warnings
from collections import defaultdict from collections import defaultdict
from typing import Any from typing import Any
from pathlib import Path
def import_(target: str, allow_none: bool = False) -> Any: def import_(target: str, allow_none: bool = False) -> Any:
...@@ -107,7 +108,18 @@ def blackbox_module(cls): ...@@ -107,7 +108,18 @@ def blackbox_module(cls):
Register a module. Use it as a decorator. Register a module. Use it as a decorator.
""" """
frm = inspect.stack()[1] frm = inspect.stack()[1]
assert (inspect.getmodule(frm[0]) is not None), ('unable to locate the definition of the given black box module, '
'please define it explicitly in a .py file.')
module_name = inspect.getmodule(frm[0]).__name__ module_name = inspect.getmodule(frm[0]).__name__
if module_name == '__main__':
main_file_path = Path(inspect.getsourcefile(frm[0]))
if main_file_path.parents[0] != Path('.'):
raise RuntimeError(f'you are using "{main_file_path}" to launch your experiment, '
f'please launch the experiment under the directory where "{main_file_path.name}" is located.')
module_name = main_file_path.stem
return _blackbox_cls(cls, module_name, 'args') return _blackbox_cls(cls, module_name, 'args')
...@@ -116,6 +128,8 @@ def register_trainer(cls): ...@@ -116,6 +128,8 @@ def register_trainer(cls):
Register a trainer. Use it as a decorator. Register a trainer. Use it as a decorator.
""" """
frm = inspect.stack()[1] frm = inspect.stack()[1]
assert (inspect.getmodule(frm[0]) is not None), ('unable to locate the definition of the given trainer, '
'please define it explicitly in a .py file.')
module_name = inspect.getmodule(frm[0]).__name__ module_name = inspect.getmodule(frm[0]).__name__
return _blackbox_cls(cls, module_name, 'full') return _blackbox_cls(cls, module_name, 'full')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment