Hybrid properties don't work in the presence of future annotations
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
調査の方向性
まず from __future__ import annotations を使った例を再現し、次に forward_reference_solver 周辺の graphene_sqlalchemy/converter.py と、610 行付近の forward reference の処理を調べます。プリミティブ型を返すハイブリッドプロパティが、フィールド定義を手動で指定しなくても機能することを確認し、モデルの forward reference については既存の動作が維持されることを確認します。
索引モデルが issue の本文から書いたものです。
説明
This has been driving me a bit nuts since the columns/hybrids refactor, and I haven't been able to figure out if it's something new, expected, or something I'm doing wrong. Here's the simplest scenario I could come up with to demonstrate it.
@declarative_mixin
@as_declarative()
class Base:
pass
class Person(Base):
__tablename__ = "person"
id = Column(postgresql.UUID(as_uuid=True), primary_key=True)
@hybrid_property
def hello(self) -> str:
return "world"
class GPerson(SQLAlchemyObjectType):
class Meta:
model = Person
class GQuery(ObjectType):
person = Field(GPerson)
schema = Schema(query=GQuery)
I would expect this to work, and it does. It even works if I make my hello property return a list[list[str]] or something weird like that. But! If you import __future__ annotations, this scenario no longer works:
from __future__ import annotations
<the code from above>
Traceback (most recent call last):
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphql/type/definition.py", line 808, in fields
fields = resolve_thunk(self._fields)
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
return thunk() if callable(thunk) else thunk
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphene/types/schema.py", line 305, in create_fields_for_type
field_type = create_graphql_type(field.type)
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphene/types/field.py", line 116, in type
return get_type(self._type)
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphene/types/utils.py", line 42, in get_type
return _type()
File "/home/vscode/.local/share/virtualenvs/bios-backend-rDkHOn2N/lib/python3.10/site-packages/graphene_sqlalchemy/converter.py", line 615, in forward_reference_solver
raise TypeError(
TypeError: No model found in Registry for forward reference for type ForwardRef('str'). Only forward references to other SQLAlchemy Models mapped to SQLAlchemyObjectTypes are allowed.
This is because with __future__ annotations, -> str creates a ForwardRef, and forward refs (unlike normal types) have to be SQLAlchemy models (https://github.com/graphql-python/graphene-sqlalchemy/blob/master/graphene_sqlalchemy/converter.py#L610). So you have to explicitly supply hybrid field definitions, even for methods that return simple primitive types. This is a huge inconvenience, as we have many of these kinds of computed properties in our codebase.
Any ideas if this is something fixable? Or are we just stuck with having to manually add field definitions everywhere?
- 主要言語
- Python
- スター
- 985
- フォーク
- 224
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
graphql-python/graphene-sqlalchemy のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 38/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 52/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
graphql-python/graphene-sqlalchemy#422 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
graphql-python/graphene-sqlalchemy#419 · コメント 3 件 ·
graphql-python/graphene-sqlalchemy の issue をすべて見る
似ている issue
-
essnmx good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
-
[Feature] 奇物选择添加优先级 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
syfoud/Simulated_Scepter#174 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Giskard-AI/giskard-oss#2840 · コメント 1 件 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success オープンarea: repo bug perceived difficulty: 2
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
yeti-platform/yeti#1380 ·