Skip to content

Commit 7713f06

Browse files
committed
✨ Refactor string truncation types to use Annotated with StringConstraints and trim_string_before
1 parent 4364d1e commit 7713f06

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

packages/models-library/src/models_library/basic_types.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
SIMPLE_VERSION_RE,
1515
UUID_RE,
1616
)
17+
from .utils.common_validators import trim_string_before
1718

1819
assert issubclass(LogLevel, Enum) # nosec
1920
assert issubclass(BootModeEnum, Enum) # nosec
@@ -150,32 +151,36 @@ def concatenate(*args: "IDStr", link_char: str = " ") -> "IDStr":
150151
return IDStr(result)
151152

152153

153-
class ShortTruncatedStr(ConstrainedStr):
154-
"""A truncated string used to input e.g. titles or display names
155-
156-
- Strips whitespaces and truncate strings that exceed the specified characters limit (curtail_length).
157-
- Ensures that the **input** data length to the API is controlled and prevents exceeding large inputs silently,
158-
i.e. without raising errors.
159-
160-
SEE https://github.com/ITISFoundation/osparc-simcore/pull/5989#discussion_r1650506583
161-
162-
DEPRECATED: Use instead Annotated[str, StringConstraints(strip_whitespace=True), trim_string_before(max_length=600)]
163-
"""
164-
165-
strip_whitespace = True
166-
curtail_length = 600
167-
168-
169-
class LongTruncatedStr(ConstrainedStr):
170-
"""Use to input e.g. descriptions or summaries
171-
172-
Analogous to ShortTruncatedStr
173-
174-
DEPRECATED: Use instead Annotated[str, StringConstraints(strip_whitespace=True), trim_string_before(max_length=65536)]
175-
"""
154+
_SHORT_TRUNCATED_STR_MAX_LENGTH: Final[int] = 600
155+
ShortTruncatedStr: TypeAlias = Annotated[
156+
str,
157+
StringConstraints(strip_whitespace=True),
158+
trim_string_before(max_length=_SHORT_TRUNCATED_STR_MAX_LENGTH),
159+
annotated_types.doc(
160+
"""
161+
A truncated string used to input e.g. titles or display names.
162+
Strips whitespaces and truncate strings that exceed the specified characters limit (curtail_length).
163+
Ensures that the **input** data length to the API is controlled and prevents exceeding large inputs silently,
164+
i.e. without raising errors.
165+
"""
166+
# SEE https://github.com/ITISFoundation/osparc-simcore/pull/5989#discussion_r1650506583
167+
),
168+
]
176169

177-
strip_whitespace = True
178-
curtail_length = 65536 # same as github descripton
170+
_LONG_TRUNCATED_STR_MAX_LENGTH: Final[int] = 65536 # same as github description
171+
LongTruncatedStr: TypeAlias = Annotated[
172+
str,
173+
StringConstraints(strip_whitespace=True),
174+
trim_string_before(max_length=_LONG_TRUNCATED_STR_MAX_LENGTH),
175+
annotated_types.doc(
176+
"""
177+
A truncated string used to input e.g. descriptions or summaries.
178+
Strips whitespaces and truncate strings that exceed the specified characters limit (curtail_length).
179+
Ensures that the **input** data length to the API is controlled and prevents exceeding large inputs silently,
180+
i.e. without raising errors.
181+
"""
182+
),
183+
]
179184

180185

181186
# auto-incremented primary-key IDs

packages/models-library/tests/test_basic_types.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from models_library.basic_types import (
5+
_SHORT_TRUNCATED_STR_MAX_LENGTH,
56
EnvVarKey,
67
IDStr,
78
MD5Str,
@@ -76,16 +77,13 @@ def test_string_identifier_constraint_type():
7677

7778

7879
def test_short_truncated_string():
80+
curtail_length = _SHORT_TRUNCATED_STR_MAX_LENGTH
7981
assert (
80-
TypeAdapter(ShortTruncatedStr).validate_python(
81-
"X" * ShortTruncatedStr.curtail_length
82-
)
83-
== "X" * ShortTruncatedStr.curtail_length
82+
TypeAdapter(ShortTruncatedStr).validate_python("X" * curtail_length)
83+
== "X" * curtail_length
8484
)
8585

8686
assert (
87-
TypeAdapter(ShortTruncatedStr).validate_python(
88-
"X" * (ShortTruncatedStr.curtail_length + 1)
89-
)
90-
== "X" * ShortTruncatedStr.curtail_length
87+
TypeAdapter(ShortTruncatedStr).validate_python("X" * (curtail_length + 1))
88+
== "X" * curtail_length
9189
)

packages/models-library/tests/test_projects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import pytest
99
from faker import Faker
10-
from models_library.api_schemas_webserver.projects import LongTruncatedStr, ProjectPatch
10+
from models_library.api_schemas_webserver.projects import ProjectPatch
11+
from models_library.basic_types import _LONG_TRUNCATED_STR_MAX_LENGTH
1112
from models_library.projects import Project
1213

1314

@@ -47,8 +48,7 @@ def test_project_with_thumbnail_as_empty_string(minimal_project: dict[str, Any])
4748

4849
def test_project_patch_truncates_description():
4950
# NOTE: checks https://github.com/ITISFoundation/osparc-simcore/issues/5988
50-
assert LongTruncatedStr.curtail_length
51-
len_truncated = int(LongTruncatedStr.curtail_length)
51+
len_truncated = _LONG_TRUNCATED_STR_MAX_LENGTH
5252

5353
long_description = "X" * (len_truncated + 10)
5454
assert len(long_description) > len_truncated

0 commit comments

Comments
 (0)