验证未知模块/对象是否必须使用特定的接口(python) [英] Verify that an unknown module/object is obliged to a specific interface (python)

查看:114
本文介绍了验证未知模块/对象是否必须使用特定的接口(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想在运行时检查给定的对象是否具有方法foo()bar().

I'd like to check at runtime, for example, that a given object has methods foo() and bar().

我的研究系统(内置于python 3.6中)经过高度参数化,可以/应该接受任何类型的对象作为其内置模块的替代品.此功能非常有用,因为使用该系统的许多不同学生可以轻松研究不同的行为,而无需更改系统的源代码.

My research system, built in python 3.6, is highly parameterized and can/should accept any kind of object as a replacement of its build in modules. This functionality is very useful because many different students who use this system can easily research different behavior without changing the source code of my system.

问题在于,如果他们错误地构建了模块,则只有在整个实验结束后(可能是数小时),他们才可能发现这一点.

The problem is that if they built the module wrong, they might discover this only after their entire experiment is ended (might be hours).

我正在寻找一种在运行时的早期检查其输入模块是否匹配特定接口的方法.甚至在实例化之前进行验证就更好了(当输入仅是类型,而不是实例时). 例如some_interface.verify(MyClass).

I am looking for a way to check at a very early stage of the runtime that their input module matches a specific interface. Verifying it even before it was instantiated is even better (when the input is only the type, not the instance). For example some_interface.verify(MyClass).

我在互联网上看到了许多解决方案(例如

I have seen many solutions on the internet (such as this), but none of them is suitable:

  1. 最常见的解决方案(try/catch)仅在运行时失败,不适用于多守护程序系统,因为只有一个守护程序发生故障时很难关闭它.

  1. The most common solution (try/catch) will only fail during runtime and is not applicable in a multi-daemon system because it is hard to shut down when only one of the daemons fail.

检查 isinstance() 不会验证任何事物.甚至更糟,因为开发人员可能会忘记实现一个函数并使用基类实现,而这可能不适合其当前的实现.

Checking isinstance() doesn't verify anything. It might be even worse because the developer might forget to implement a function and use base class implementation, which might not fit its current implementation.

使用 ABC (抽象基类)需要开发人员从基类继承.如果他/她没有这样做,则在实例化该类时不会发出警告或错误.另一方面,如果开发人员确实实现了该接口但未从base继承,则issubclass()将返回False.

Using ABC (Abstract Base Classes) requires the developer to inherit from the base class. If she/he fail to do so, no warning or error will be issued when instantiating the class. On the other hand, if the developer did implement the interface but did not inherit from base, then issubclass() will return False.

使用 zope接口是我的首选,但是它有一些缺点:

Using zope interfaces was my goto, but it has a few shortcomings:

  • 它要求开发人员明确提及它正在实现该接口.尽管实际的实现是正确的,但未指定此选项将导致错误.
  • 在实例化模块之前无法验证模块. implementedBy()方法仅会检查模块是否声明它正在实现该接口,但要进行实际验证,应在实际实例上调用verifyObject().
  • 它不支持自python 3.5起添加的新键入功能
  • It requires the developer to explicitly mention that it is implementing the interface. Failing to specify this will result in an error, although the actual implementation is correct.
  • It cannot verify a module before it was instantiated. The implementedBy() method will only check if the module declared it is implementing the interface, but to actually verify it, you should call verifyObject() on the actual instance.
  • It does not support the new typing feature that was added since python 3.5

显然,zope还通过调用verifyObject(YourInterface, obj, tentative=True)来支持隐式实现,这并不强制开发人员将类明确定义为接口的实现.

Apparently, zope also supports implicit implementation by calling verifyObject(YourInterface, obj, tentative=True) which does not force the developer to explicitly defining the class as an implementer of the interface.

推荐答案

在我看来,问题不是工具问题.主要问题是,即使支持某些接口,也无法确保该模块确实有效.我要做的是为模块创建测试,并在初始化插件时运行它.该测试不仅应验证类型和接口(isinstancehasattr等仅是完成任务的工具),而且还应验证(如果可能)模块功能的最小正确性.例如.执行一些不需要太多时间来完成和验证结果的基本任务就可以了.如果插件在这样的测试任务中失败,则该插件无效.

To my mind, the problem is not a problem of tools. The main problem is that even if some interface is supported, no one can be sure the module really works. What would I do is creating a test for modules and running it when initializing plugins. The test should verify not just types and interfaces (isinstance, hasattr and so on are just tools for the task), but (if possible) minimal correctness of the module's functioning. E.g. it would be fine to perform some basic task that does not require much time to complete and verify the results. If a plugin fails during such a test task, then the plugin is not valid.

这篇关于验证未知模块/对象是否必须使用特定的接口(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆