Record Types
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
I would like to be able to type a Dataframe like object with MyPy, where different columns have different types and you can get each as column as an attribute on the dataframe. This is how libraries like Pandas and Ibis work.
Generally, this requires a function to return different types by mapping string literals to different types (record kinds).
Here is a mock example implemented in Typescript, which checks properly:
class Column {
mean(): number {
return 0;
}
}
class GeoColumn extends Column {
length(): number {
return 0;
}
}
class Dataframe<T extends { [key: string]: Column} > {
constructor(private cols: T) {
}
getColumn<K extends keyof T>(name: K): T[K] {
return this.cols[name]
}
}
const d = new Dataframe({ name: new Column(), location: new GeoColumn() });
d.getColumn("name").mean();
// We can call `length` because this is a GeoColumn
d.getColumn("location").length();
Possible Syntaxes
Here are a few possible ways this could be spelled in Python:
self as TypedDict
Since we already have a TypedDict construct one of the least invasive approaches is to type self as a TypeDict.
This would probably require anonymous TypeDicts, which was proposed previously (https://github.com/python/mypy/issues/985#issuecomment-250640149).
It would also required TypedDicts to be able to take generic parameters.
class Column:
def mean(self) -> int:
return 0
class GeoColumn(Column):
def length(self) -> int:
return 0
T = TypeVar("T", bound=Dict[str, Column])
K = TypeVar("K", bound=str)
V = TypeVar("V", bound=Column)
class Dataframe(Generic[T]):
def __init__(self, cols: T):
self.cols = cols
def __getattr__(self: Dataframe[TypedDict({K: V})], name: K) -> V:
return self.cols[name]
d = Dataframe({"name": Column(), "location": GeoColumn()})
d.name.mean()
d.location.length()
Type Level .keys and __getitem__
Another option would be to mirror how Typescript does this, by introducing type level keys and __gettitem__ functions. This would also require generic to depend on other generics (https://github.com/python/mypy/issues/2756).
T = TypeVar("T", bound=Dict[str, Column])
K = TypeVar("K", bound=KeyOf[T])
class Dataframe(Generic[T]):
def __init__(self, cols: T):
self.cols = cols
def __getattr__(self, name: K) -> GetItem[T, K]:
return self.cols[name]
Conclusion
I would like to have a way to type Dataframes that have different column types in a generic way. This is useful for typing frameworks like Ibis or Pandas.
This is somewhat related to variadic generics I believe (https://github.com/python/typing/issues/193). Also related: https://github.com/dropbox/sqlalchemy-stubs/issues/69
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
実装ファイル、テスト、エントリポイントは指定されていません。まず、提案されている TypedDict と type-level keys のアプローチを読み、続いて匿名 TypedDict、ジェネリック依存関係、可変長ジェネリクスに関するリンク先の議論を確認してください。異なる列型を持つ dataframes を型付けするための合意された汎用的な方法が必要になりますが、この issue では具体的な実装やテスト計画は定義されていません。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100