Removed `Mapped[Entity]` type annotation.

`Mapped` has been introduced only in SQLAlchemy 1.4, while Debian stable
still ships 1.3.

Removing the type annotation doesn't come with a big cost, but it keeps
Platypush compatible with Debian stable.
This commit is contained in:
Fabio Manganiello 2023-04-24 21:48:52 +02:00
parent e955ffc018
commit 8478245cde
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 8 additions and 7 deletions

View File

@ -20,7 +20,7 @@ from sqlalchemy import (
UniqueConstraint, UniqueConstraint,
inspect as schema_inspect, inspect as schema_inspect,
) )
from sqlalchemy.orm import ColumnProperty, Mapped, backref, relationship from sqlalchemy.orm import ColumnProperty, backref, relationship
from sqlalchemy.orm.exc import ObjectDeletedError from sqlalchemy.orm.exc import ObjectDeletedError
from platypush.common.db import Base from platypush.common.db import Base
@ -84,7 +84,7 @@ if 'entity' not in Base.metadata:
onupdate=datetime.utcnow(), onupdate=datetime.utcnow(),
) )
parent: Mapped['Entity'] = relationship( parent = relationship(
'Entity', 'Entity',
remote_side=[id], remote_side=[id],
uselist=False, uselist=False,
@ -131,15 +131,16 @@ if 'entity' not in Base.metadata:
to reuse entity objects in other threads or outside of their to reuse entity objects in other threads or outside of their
associated SQLAlchemy session context. associated SQLAlchemy session context.
""" """
def key_value_pair(col: ColumnProperty): def key_value_pair(col: ColumnProperty):
try: try:
return (col.key, getattr(self, col.key, None)) return (col.key, getattr(self, col.key, None))
except ObjectDeletedError as e: except ObjectDeletedError:
return None return None
return self.__class__( return self.__class__(
**dict( **dict(
key_value_pair(col) key_value_pair(col) # type: ignore
for col in self.columns for col in self.columns
if key_value_pair(col) is not None if key_value_pair(col) is not None
), ),
@ -180,7 +181,7 @@ if 'entity' not in Base.metadata:
""" """
col_name = self._column_name(col) col_name = self._column_name(col)
if col_name is None: if col_name is None:
return tuple() return ()
return col_name, self._serialize_value(col_name) return col_name, self._serialize_value(col_name)
def to_dict(self) -> dict: def to_dict(self) -> dict:
@ -215,7 +216,7 @@ if 'entity' not in Base.metadata:
""" """
Serializes the new value before assigning it to an attribute. Serializes the new value before assigning it to an attribute.
""" """
matching_columns = [c for c in self.columns if c.expression.name == key] matching_columns = [c for c in self.columns if c.expression.name == key] # type: ignore
if ( if (
matching_columns matching_columns
@ -275,7 +276,7 @@ def _discover_entity_types():
for _, obj in inspect.getmembers(module): for _, obj in inspect.getmembers(module):
if inspect.isclass(obj) and issubclass(obj, Entity): if inspect.isclass(obj) and issubclass(obj, Entity):
entities_registry[obj] = {} entities_registry[obj] = {} # type: ignore
def get_entities_registry() -> EntityRegistryType: def get_entities_registry() -> EntityRegistryType: