简体   繁体   English

将None而不是Series传递给函数时出错

[英]Error when passing None instead of Series to function

I have a module fi with the following classes defined: 我有一个模块fi ,其中定义了以下类:

class CashFlows(Asset):

   def __init__(self, amounts : pandas.Series, probabilities : pandas.Series = None):
       amounts = Asset.to_cash_flows()

       if probabilities is None:
           probabilities = pandas.Series([1]*len(amounts), index=amounts.index)

I then have another class Bond(fi.Asset) with this method within it, which is where CashFlows is being called: 然后,我有了另一个带有此方法的类Bond(fi.Asset) ,在CashFlows中调用CashFlows

def to_cash_flows(self, notional : float = 100.0) -> fi.asset.CashFlows:
    #Bunch of stuff here which is not related
    series = pandas.Series(list_of_data, indices_of_data)
    return fi.CashFlows(series)

I get the error obiwan.ObiwanError:__init__(probabilities) is None but should be <class 'pandas.core.series.Series'> , but I thought I was addressing the possibility of such an Error with the if statement. 我收到错误obiwan.ObiwanError:__init__(probabilities) is None but should be <class 'pandas.core.series.Series'> ,但我认为我正在使用if语句解决此类错误的可能性。 obiwan is just a Python type-checking package that I am using during Unit-Testing. obiwan只是我在单元测试期间使用的Python类型检查包。

Thank You 谢谢

According to the obiwan PyPI page : 根据obiwan PyPI页面

You can specify alternative constraint types using sets: 您可以使用集合指定替代约束类型:

def example5(x: {int,float}):
    ...

Taking that at face value, it would seem you should replace: 从表面上看,似乎您应该替换:

probabilities : pandas.Series = None

with: 有:

probabilities : {pandas.Series, type(None)} = None

This will tell obiwan that None is an acceptable value for the parameter. 这将告诉obiwan参数无可接受的值。

obiwan is not actually looking into the function itself and doesn't care about the if statement that compares the parameter to None. 欧比旺实际上并没有寻找函数本身并不关心if该参数比较无说法。 It is only looking at the function declaration. 它仅查看函数声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM