diff options
author | Federico Caselli <cfederico87@gmail.com> | 2021-12-22 21:45:45 +0100 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-12-30 18:07:26 -0500 |
commit | e913ec8155b64e055f3a88ca9c1bb7f112202c76 (patch) | |
tree | b0847baac43de628cd74bc2ff70cc352bbcb0270 /lib/sqlalchemy/dialects/postgresql/dml.py | |
parent | 54875c21601eaca01e3217d5b22fab6f6cf50992 (diff) | |
download | sqlalchemy-e913ec8155b64e055f3a88ca9c1bb7f112202c76.tar.gz |
Properly type _generative, decorator, public_factory
Good new is that pylance likes it and copies over the
singature and everything.
Bad news is that mypy does not support this yet https://github.com/python/mypy/issues/8645
Other minor bad news is that non_generative is not typed. I've tried using a protocol
like the one in the comment but the signature is not ported over by pylance, so it's
probably best to just live without it to have the correct signature.
notes from mike: these three decorators are at the core of getting
the library to be typed, more good news is that pylance will
do all the things we like re: public_factory, see
https://github.com/microsoft/pyright/issues/2758#issuecomment-1002788656
.
For @_generative, we will likely move to using pep 673 once mypy
supports it which may be soon. but overall having the explicit
"return self" in the methods, while a little inconvenient, makes
the typing more straightforward and locally present in the files
rather than being decided at a distance. having "return self"
present, or not, both have problems, so maybe we will be able
to change it again if things change as far as decorator support.
As it is, I feel like we are barely squeaking by with our decorators,
the typing is already pretty out there.
Change-Id: Ic77e13fc861def76a5925331df85c0aa48d77807
References: #6810
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/dml.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/dml.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/dml.py b/lib/sqlalchemy/dialects/postgresql/dml.py index 4451639f3..aa21bd8c0 100644 --- a/lib/sqlalchemy/dialects/postgresql/dml.py +++ b/lib/sqlalchemy/dialects/postgresql/dml.py @@ -4,6 +4,7 @@ # # This module is part of SQLAlchemy and is released under # the MIT License: https://www.opensource.org/licenses/mit-license.php +import typing from . import ext from ... import util @@ -21,6 +22,8 @@ from ...util.langhelpers import public_factory __all__ = ("Insert", "insert") +SelfInsert = typing.TypeVar("SelfInsert", bound="Insert") + class Insert(StandardInsert): """PostgreSQL-specific implementation of INSERT. @@ -75,13 +78,13 @@ class Insert(StandardInsert): @_generative @_on_conflict_exclusive def on_conflict_do_update( - self, + self: SelfInsert, constraint=None, index_elements=None, index_where=None, set_=None, where=None, - ): + ) -> SelfInsert: r""" Specifies a DO UPDATE SET action for ON CONFLICT clause. @@ -138,12 +141,16 @@ class Insert(StandardInsert): self._post_values_clause = OnConflictDoUpdate( constraint, index_elements, index_where, set_, where ) + return self @_generative @_on_conflict_exclusive def on_conflict_do_nothing( - self, constraint=None, index_elements=None, index_where=None - ): + self: SelfInsert, + constraint=None, + index_elements=None, + index_where=None, + ) -> SelfInsert: """ Specifies a DO NOTHING action for ON CONFLICT clause. @@ -173,6 +180,7 @@ class Insert(StandardInsert): self._post_values_clause = OnConflictDoNothing( constraint, index_elements, index_where ) + return self insert = public_factory( |