如何键入提示转换 RDD 的函数?
How to type hint a function that transforms an RDD?
给定一个 StructType 模式,我希望能够定义
def foo(row: schema)
return row.field
并让 PyCharm 识别行的字段,但 PyCharm 不将 'schema' 识别为一种类型。内联没有区别。 (我正在使用 Python 3.8)
我猜您想将类型提示指定为 StructType
?
from pyspark.sql.types import StructType
def foo(row: StructType):
return row.field
技术上不正确; row 是一行,但由于 duck typing,它工作得很好:
from dataclasses import dataclass
@dataclass
class HintedRow:
x: int
y: str
def foo(row: HintedRow):
return row.x
df.rdd.map(foo)
现在您可以像这样在单元测试中使用它,pyspark 不会抱怨,因为 HintedRow 的属性与 Row 的属性相同:
test_row = HintedRow(x=1, y='bar')
assert foo(test_row) == 1
给定一个 StructType 模式,我希望能够定义
def foo(row: schema)
return row.field
并让 PyCharm 识别行的字段,但 PyCharm 不将 'schema' 识别为一种类型。内联没有区别。 (我正在使用 Python 3.8)
我猜您想将类型提示指定为 StructType
?
from pyspark.sql.types import StructType
def foo(row: StructType):
return row.field
技术上不正确; row 是一行,但由于 duck typing,它工作得很好:
from dataclasses import dataclass
@dataclass
class HintedRow:
x: int
y: str
def foo(row: HintedRow):
return row.x
df.rdd.map(foo)
现在您可以像这样在单元测试中使用它,pyspark 不会抱怨,因为 HintedRow 的属性与 Row 的属性相同:
test_row = HintedRow(x=1, y='bar')
assert foo(test_row) == 1