From 487e9533c7debc2731ff383eff2f82b8e8766d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Fri, 8 Dec 2023 15:11:57 +0100 Subject: [PATCH] Fix some typing issues --- c2cgeoform/__init__.py | 4 ++-- c2cgeoform/schema.py | 14 ++++++++++++-- c2cgeoform/views/abstract_views.py | 14 ++++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/c2cgeoform/__init__.py b/c2cgeoform/__init__.py index 11acd3b0..1b637c9f 100644 --- a/c2cgeoform/__init__.py +++ b/c2cgeoform/__init__.py @@ -1,4 +1,4 @@ -from typing import Any, Union +from typing import Any, Tuple, Union import pyramid.config from deform import Form, widget @@ -16,7 +16,7 @@ """ Default search paths for the form templates. """ -default_search_paths = ( +default_search_paths: Tuple[str, ...] = ( resource_filename("c2cgeoform", "templates/widgets"), resource_filename("deform", "templates"), ) diff --git a/c2cgeoform/schema.py b/c2cgeoform/schema.py index 0e4b89ea..4b04ee8e 100644 --- a/c2cgeoform/schema.py +++ b/c2cgeoform/schema.py @@ -24,7 +24,13 @@ def deferred_dbsession(node: Any, kwargs: dict[str, Any]) -> sqlalchemy.orm.Sess return cast(sqlalchemy.orm.Session, kwargs.get("dbsession")) -def unique_validator(mapper: type[Any], column: str, id_column: str, node: str, value: Any) -> None: +def unique_validator( + mapper: type[Any], + column: sqlalchemy.orm.attributes.InstrumentedAttribute[Any], + id_column: sqlalchemy.orm.attributes.InstrumentedAttribute[Any], + node: str, + value: Any, +) -> None: dbsession: sqlalchemy.orm.Session = node.bindings["dbsession"] # type: ignore[attr-defined] _id = node.bindings["request"].matchdict["id"] # type: ignore[attr-defined] _id = _id if _id != "new" else None @@ -58,7 +64,11 @@ def __init__(self, *args: Any, **kw: Any) -> None: self.request = deferred_request self.dbsession = deferred_dbsession - def add_unique_validator(self, column: sqlalchemy.sql.elements.NamedColumn[Any], column_id: str) -> None: + def add_unique_validator( + self, + column: sqlalchemy.orm.attributes.InstrumentedAttribute[Any], + column_id: sqlalchemy.orm.attributes.InstrumentedAttribute[Any], + ) -> None: """ Adds an unique validator on this schema instance. diff --git a/c2cgeoform/views/abstract_views.py b/c2cgeoform/views/abstract_views.py index af92fe83..983e74dd 100644 --- a/c2cgeoform/views/abstract_views.py +++ b/c2cgeoform/views/abstract_views.py @@ -1,5 +1,5 @@ import logging -from typing import Any, Callable, Generic, Optional, TypedDict, TypeVar, Union, cast +from typing import Any, Callable, Dict, Generic, Optional, TypedDict, TypeVar, Union, cast import geojson import pyramid.request @@ -229,9 +229,11 @@ class _Index(TypedDict): class AbstractViews(Generic[T]): - _model = None # sqlalchemy model + _model: Optional[type[T]] = None # sqlalchemy model _list_fields: list[ListField[T]] = [] # Fields in list - _list_ordered_fields: list[sqlalchemy.schema.Column[Any]] = [] # Fields in list used for default orderby + _list_ordered_fields: list[ + Union[sqlalchemy.sql.elements.ColumnClause[Any], sqlalchemy.sql.elements.ColumnElement[Any]] + ] = [] # Fields in list used for default orderby _id_field: Optional[str] = None # Primary key _geometry_field: Optional[str] = None # Geometry field _base_schema: Optional[type[T]] = None # base colander schema @@ -244,8 +246,8 @@ class AbstractViews(Generic[T]): def __init__(self, request: pyramid.request.Request) -> None: self._request = request self._schema: Optional[str] = None - self._appstruct: Optional[str] = None - self._obj: Optional[type[T]] = None + self._appstruct: Optional[Dict[str, Any]] = None + self._obj: Optional[T] = None def index(self) -> _Index: return { @@ -571,7 +573,7 @@ def save(self) -> Union[JSONDict, pyramid.response.Response]: self._request.route_url( "c2cgeoform_item", action="edit", - id=self._obj.__getattribute__(self._id_field), # type: ignore[call-arg,arg-type] # pylint: disable=unnecessary-dunder-call + id=self._obj.__getattribute__(self._id_field), # type: ignore[arg-type] # pylint: disable=unnecessary-dunder-call _query=[("msg_col", "submit_ok")], ) )