【问题标题】:Inspect doesn't Return Data Type of a Function检查不返回函数的数据类型
【发布时间】:2021-12-23 03:20:48
【问题描述】:

我正在尝试使用joke2k/faker 来生成假数据。允许使用函数名称动态生成,例如"random_int",我用 inspect 查找函数的签名。签名应包括输入参数的数据类型和默认值。

inspect.signature(fake.random_int)

但它返回以下没有数据类型:

<Signature (min=0, max=9999, step=1)>

我在 faker 库中检查 source code of the functioninspect 应该返回 &lt;Signature (min: int = 0, max: int = 9999, step: int = 1) -&gt; int&gt;

def random_int(self, min: int = 0, max: int = 9999, step: int = 1) -> int:
    """Generate a random integer between two integers ``min`` and ``max`` inclusive
    while observing the provided ``step`` value.
    This method is functionally equivalent to randomly sampling an integer
    from the sequence ``range(min, max + 1, step)``.
    :sample:
    :sample size=10: min=0, max=15
    :sample size=10: min=0, max=15, step=3
    """
    return self.generator.random.randrange(min, max + 1, step)

代码编译到 python 库时,python 类型会丢失吗?如何重新编译保留打字的库?

【问题讨论】:

    标签: python-3.x faker


    【解决方案1】:

    上述情况发生在 Faker == 8.16.0。 将 Faker 升级到 9.8 版后,inspect.signature() 返回正确的输入。

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2015-07-20
      • 1970-01-01
      • 2011-06-27
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多