仅发现此问题仅发生在我的jupyter-lab上。在ubuntu bash shell上执行时,同一脚本可以顺利通过检查程序。
类重写检查器if method.__name__ not in dir(cls): raise NameError
不会出现,但是当我将代码复制到本地目录并从本地的jupyer-lab内核中导入时,它会出现。
我正在使用ray-0.6.2,并尝试自ray.rllib
中自定义一些代码。因此,我将一些目标代码复制到本地计算机,然后从本地导入模块。模块copy
的方法ray.rllib.agents.ppo.ppo_policy_graph.PPOPolicyGraph
使用以下装饰检查器
ray.rllib.evaluation.tf_policy_graph.TFPolicyGraph
的抽象方法。
def override(cls):
"""Annotation for documenting method overrides.
Arguments:
cls (type): The superclass that provides the overriden method. If this
cls does not actually have the method, an error is raised.
"""
def check_override(method):
if method.__name__ not in dir(cls):
raise NameError("{} does not override any method of {}".format(
method, cls))
return method
return check_override
当我从ray.rllib.agents.ppo.ppo_policy_graph.PPOPolicyGraph
这样的包中导入模块时,测试通过了,但是当我复制了确切的相关代码并从本地my.localpath.agents.ppo.ppo_policy_graph.PPOPolicyGraph
中导入时,测试就通过了。
我发现在jupyter-lab内核中dir(TFPolicyGraph)
不包含抽象方法copy
名称,
我了解dir()
不能保证显示所有方法名称。但是,我想知道从已安装的软件包导入时它如何工作而不会出现问题。
以下是该问题的回溯和pdb结果。
Traceback (most recent call last):
File "<ipython-input-17-880dd2fa265a>", line 3, in <module>
from xray.agents.ppo.ppo_policy_graph import PPOPolicyGraph
File "/home/kyu/Projects/x_ray/xray/agents/ppo/ppo_policy_graph.py", line 95, in <module>
class PPOPolicyGraph(LearningRateSchedule, TFPolicyGraph):
File "/home/kyu/Projects/x_ray/xray/agents/ppo/ppo_policy_graph.py", line 256, in PPOPolicyGraph
@override(TFPolicyGraph)
File "/home/kyu/anaconda3/envs/ray-tutorial/lib/python3.6/site-packages/ray/rllib/utils/annotations.py", line 17, in check_override
method, cls))
NameError: <function PPOPolicyGraph.copy at 0x7fbdb07ae6a8> does not override any method of <class 'ray.rllib.evaluation.tf_policy_graph.TFPolicyGraph'>
> /home/kyu/anaconda3/envs/ray-tutorial/lib/python3.6/site-packages/ray/rllib/utils/annotations.py(17)check_override()
-> method, cls))
(Pdb) p cls
<class 'ray.rllib.evaluation.tf_policy_graph.TFPolicyGraph'>
(Pdb) p dir(cls)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_is_training_placeholder', '_get_loss_inputs_dict', 'apply_gradients', 'build_apply_gradients', 'build_compute_actions', 'build_compute_apply', 'build_compute_gradients', 'compute_actions', 'compute_apply', 'compute_gradients', 'compute_single_action', 'extra_apply_grad_feed_dict', 'extra_apply_grad_fetches', 'extra_compute_action_feed_dict', 'extra_compute_action_fetches', 'extra_compute_grad_feed_dict', 'extra_compute_grad_fetches', 'get_initial_state', 'get_state', 'get_weights', 'gradients', 'loss_inputs', 'on_global_var_update', 'optimizer', 'postprocess_trajectory', 'set_state', 'set_weights']
(Pdb) p [c for c in dir(cls) if c.startswith('c')]
['compute_actions', 'compute_apply', 'compute_gradients', 'compute_single_action']
(Pdb) l
12 """
13
14 def check_override(method):
15 if method.__name__ not in dir(cls):
16 raise NameError("{} does not override any method of {}".format(
17 -> method, cls))
18 return method
19
20 return check_override
[EOF]
(Pdb) p method
<function PPOPolicyGraph.copy at 0x7fbdb07ae6a8>
(Pdb) p method.__name__
'copy'
(Pdb) dir(cls)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_is_training_placeholder', '_get_loss_inputs_dict', 'apply_gradients', 'build_apply_gradients', 'build_compute_actions', 'build_compute_apply', 'build_compute_gradients', 'compute_actions', 'compute_apply', 'compute_gradients', 'compute_single_action', 'extra_apply_grad_feed_dict', 'extra_apply_grad_fetches', 'extra_compute_action_feed_dict', 'extra_compute_action_fetches', 'extra_compute_grad_feed_dict', 'extra_compute_grad_fetches', 'get_initial_state', 'get_state', 'get_weights', 'gradients', 'loss_inputs', 'on_global_var_update', 'optimizer', 'postprocess_trajectory', 'set_state', 'set_weights']