question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hybrid_property support

See original GitHub issue

Hi, I saw the thread a this topic here, but can’t haven’t seen any TODOs in this repo for it, so I’ll go ahead and make one.

We need support to infer types for hybrid_property similar to property, returning different types based on whether it’s being accessed on a class or an instance, and support for the @<property>.expression decorator.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
ckarnellcommented, Nov 25, 2019

If anyone’s reading this and is curious if there’s a quick way to get an easy (but janky) fix here, you can do something like this to basically sub in mypy’s type checking for property for your hybrid_propertys by using a third variable called typed_hybrid_property who’s type changes depending on whether it’s runtime.

if TYPE_CHECKING:
    # Use this to make hybrid_property's have the same typing as a normal property until stubs are improved.
    typed_hybrid_property = property
else:
    from sqlalchemy.ext.hybrid import hybrid_property as typed_hybrid_property

This also allows mypy to type expression functions, which is nice.

3reactions
zzzeekcommented, Feb 19, 2021

Here we go, this is just about the whole thing, how about this

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], Union[_T, ColumnElement[_T]]],
        expr: Optional[Callable[[Any], ColumnElement[_T]]] = None,
    ):
        self.fget = fget
        if expr is None:
            self.expr = fget
        else:
            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:

    # seems like "use the name twice" pattern isn't accepted by
    # mypy, so use two separate names?

    @hybrid_property
    def _my_thing_inst(self) -> int:
        return 5

    @_my_thing_inst.expression
    def my_thing(cls) -> "ColumnElement[int]":
        return column("five", Integer)


mc = MyClass()

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hybrid Attributes — SQLAlchemy 2.0 Documentation
“hybrid” means the attribute has distinct behaviors defined at the class level ... New in version 1.2: added support for bulk updates to...
Read more >
What is the difference between marital, separate and hybrid ...
Hybrid property includes assets that may have once been separate, but over time has generated income, increased in value and/or is mixed with...
Read more >
Equitable Distribution: What is Hybrid Property?
A marital residence is often hybrid property when one party purchased it prior to the marriage with their separate funds, and then the...
Read more >
Property Management Services
We take on the responsibility of bringing in new tenants, rent payments, handling maintenance issues, and responding to tenant complaints and concerns. Hybrid...
Read more >
How to Support the Hybrid Work Model - CommercialSearch
On day two of CoreNet's Corporate Real Estate Week 2021 several panels addressed the shifts in workforce planning and how landlords and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found