From 03128b9a58a325f1df0f5886441edc9854d9ec36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leynier=20Guti=C3=A9rrez=20Gonz=C3=A1lez?= Date: Wed, 17 Nov 2021 01:53:30 +0000 Subject: [PATCH 1/3] fix: interpolations erros and other things reported by sourcery-ai --- CODE_OF_CONDUCT.md | 22 ++++++++++------------ CONTRIBUTING.md | 2 +- Makefile | 17 +++++++++++++++++ postgrest_py/_async/client.py | 3 +-- postgrest_py/_async/request_builder.py | 8 ++++---- postgrest_py/_sync/client.py | 3 +-- postgrest_py/_sync/request_builder.py | 8 ++++---- tests/_async/test_request_builder.py | 4 ++-- tests/_sync/test_request_builder.py | 4 ++-- 9 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 Makefile diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index ace8809a..c42431d6 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -14,21 +14,21 @@ appearance, race, religion, or sexual identity and orientation. Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery and unwelcome sexual attention or +- The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a +- Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities @@ -74,5 +74,3 @@ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.ht For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq - - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 559a740a..04b91429 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,4 +29,4 @@ When making pull requests to the repository, make sure to follow these guideline - In your PR's description, link to any related issues or pull requests to give reviewers the full context of your change. - For commit messages, follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format. - For example, if you update documentation for a specific extension, your commit message might be: `docs(extension-name) updated installation documentation`. -diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md + diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..d05bd9a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +install: install_poetry + poetry install + +install_poetry: + curl -sSL https://install.python-poetry.org | python - + +precommit: install + poetry run pre-commit run --all-files + +tests: install + poetry run pytest --cov=./ --cov-report=xml -vv + +build: install + poetry run unasync postgrest_py tests + +clean: + sudo rm -r .venv diff --git a/postgrest_py/_async/client.py b/postgrest_py/_async/client.py index 1fa1acbb..e1c11dc8 100644 --- a/postgrest_py/_async/client.py +++ b/postgrest_py/_async/client.py @@ -76,5 +76,4 @@ def from_table(self, table: str) -> AsyncRequestBuilder: async def rpc(self, func: str, params: dict) -> Response: """Perform a stored procedure call.""" path = f"/rpc/{func}" - r = await self.session.post(path, json=params) - return r + return await self.session.post(path, json=params) diff --git a/postgrest_py/_async/request_builder.py b/postgrest_py/_async/request_builder.py index aa7e99c4..9c6ce9c8 100644 --- a/postgrest_py/_async/request_builder.py +++ b/postgrest_py/_async/request_builder.py @@ -144,17 +144,17 @@ def in_(self, column: str, values: Iterable[str]): def cs(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "cs", f"{{values}}") + return self.filter(column, "cs", f"{{{values}}}") def cd(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "cd", f"{{values}}") + return self.filter(column, "cd", f"{{{values}}}") def ov(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "ov", f"{{values}}") + return self.filter(column, "ov", f"{{{values}}}") def sl(self, column: str, range: Tuple[int, int]): return self.filter(column, "sl", f"({range[0]},{range[1]})") @@ -173,7 +173,7 @@ def adj(self, column: str, range: Tuple[int, int]): def match(self, query: Dict[str, Any]): updated_query = None - for key in query.keys(): + for key in query: value = query.get(key, "") updated_query = self.eq(key, value) return updated_query diff --git a/postgrest_py/_sync/client.py b/postgrest_py/_sync/client.py index db2948ef..898d4c6e 100644 --- a/postgrest_py/_sync/client.py +++ b/postgrest_py/_sync/client.py @@ -76,5 +76,4 @@ def from_table(self, table: str) -> SyncRequestBuilder: def rpc(self, func: str, params: dict) -> Response: """Perform a stored procedure call.""" path = f"/rpc/{func}" - r = self.session.post(path, json=params) - return r + return self.session.post(path, json=params) diff --git a/postgrest_py/_sync/request_builder.py b/postgrest_py/_sync/request_builder.py index cb1d979c..08c784b1 100644 --- a/postgrest_py/_sync/request_builder.py +++ b/postgrest_py/_sync/request_builder.py @@ -144,17 +144,17 @@ def in_(self, column: str, values: Iterable[str]): def cs(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "cs", f"{{values}}") + return self.filter(column, "cs", f"{{{values}}}") def cd(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "cd", f"{{values}}") + return self.filter(column, "cd", f"{{{values}}}") def ov(self, column: str, values: Iterable[str]): values = map(sanitize_param, values) values = ",".join(values) - return self.filter(column, "ov", f"{{values}}") + return self.filter(column, "ov", f"{{{values}}}") def sl(self, column: str, range: Tuple[int, int]): return self.filter(column, "sl", f"({range[0]},{range[1]})") @@ -173,7 +173,7 @@ def adj(self, column: str, range: Tuple[int, int]): def match(self, query: Dict[str, Any]): updated_query = None - for key in query.keys(): + for key in query: value = query.get(key, "") updated_query = self.eq(key, value) return updated_query diff --git a/tests/_async/test_request_builder.py b/tests/_async/test_request_builder.py index bb77c764..2ea19dbd 100644 --- a/tests/_async/test_request_builder.py +++ b/tests/_async/test_request_builder.py @@ -19,14 +19,14 @@ def test_select(self, request_builder: AsyncRequestBuilder): builder = request_builder.select("col1", "col2") assert builder.session.params["select"] == "col1,col2" - assert builder.session.headers.get("prefer") == None + assert builder.session.headers.get("prefer") is None assert builder.http_method == "GET" assert builder.json == {} def test_select_with_count(self, request_builder: AsyncRequestBuilder): builder = request_builder.select(count="exact") - assert builder.session.params.get("select") == None + assert builder.session.params.get("select") is None assert builder.session.headers["prefer"] == "count=exact" assert builder.http_method == "HEAD" assert builder.json == {} diff --git a/tests/_sync/test_request_builder.py b/tests/_sync/test_request_builder.py index 1eedae7a..61a53375 100644 --- a/tests/_sync/test_request_builder.py +++ b/tests/_sync/test_request_builder.py @@ -19,14 +19,14 @@ def test_select(self, request_builder: SyncRequestBuilder): builder = request_builder.select("col1", "col2") assert builder.session.params["select"] == "col1,col2" - assert builder.session.headers.get("prefer") == None + assert builder.session.headers.get("prefer") is None assert builder.http_method == "GET" assert builder.json == {} def test_select_with_count(self, request_builder: SyncRequestBuilder): builder = request_builder.select(count="exact") - assert builder.session.params.get("select") == None + assert builder.session.params.get("select") is None assert builder.session.headers["prefer"] == "count=exact" assert builder.http_method == "HEAD" assert builder.json == {} From bba1ff45b8f209b0dac005ae09bcdf08420b901f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leynier=20Guti=C3=A9rrez=20Gonz=C3=A1lez?= Date: Sun, 2 Jan 2022 17:55:33 +0000 Subject: [PATCH 2/3] feat: add return mode like a parameter --- postgrest_py/_async/client.py | 2 +- postgrest_py/_async/request_builder.py | 28 +++++++++++++++++++++++--- postgrest_py/_sync/client.py | 2 +- postgrest_py/_sync/request_builder.py | 28 +++++++++++++++++++++++--- postgrest_py/base_request_builder.py | 24 +++++++++++++--------- postgrest_py/constants.py | 5 +++++ 6 files changed, 71 insertions(+), 18 deletions(-) diff --git a/postgrest_py/_async/client.py b/postgrest_py/_async/client.py index aa0a90ce..502a36de 100644 --- a/postgrest_py/_async/client.py +++ b/postgrest_py/_async/client.py @@ -45,7 +45,7 @@ async def aclose(self) -> None: def from_(self, table: str) -> AsyncRequestBuilder: """Perform a table operation.""" return AsyncRequestBuilder(self.session, f"/{table}") - + def table(self, table: str) -> AsyncRequestBuilder: """Alias to self.from_().""" return self.from_(table) diff --git a/postgrest_py/_async/request_builder.py b/postgrest_py/_async/request_builder.py index c058940f..2f1535de 100644 --- a/postgrest_py/_async/request_builder.py +++ b/postgrest_py/_async/request_builder.py @@ -11,6 +11,7 @@ pre_upsert, process_response, ) +from postgrest_py.constants import ReturnMethod from postgrest_py.utils import AsyncClient @@ -78,6 +79,7 @@ def insert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, upsert=False, ) -> AsyncQueryRequestBuilder: method, json = pre_insert( @@ -85,6 +87,7 @@ def insert( self.path, json, count=count, + returning=returning, upsert=upsert, ) return AsyncQueryRequestBuilder(self.session, self.path, method, json) @@ -94,6 +97,7 @@ def upsert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ignore_duplicates=False, ) -> AsyncQueryRequestBuilder: method, json = pre_upsert( @@ -101,6 +105,7 @@ def upsert( self.path, json, count=count, + returning=returning, ignore_duplicates=ignore_duplicates, ) return AsyncQueryRequestBuilder(self.session, self.path, method, json) @@ -110,10 +115,27 @@ def update( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ) -> AsyncFilterRequestBuilder: - method, json = pre_update(self.session, self.path, json, count=count) + method, json = pre_update( + self.session, + self.path, + json, + count=count, + returning=returning, + ) return AsyncFilterRequestBuilder(self.session, self.path, method, json) - def delete(self, *, count: Optional[CountMethod] = None) -> AsyncFilterRequestBuilder: - method, json = pre_delete(self.session, self.path, count=count) + def delete( + self, + *, + count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, + ) -> AsyncFilterRequestBuilder: + method, json = pre_delete( + self.session, + self.path, + count=count, + returning=returning, + ) return AsyncFilterRequestBuilder(self.session, self.path, method, json) diff --git a/postgrest_py/_sync/client.py b/postgrest_py/_sync/client.py index 192271cd..d3672db7 100644 --- a/postgrest_py/_sync/client.py +++ b/postgrest_py/_sync/client.py @@ -45,7 +45,7 @@ def aclose(self) -> None: def from_(self, table: str) -> SyncRequestBuilder: """Perform a table operation.""" return SyncRequestBuilder(self.session, f"/{table}") - + def table(self, table: str) -> SyncRequestBuilder: """Alias to self.from_().""" return self.from_(table) diff --git a/postgrest_py/_sync/request_builder.py b/postgrest_py/_sync/request_builder.py index 01cd3a97..0bbd8654 100644 --- a/postgrest_py/_sync/request_builder.py +++ b/postgrest_py/_sync/request_builder.py @@ -11,6 +11,7 @@ pre_upsert, process_response, ) +from postgrest_py.constants import ReturnMethod from postgrest_py.utils import SyncClient @@ -78,6 +79,7 @@ def insert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, upsert=False, ) -> SyncQueryRequestBuilder: method, json = pre_insert( @@ -85,6 +87,7 @@ def insert( self.path, json, count=count, + returning=returning, upsert=upsert, ) return SyncQueryRequestBuilder(self.session, self.path, method, json) @@ -94,6 +97,7 @@ def upsert( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ignore_duplicates=False, ) -> SyncQueryRequestBuilder: method, json = pre_upsert( @@ -101,6 +105,7 @@ def upsert( self.path, json, count=count, + returning=returning, ignore_duplicates=ignore_duplicates, ) return SyncQueryRequestBuilder(self.session, self.path, method, json) @@ -110,10 +115,27 @@ def update( json: dict, *, count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, ) -> SyncFilterRequestBuilder: - method, json = pre_update(self.session, self.path, json, count=count) + method, json = pre_update( + self.session, + self.path, + json, + count=count, + returning=returning, + ) return SyncFilterRequestBuilder(self.session, self.path, method, json) - def delete(self, *, count: Optional[CountMethod] = None) -> SyncFilterRequestBuilder: - method, json = pre_delete(self.session, self.path, count=count) + def delete( + self, + *, + count: Optional[CountMethod] = None, + returning: ReturnMethod = ReturnMethod.representation, + ) -> SyncFilterRequestBuilder: + method, json = pre_delete( + self.session, + self.path, + count=count, + returning=returning, + ) return SyncFilterRequestBuilder(self.session, self.path, method, json) diff --git a/postgrest_py/base_request_builder.py b/postgrest_py/base_request_builder.py index 0ebbb893..cdcdcef0 100644 --- a/postgrest_py/base_request_builder.py +++ b/postgrest_py/base_request_builder.py @@ -3,7 +3,7 @@ from httpx import Response -from postgrest_py.constants import CountMethod, Filters, RequestMethod +from postgrest_py.constants import CountMethod, Filters, RequestMethod, ReturnMethod from postgrest_py.utils import ( AsyncClient, SyncClient, @@ -33,10 +33,11 @@ def pre_insert( path: str, json: dict, *, - count: Optional[CountMethod] = None, - upsert=False, + count: Optional[CountMethod], + returning: ReturnMethod, + upsert: bool, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") if upsert: @@ -50,8 +51,9 @@ def pre_upsert( path: str, json: dict, *, - count: Optional[CountMethod] = None, - ignore_duplicates=False, + count: Optional[CountMethod], + returning: ReturnMethod, + ignore_duplicates: bool, ) -> Tuple[RequestMethod, dict]: prefer_headers = ["return=representation"] if count: @@ -67,9 +69,10 @@ def pre_update( path: str, json: dict, *, - count: Optional[CountMethod] = None, + count: Optional[CountMethod], + returning: ReturnMethod, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") session.headers["prefer"] = ",".join(prefer_headers) @@ -80,9 +83,10 @@ def pre_delete( session: Union[AsyncClient, SyncClient], path: str, *, - count: Optional[CountMethod] = None, + count: Optional[CountMethod], + returning: ReturnMethod, ) -> Tuple[RequestMethod, dict]: - prefer_headers = ["return=representation"] + prefer_headers = [f"return={returning}"] if count: prefer_headers.append(f"count={count}") session.headers["prefer"] = ",".join(prefer_headers) diff --git a/postgrest_py/constants.py b/postgrest_py/constants.py index 93c294e5..652074bc 100644 --- a/postgrest_py/constants.py +++ b/postgrest_py/constants.py @@ -40,3 +40,8 @@ class RequestMethod(str, Enum): PUT = "PUT" DELETE = "DELETE" HEAD = "HEAD" + + +class ReturnMethod(str, Enum): + minimal = "minimal" + representation = "representation" From 59456d4f8dbd83bf30c37a161c7cc1a30ce0df48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leynier=20Guti=C3=A9rrez=20Gonz=C3=A1lez?= Date: Sun, 2 Jan 2022 18:17:42 +0000 Subject: [PATCH 3/3] chore: change constants.py by types.py --- postgrest_py/_async/request_builder.py | 2 +- postgrest_py/_sync/request_builder.py | 2 +- postgrest_py/base_request_builder.py | 2 +- postgrest_py/{constants.py => types.py} | 0 tests/_async/test_request_builder.py | 2 +- tests/_sync/test_request_builder.py | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename postgrest_py/{constants.py => types.py} (100%) diff --git a/postgrest_py/_async/request_builder.py b/postgrest_py/_async/request_builder.py index 2f1535de..5566b6f4 100644 --- a/postgrest_py/_async/request_builder.py +++ b/postgrest_py/_async/request_builder.py @@ -11,7 +11,7 @@ pre_upsert, process_response, ) -from postgrest_py.constants import ReturnMethod +from postgrest_py.types import ReturnMethod from postgrest_py.utils import AsyncClient diff --git a/postgrest_py/_sync/request_builder.py b/postgrest_py/_sync/request_builder.py index 0bbd8654..3144cbd9 100644 --- a/postgrest_py/_sync/request_builder.py +++ b/postgrest_py/_sync/request_builder.py @@ -11,7 +11,7 @@ pre_upsert, process_response, ) -from postgrest_py.constants import ReturnMethod +from postgrest_py.types import ReturnMethod from postgrest_py.utils import SyncClient diff --git a/postgrest_py/base_request_builder.py b/postgrest_py/base_request_builder.py index cdcdcef0..a958733c 100644 --- a/postgrest_py/base_request_builder.py +++ b/postgrest_py/base_request_builder.py @@ -3,7 +3,7 @@ from httpx import Response -from postgrest_py.constants import CountMethod, Filters, RequestMethod, ReturnMethod +from postgrest_py.types import CountMethod, Filters, RequestMethod, ReturnMethod from postgrest_py.utils import ( AsyncClient, SyncClient, diff --git a/postgrest_py/constants.py b/postgrest_py/types.py similarity index 100% rename from postgrest_py/constants.py rename to postgrest_py/types.py diff --git a/tests/_async/test_request_builder.py b/tests/_async/test_request_builder.py index 385a6337..85339b01 100644 --- a/tests/_async/test_request_builder.py +++ b/tests/_async/test_request_builder.py @@ -1,7 +1,7 @@ import pytest from postgrest_py import AsyncRequestBuilder -from postgrest_py.constants import CountMethod +from postgrest_py.types import CountMethod from postgrest_py.utils import AsyncClient diff --git a/tests/_sync/test_request_builder.py b/tests/_sync/test_request_builder.py index dac03efe..4fb38500 100644 --- a/tests/_sync/test_request_builder.py +++ b/tests/_sync/test_request_builder.py @@ -1,7 +1,7 @@ import pytest from postgrest_py import SyncRequestBuilder -from postgrest_py.constants import CountMethod +from postgrest_py.types import CountMethod from postgrest_py.utils import SyncClient