Skip to content

no implementation for hybrids available #171

Open
@zzzeek

Description

@zzzeek

re: #170

this worked for sqlalchemy-stubs, but does not seem to work here

POC from dropbox/sqlalchemy-stubs#98 (comment)

from typing import Any
from typing import Callable
from typing import Generic
from typing import Optional
from typing import overload
from typing import Type
from typing import TypeVar
from typing import Union

from sqlalchemy import column
from sqlalchemy import Integer
from sqlalchemy.sql import ColumnElement

_T = TypeVar("_T")


class hybrid_property(Generic[_T]):
    def __init__(
        self,
        fget: Callable[[Any], _T],
        expr: Callable[[Any], ColumnElement[_T]],
    ):
        self.fget = fget
        self.expr = expr

    @overload
    def __get__(
        self, instance: None, owner: Optional[Type[Any]]
    ) -> "ColumnElement[_T]":
        ...

    @overload
    def __get__(self, instance: object, owner: Optional[Type[Any]]) -> _T:
        ...

    def __get__(
        self, instance: Union[object, None], owner: Optional[Type[Any]] = None
    ) -> Any:
        if instance is None:
            return self.expr(owner)
        else:
            return self.fget(instance)

    def expression(
        self, expr: "Callable[[Any], ColumnElement[_T]]"
    ) -> "hybrid_property[_T]":
        return hybrid_property(self.fget, expr)


class MyClass:
    def my_thing_inst(self) -> int:
        return 5

    def my_thing_expr(cls) -> "ColumnElement[int]":
        return column("five", Integer)

    my_thing = hybrid_property(my_thing_inst, my_thing_expr)


mc = MyClass()

int_value: int = mc.my_thing
expr: ColumnElement[int] = MyClass.my_thing

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions