Python lru_缓存干扰单_分派完成的类型检查
我有一个有三个注册函数的。一个是在Python lru_缓存干扰单_分派完成的类型检查,python,python-3.x,dispatch,lru,single-dispatch,Python,Python 3.x,Dispatch,Lru,Single Dispatch,我有一个有三个注册函数的。一个是在int上发送,它工作正常。第二个在自定义类型上调度,也可以正常工作。第三个也是一个自定义类型,但该类使用lru\u缓存decorator进行包装 (为了使事情更复杂一些,该类通过另一个类的\uuuu调用\uuu方法上的methoddispatch以迂回的方式实例化。) 在音高等级内: @oph_utils.method_dispatch def augmented(self, other): raise NotImplementedError @aug
int
上发送,它工作正常。第二个在自定义类型上调度,也可以正常工作。第三个也是一个自定义类型,但该类使用lru\u缓存
decorator进行包装
(为了使事情更复杂一些,该类通过另一个类的\uuuu调用\uuu
方法上的methoddispatch以迂回的方式实例化。)
在音高等级内:
@oph_utils.method_dispatch
def augmented(self, other):
raise NotImplementedError
@augmented.register(int)
def _(self, other):
return "works fine"
@augmented.register(Interval)
def _(self, other):
return "works fine too"
@augmented.register(QualifiedInterval)
def _(self, other):
return "only works if QualifiedInterval class does NOT have lru_cache"
(还有很多事情要做,但这些都不起作用。)
基本上-如果我有lru_缓存,并将一个限定的interval传递到函数中,它不会分派并引发NotImplementedError。如果我注释掉缓存装饰器,它就会工作。REPL上的手动类型检查显示了相同的类型(“QualifiedInterval”)。我尝试过以几种不同的方式调用创建QualifiedInterval的命令,并尝试将其分配给变量。还是不行。我尝试过在增强函数中进行显式类型检查。如果启用了缓存,则类型检查也会失败。分析出错原因
基本上-如果我有lru_缓存,并将一个限定的interval传递到函数中,它不会分派
是一个函数,返回包装任何可调用(包括类)的。因此,当您将lru_缓存应用于QualifiedInterval类时,该变量将被分配给包装函数,而不是类本身
>>> @lru_cache(maxsize=None, typed=True)
class QualifiedInterval:
pass
>>> type(QualifiedInterval)
<class 'functools._lru_cache_wrapper'>
请注意,添加了。\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
属性,该属性通过包装函数到达原始的unwrapped类
希望这一切都能澄清,并指明前进的方向:-)这样就行了。(谢谢!)但是为什么type(instanceofqalifiedinterval)
一方面返回QualifiedInterval
,另一方面type(instanceofqalifiedinterval)是QualifiedInterval
返回false。
>>> @lru_cache(maxsize=None, typed=True)
class QualifiedInterval:
pass
>>> type(QualifiedInterval)
<class 'functools._lru_cache_wrapper'>
@augmented.register(QualifiedInterval.__wrapped__)
def _(self, other):
return "show now work QualifiedInterval class has an lru_cache"