Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unused 'charset' in generate schema encoders #1817

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ ENV/
# MyPy caches
.mypy_cache/

# macos
.DS_Store
3 changes: 2 additions & 1 deletion tests/schema/models_postgres_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tortoise import Model, fields
from tortoise.contrib.postgres.fields import TSVectorField
from tortoise.contrib.postgres.fields import TSVectorField, ArrayField
from tortoise.contrib.postgres.indexes import (
BloomIndex,
BrinIndex,
Expand All @@ -19,6 +19,7 @@ class Index(Model):
sp_gist = fields.CharField(max_length=200)
hash = fields.CharField(max_length=200)
partial = fields.CharField(max_length=200)
array = ArrayField(element_type="text", default=["a", "b", "c"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Index model is used for testing indexes specifically and there is no index on array in this case. It would be better to use another model for testing arrays.

It might make sense to add a new test or find a more suitable one.


class Meta:
indexes = [
Expand Down
6 changes: 4 additions & 2 deletions tests/schema/test_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,8 @@ async def test_index_unsafe(self):
"gist" TSVECTOR NOT NULL,
"sp_gist" VARCHAR(200) NOT NULL,
"hash" VARCHAR(200) NOT NULL,
"partial" VARCHAR(200) NOT NULL
"partial" VARCHAR(200) NOT NULL,
"array" TEXT[] NOT NULL DEFAULT ('a','b','c')
);
CREATE INDEX "idx_index_bloom_280137" ON "index" USING BLOOM ("bloom");
CREATE INDEX "idx_index_brin_a54a00" ON "index" USING BRIN ("brin");
Expand All @@ -1118,7 +1119,8 @@ async def test_index_safe(self):
"gist" TSVECTOR NOT NULL,
"sp_gist" VARCHAR(200) NOT NULL,
"hash" VARCHAR(200) NOT NULL,
"partial" VARCHAR(200) NOT NULL
"partial" VARCHAR(200) NOT NULL,
"array" TEXT[] NOT NULL DEFAULT ('a','b','c')
);
CREATE INDEX IF NOT EXISTS "idx_index_bloom_280137" ON "index" USING BLOOM ("bloom");
CREATE INDEX IF NOT EXISTS "idx_index_brin_a54a00" ON "index" USING BRIN ("brin");
Expand Down
2 changes: 1 addition & 1 deletion tortoise/contrib/postgres/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TSVectorField(Field):
class ArrayField(Field, list): # type: ignore
def __init__(self, element_type: str = "int", **kwargs: Any):
super().__init__(**kwargs)
self.element_type = element_type
self.element_type = element_type.upper()

@property
def SQL_TYPE(self) -> str: # type: ignore
Expand Down
19 changes: 8 additions & 11 deletions tortoise/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _escape_unicode(value: str, mapping=None) -> str:
escape_string = _escape_unicode


def escape_item(val: Any, charset, mapping=None) -> str:
def escape_item(val: Any, mapping=None) -> str:
if mapping is None:
mapping = encoders
encoder = mapping.get(type(val))
Expand All @@ -37,31 +37,28 @@ def escape_item(val: Any, charset, mapping=None) -> str:
except KeyError:
raise TypeError("no default type converter defined")

if encoder in (escape_dict, escape_sequence):
val = encoder(val, charset, mapping)
else:
val = encoder(val, mapping)
val = encoder(val, mapping)
return val


def escape_dict(val: Dict, charset, mapping=None) -> dict:
def escape_dict(val: Dict, mapping=None) -> dict:
n = {}
for k, v in val.items():
quoted = escape_item(v, charset, mapping)
quoted = escape_item(v, mapping)
n[k] = quoted
return n


def escape_sequence(val: Sequence, charset, mapping=None) -> str:
def escape_sequence(val: Sequence, mapping=None) -> str:
n = []
for item in val:
quoted = escape_item(item, charset, mapping)
quoted = escape_item(item, mapping)
n.append(quoted)
return "(" + ",".join(n) + ")"


def escape_set(val: Set, charset, mapping=None) -> str:
return ",".join([escape_item(x, charset, mapping) for x in val])
def escape_set(val: Set, mapping=None) -> str:
return ",".join([escape_item(x, mapping) for x in val])


def escape_bool(value: bool, mapping=None) -> str:
Expand Down