Skip to content

Commit a1b0ded

Browse files
committed
lint, address review comment
1 parent 6edec21 commit a1b0ded

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

src/replit_river/codegen/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]:
575575
if name == "$kind":
576576
safe_name = "kind"
577577
else:
578-
# For TypedDict encoder, use normalized name to access the TypedDict field
579-
# but the output dictionary key should use the original name
578+
# For TypedDict encoder, use normalized name to access
579+
# the TypedDict field but the output dictionary key should
580+
# use the original name
580581
if base_model == "TypedDict":
581-
specialized_name = normalize_special_chars(name)
582-
safe_name = specialized_name.lstrip("_") if name != specialized_name else name
582+
safe_name = normalize_special_chars(name)
583583
else:
584584
safe_name = name
585585
if prop.type == "object" and not prop.patternProperties:
@@ -683,15 +683,17 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]:
683683
if name != specialized_name:
684684
if base_model == "BaseModel":
685685
# Pydantic doesn't allow leading underscores in field names
686-
effective_name = specialized_name.lstrip("_")
686+
effective_name = specialized_name
687687
extras.append(f"alias={repr(name)}")
688688
elif base_model == "TypedDict":
689689
# For TypedDict, we use the normalized name directly
690-
# TypedDict doesn't support aliases, so we normalize the field name
691-
effective_name = specialized_name.lstrip("_")
690+
# TypedDict doesn't support aliases, so we normalize
691+
# the field name
692+
effective_name = specialized_name
692693
else:
693-
# For RiverError (which extends BaseModel), use alias like BaseModel
694-
effective_name = specialized_name.lstrip("_")
694+
# For RiverError (which extends BaseModel), use alias
695+
# like BaseModel
696+
effective_name = specialized_name
695697
extras.append(f"alias={repr(name)}")
696698

697699
effective_field_names[effective_name].append(name)

src/replit_river/codegen/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def work(
165165
def normalize_special_chars(value: str) -> str:
166166
for char in SPECIAL_CHARS:
167167
value = value.replace(char, "_")
168-
return value
168+
return value.lstrip("_")
169169

170170

171171
def render_type_expr(value: TypeExpression) -> str:

tests/v1/codegen/test_input_special_chars.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77

88
def test_input_special_chars_basemodel() -> None:
9-
"""Test that codegen handles special characters in input field names for BaseModel."""
10-
11-
# Test should pass without raising an exception
9+
"""Handles special characters in input field names for BaseModel."""
10+
1211
schema_to_river_client_codegen(
13-
read_schema=lambda: open("tests/v1/codegen/rpc/input-special-chars-schema.json"),
12+
read_schema=lambda: open(
13+
"tests/v1/codegen/rpc/input-special-chars-schema.json"
14+
), # noqa: E501
1415
target_path="tests/v1/codegen/rpc/generated_input_special",
1516
client_name="InputSpecialClient",
1617
typed_dict_inputs=False, # BaseModel inputs
@@ -21,11 +22,13 @@ def test_input_special_chars_basemodel() -> None:
2122

2223

2324
def test_input_special_chars_typeddict() -> None:
24-
"""Test that codegen handles special characters in input field names for TypedDict."""
25-
25+
"""Handles special characters in input field names for TypedDict."""
26+
2627
# Test should pass without raising an exception
2728
schema_to_river_client_codegen(
28-
read_schema=lambda: open("tests/v1/codegen/rpc/input-special-chars-schema.json"),
29+
read_schema=lambda: open(
30+
"tests/v1/codegen/rpc/input-special-chars-schema.json"
31+
), # noqa: E501
2932
target_path="tests/v1/codegen/rpc/generated_input_special_td",
3033
client_name="InputSpecialTDClient",
3134
typed_dict_inputs=True, # TypedDict inputs
@@ -36,11 +39,13 @@ def test_input_special_chars_typeddict() -> None:
3639

3740

3841
def test_input_collision_error_basemodel() -> None:
39-
"""Test that codegen raises ValueError for input field name collisions with BaseModel."""
42+
"""Raises ValueError for input field name collisions with BaseModel."""
4043

4144
with pytest.raises(ValueError) as exc_info:
4245
schema_to_river_client_codegen(
43-
read_schema=lambda: open("tests/v1/codegen/rpc/input-collision-schema.json"),
46+
read_schema=lambda: open(
47+
"tests/v1/codegen/rpc/input-collision-schema.json"
48+
), # noqa: E501
4449
target_path="tests/v1/codegen/rpc/generated_input_collision",
4550
client_name="InputCollisionClient",
4651
typed_dict_inputs=False, # BaseModel inputs
@@ -58,11 +63,13 @@ def test_input_collision_error_basemodel() -> None:
5863

5964

6065
def test_input_collision_error_typeddict() -> None:
61-
"""Test that codegen raises ValueError for input field name collisions with TypedDict."""
66+
"""Raises ValueError for input field name collisions with TypedDict."""
6267

6368
with pytest.raises(ValueError) as exc_info:
6469
schema_to_river_client_codegen(
65-
read_schema=lambda: open("tests/v1/codegen/rpc/input-collision-schema.json"),
70+
read_schema=lambda: open(
71+
"tests/v1/codegen/rpc/input-collision-schema.json"
72+
), # noqa: E501
6673
target_path="tests/v1/codegen/rpc/generated_input_collision_td",
6774
client_name="InputCollisionTDClient",
6875
typed_dict_inputs=True, # TypedDict inputs
@@ -80,8 +87,8 @@ def test_input_collision_error_typeddict() -> None:
8087

8188

8289
def test_init_special_chars_basemodel() -> None:
83-
"""Test that codegen handles special characters in init field names for BaseModel."""
84-
90+
"""Handles special characters in init field names for BaseModel."""
91+
8592
init_schema = {
8693
"services": {
8794
"test_service": {
@@ -92,21 +99,21 @@ def test_init_special_chars_basemodel() -> None:
9299
"properties": {
93100
"init-field1": {"type": "string"},
94101
"init:field2": {"type": "number"},
95-
"init.field3": {"type": "boolean"}
102+
"init.field3": {"type": "boolean"},
96103
},
97-
"required": ["init-field1"]
104+
"required": ["init-field1"],
98105
},
99106
"output": {"type": "boolean"},
100107
"errors": {"not": {}},
101-
"type": "stream"
108+
"type": "stream",
102109
}
103110
}
104111
}
105112
}
106113
}
107-
114+
108115
import json
109-
116+
110117
# Test should pass without raising an exception
111118
schema_to_river_client_codegen(
112119
read_schema=lambda: StringIO(json.dumps(init_schema)),
@@ -120,8 +127,8 @@ def test_init_special_chars_basemodel() -> None:
120127

121128

122129
def test_init_special_chars_typeddict() -> None:
123-
"""Test that codegen handles special characters in init field names for TypedDict."""
124-
130+
"""Handles special characters in init field names for TypedDict."""
131+
125132
init_schema = {
126133
"services": {
127134
"test_service": {
@@ -132,21 +139,21 @@ def test_init_special_chars_typeddict() -> None:
132139
"properties": {
133140
"init-field1": {"type": "string"},
134141
"init:field2": {"type": "number"},
135-
"init.field3": {"type": "boolean"}
142+
"init.field3": {"type": "boolean"},
136143
},
137-
"required": ["init-field1"]
144+
"required": ["init-field1"],
138145
},
139146
"output": {"type": "boolean"},
140147
"errors": {"not": {}},
141-
"type": "stream"
148+
"type": "stream",
142149
}
143150
}
144151
}
145152
}
146153
}
147-
154+
148155
import json
149-
156+
150157
# Test should pass without raising an exception
151158
schema_to_river_client_codegen(
152159
read_schema=lambda: StringIO(json.dumps(init_schema)),

0 commit comments

Comments
 (0)