我有像这样的梁代码:
def foo(i: int) -> Foo:
...
class IntToFoo(beam.PTransform):
def expand(self, pcoll: PCollection[int]) -> PCollection[Foo]:
return pcoll | beam.Map(foo)
运行pyright它抱怨返回线:
Expression of type "PValue" cannot be assigned to return type "PCollection[Foo]"
"PValue" is incompatible with "PCollection[Foo]
现在 AFAIU 似乎梁没有提供足够的类型提示。作为 beam 的用户,有没有办法解决这个问题,以便 pyright 接受代码(不忽略类型错误)。
编辑:
我现在尝试:
temp = pcoll | beam.Map(foo)
assert isinstance(temp, PCollection[Foo])
return temp
好消息是,线路也temp =
没有return
任何错误。坏消息是它现在在 isinstance 线上抱怨:
不允许使用类型参数的 TypeVar 或泛型类型
编辑 2:
现在我解决了
temp = pcoll | beam.Map(foo)
return cast(PCollection[Foo], temp)
IMO 仍然不是一个解决方案,因为我需要同时指定PCollection[Foo]
签名和演员表。