Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Hybrid properties don't work in the presence of future annotations

Abierto
#389 6 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python, sqlalchemy
Área
api, backend

Línea de trabajo

Comienza reproduciendo el ejemplo con from __future__ import annotations; después, inspecciona graphene_sqlalchemy/converter.py alrededor de forward_reference_solver y el manejo de referencias adelantadas cerca de la línea 610. Confirma que las propiedades híbridas que devuelven tipos primitivos funcionan sin definiciones de campos proporcionadas manualmente, mientras que las referencias adelantadas de los modelos conservan su comportamiento actual.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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?

Lenguaje dominante
Python
Estrellas
985
Forks
224
Métricas de merge de PR
Sin PR fusionados en 30 d

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de graphql-python/graphene-sqlalchemy

Todos los issues de graphql-python/graphene-sqlalchemy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.