_T = TypeVar("_T", bound=Any)
_MP = TypeVar("_MP", bound="MapperProperty[Any]")
+_Fn = TypeVar("_Fn", bound="Callable[..., Any]")
+
_WithPolymorphicArg = Union[
Literal["*"],
return fn
-def validates(*names, **kw):
+def validates(
+ *names: str, include_removes: bool = False, include_backrefs: bool = False
+) -> Callable[[_Fn], _Fn]:
r"""Decorate a method as a 'validator' for one or more named properties.
Designates a method as a validator, a method which receives the
:ref:`simple_validators` - usage examples for :func:`.validates`
"""
- include_removes = kw.pop("include_removes", False)
- include_backrefs = kw.pop("include_backrefs", True)
- def wrap(fn):
- fn.__sa_validators__ = names
- fn.__sa_validation_opts__ = {
+ def wrap(fn: _Fn) -> _Fn:
+ fn.__sa_validators__ = names # type: ignore[attr-defined]
+ fn.__sa_validation_opts__ = { # type: ignore[attr-defined]
"include_removes": include_removes,
"include_backrefs": include_backrefs,
}
--- /dev/null
+from sqlalchemy.orm import DeclarativeBase
+from sqlalchemy.orm import Mapped
+from sqlalchemy.orm import mapped_column
+from sqlalchemy.orm import validates
+
+
+class Base(DeclarativeBase):
+ pass
+
+
+class User(Base):
+ __tablename__ = "User"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ name: Mapped[str]
+
+ @validates("name", include_removes=True)
+ def validate_name(self, name: str) -> str:
+ """test #8577"""
+ return name + "hi"