Skip to content

Commit cdd1b55

Browse files
authored
Merge pull request #2308 from CosmWasm/tkulik/chore/update_python_codegen
chore: Update python codegen
2 parents f3f338c + d2b5896 commit cdd1b55

File tree

9 files changed

+240
-69
lines changed

9 files changed

+240
-69
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cw-schema-codegen/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ rand = { version = "0.8.5", features = ["min_const_gen"] }
2929
serde = { workspace = true, features = ["derive"] }
3030
serde_json = "1.0.128"
3131
tempfile = "3.14.0"
32+
cosmwasm-std = { version = "2.2.0-rc.1", path = "../std", default-features = false, features = [
33+
"std",
34+
] }

packages/cw-schema-codegen/src/python/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn expand_node_name<'a>(
1212
match node.value {
1313
cw_schema::NodeType::Array { items } => {
1414
let items = &schema.definitions[items];
15-
format!("{}[]", expand_node_name(schema, items)).into()
15+
format!("typing.List[{}]", expand_node_name(schema, items)).into()
1616
}
1717
cw_schema::NodeType::Float => "float".into(),
1818
cw_schema::NodeType::Double => "float".into(),
@@ -35,12 +35,11 @@ fn expand_node_name<'a>(
3535
format!("[{}]", items).into()
3636
}
3737
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),
38-
3938
cw_schema::NodeType::Decimal { .. } => "decimal.Decimal".into(),
4039
cw_schema::NodeType::Address => "str".into(),
41-
cw_schema::NodeType::Checksum => todo!(),
42-
cw_schema::NodeType::HexBinary => todo!(),
43-
cw_schema::NodeType::Timestamp => todo!(),
40+
cw_schema::NodeType::Checksum => "str".into(),
41+
cw_schema::NodeType::HexBinary => "str".into(),
42+
cw_schema::NodeType::Timestamp => "str".into(),
4443
cw_schema::NodeType::Unit => "None".into(),
4544
}
4645
}

packages/cw-schema-codegen/templates/python/struct.tpl.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@
88
{% match ty %}
99
{% when TypeTemplate::Unit %}
1010
class {{ name }}(RootModel):
11-
'''{% for doc in docs %}
11+
"""{% for doc in docs %}
1212
{{ doc }}
13-
{% endfor %}'''
13+
{% endfor %}"""
1414
root: None
1515
{% when TypeTemplate::Tuple with (types) %}
1616
class {{ name }}(RootModel):
17-
'''{% for doc in docs %}
17+
"""{% for doc in docs %}
1818
{{ doc }}
19-
{% endfor %}'''
19+
{% endfor %}"""
2020
root: typing.Tuple[{{ types|join(", ") }}]
2121
{% when TypeTemplate::Named with { fields } %}
2222
class {{ name }}(BaseModel):
23-
'''{% for doc in docs %}
23+
"""{% for doc in docs %}
2424
{{ doc }}
25-
{% endfor %}'''
25+
{% endfor %}"""
2626
{% for field in fields %}
2727
{{ field.name }}: {{ field.ty }}
28-
'''{% for doc in field.docs %}
28+
"""{% for doc in field.docs %}
2929
# {{ doc }}
30-
{% endfor %}'''
30+
{% endfor %}"""
3131
{% endfor %}
3232
{% endmatch %}

packages/cw-schema-codegen/tests/python_tpl.rs

+89-50
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,65 @@ use cw_schema::Schemaifier;
22
use serde::{Deserialize, Serialize};
33
use std::io::Write;
44

5-
#[derive(Schemaifier, Serialize, Deserialize)]
5+
/// This is a struct level documentation for enum type
6+
#[derive(Schemaifier, Serialize, Deserialize, PartialEq, Debug)]
67
pub enum SomeEnum {
8+
/// Field1 docs
79
Field1,
10+
11+
/// Field2 docs
812
Field2(u32, u32),
9-
Field3 { a: String, b: u32 },
10-
// Field4(Box<SomeEnum>), // TODO tkulik: Do we want to support Box<T> ?
11-
// Field5 { a: Box<SomeEnum> },
13+
14+
/// Field3 docs
15+
Field3 {
16+
/// `a` field docs
17+
a: String,
18+
19+
/// `b` field docs
20+
b: u32,
21+
},
1222
}
1323

14-
#[derive(Schemaifier, Serialize, Deserialize)]
24+
/// This is a struct level documentation for unit struct
25+
#[derive(Schemaifier, Serialize, Deserialize, PartialEq, Debug)]
1526
pub struct UnitStructure;
1627

17-
#[derive(Schemaifier, Serialize, Deserialize)]
28+
/// This is a struct level documentation for tuple
29+
#[derive(Schemaifier, Serialize, Deserialize, PartialEq, Debug)]
1830
pub struct TupleStructure(u32, String, u128);
1931

20-
#[derive(Schemaifier, Serialize, Deserialize)]
32+
/// This is a struct level documentation for named structure
33+
#[derive(Schemaifier, Serialize, Deserialize, PartialEq, Debug)]
2134
pub struct NamedStructure {
35+
/// `a` field docs
2236
a: String,
37+
38+
/// `b` field docs
2339
b: u8,
40+
41+
/// `c` field docs
2442
c: SomeEnum,
2543
}
2644

45+
#[derive(Schemaifier, Serialize, Deserialize, PartialEq, Debug)]
46+
pub struct AllSimpleTypesAndDocs {
47+
array_field: Vec<String>,
48+
float_field: f32,
49+
double_field: f64,
50+
bool_field: bool,
51+
string_field: String,
52+
int_field: i64,
53+
bytes_field: cosmwasm_std::Binary,
54+
opt_field: Option<String>,
55+
byte_field: u8,
56+
decimal_field: cosmwasm_std::Decimal,
57+
address_field: cosmwasm_std::Addr,
58+
checksum_field: cosmwasm_std::Checksum,
59+
hexbinary_field: cosmwasm_std::HexBinary,
60+
timestamp_field: cosmwasm_std::Timestamp,
61+
unit_field: (),
62+
}
63+
2764
#[test]
2865
fn simple_enum() {
2966
// generate the schemas for each of the above types
@@ -55,61 +92,63 @@ fn simple_enum() {
5592
}
5693

5794
macro_rules! validator {
58-
($typ:ty) => {{
59-
let a: Box<dyn FnOnce(&str)> = Box::new(|output| {
60-
serde_json::from_str::<$typ>(output).unwrap();
61-
});
62-
a
95+
($typ:ty, $example:expr) => {{
96+
(
97+
stringify!($typ),
98+
cw_schema::schema_of::<$typ>(),
99+
serde_json::to_string(&$example).unwrap(),
100+
{
101+
let a: Box<dyn FnOnce(&str)> = Box::new(|output| {
102+
let result = serde_json::from_str::<$typ>(output).unwrap();
103+
assert_eq!(result, $example);
104+
});
105+
a
106+
},
107+
)
63108
}};
64109
}
65110

66111
#[test]
67112
fn assert_validity() {
68113
let schemas = [
69-
(
70-
"SomeEnum",
71-
cw_schema::schema_of::<SomeEnum>(),
72-
serde_json::to_string(&SomeEnum::Field1).unwrap(),
73-
validator!(SomeEnum),
74-
),
75-
(
76-
"SomeEnum",
77-
cw_schema::schema_of::<SomeEnum>(),
78-
serde_json::to_string(&SomeEnum::Field2(10, 23)).unwrap(),
79-
validator!(SomeEnum),
80-
),
81-
(
82-
"SomeEnum",
83-
cw_schema::schema_of::<SomeEnum>(),
84-
serde_json::to_string(&SomeEnum::Field3 {
114+
validator!(SomeEnum, SomeEnum::Field1),
115+
validator!(SomeEnum, SomeEnum::Field2(10, 23)),
116+
validator!(
117+
SomeEnum,
118+
SomeEnum::Field3 {
85119
a: "sdf".to_string(),
86120
b: 12,
87-
})
88-
.unwrap(),
89-
validator!(SomeEnum),
90-
),
91-
(
92-
"UnitStructure",
93-
cw_schema::schema_of::<UnitStructure>(),
94-
serde_json::to_string(&UnitStructure {}).unwrap(),
95-
validator!(UnitStructure),
121+
}
96122
),
97-
(
98-
"TupleStructure",
99-
cw_schema::schema_of::<TupleStructure>(),
100-
serde_json::to_string(&TupleStructure(10, "aasdf".to_string(), 2)).unwrap(),
101-
validator!(TupleStructure),
102-
),
103-
(
104-
"NamedStructure",
105-
cw_schema::schema_of::<NamedStructure>(),
106-
serde_json::to_string(&NamedStructure {
123+
validator!(UnitStructure, UnitStructure {}),
124+
validator!(TupleStructure, TupleStructure(10, "aasdf".to_string(), 2)),
125+
validator!(
126+
NamedStructure,
127+
NamedStructure {
107128
a: "awer".to_string(),
108129
b: 4,
109130
c: SomeEnum::Field1,
110-
})
111-
.unwrap(),
112-
validator!(NamedStructure),
131+
}
132+
),
133+
validator!(
134+
AllSimpleTypesAndDocs,
135+
AllSimpleTypesAndDocs {
136+
array_field: vec!["abc".to_string(), "def".to_string()],
137+
float_field: 10.2,
138+
double_field: 10.232323,
139+
bool_field: true,
140+
string_field: "sdfsdf".to_string(),
141+
int_field: -10,
142+
bytes_field: cosmwasm_std::Binary::new(vec![0x1, 0x2, 0x3]),
143+
opt_field: Some("sdfsdfwer".to_string()),
144+
byte_field: 9,
145+
decimal_field: cosmwasm_std::Decimal::one(),
146+
address_field: cosmwasm_std::Addr::unchecked("some_address"),
147+
checksum_field: cosmwasm_std::Checksum::generate(&[0x10]),
148+
hexbinary_field: cosmwasm_std::HexBinary::from_hex("FAFAFA").unwrap(),
149+
timestamp_field: cosmwasm_std::Timestamp::from_seconds(100),
150+
unit_field: (),
151+
}
113152
),
114153
];
115154

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/python_tpl.rs
3+
expression: output
4+
snapshot_kind: text
5+
---
6+
# This code is @generated by cw-schema-codegen. Do not modify this manually.
7+
8+
import typing
9+
import decimal
10+
from pydantic import BaseModel, RootModel
11+
12+
13+
14+
class UnitStructure(RootModel):
15+
"""
16+
This is a struct level documentation for unit struct
17+
"""
18+
root: None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/python_tpl.rs
3+
expression: output
4+
snapshot_kind: text
5+
---
6+
# This code is @generated by cw-schema-codegen. Do not modify this manually.
7+
8+
import typing
9+
import decimal
10+
from pydantic import BaseModel, RootModel
11+
12+
13+
14+
class TupleStructure(RootModel):
15+
"""
16+
This is a struct level documentation for tuple
17+
"""
18+
root: typing.Tuple[int, str, int]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
source: packages/cw-schema-codegen/tests/python_tpl.rs
3+
expression: output
4+
snapshot_kind: text
5+
---
6+
# This code is @generated by cw-schema-codegen. Do not modify this manually.
7+
8+
import typing
9+
import decimal
10+
from pydantic import BaseModel, RootModel
11+
12+
class SomeEnum(RootModel):
13+
"""
14+
This is a struct level documentation for enum type
15+
"""
16+
17+
18+
19+
class Field1(RootModel):
20+
"""
21+
Field1 docs
22+
"""
23+
root: typing.Literal['Field1']
24+
25+
26+
27+
class Field2(BaseModel):
28+
"""
29+
Field2 docs
30+
"""
31+
Field2: typing.Tuple[int, int]
32+
33+
34+
35+
class Field3(BaseModel):
36+
class __Inner(BaseModel):
37+
"""
38+
Field3 docs
39+
"""
40+
41+
a: str
42+
"""
43+
`a` field docs
44+
"""
45+
46+
b: int
47+
"""
48+
`b` field docs
49+
"""
50+
51+
Field3: __Inner
52+
53+
54+
root: typing.Union[ Field1, Field2, Field3, ]
55+
# This code is @generated by cw-schema-codegen. Do not modify this manually.
56+
57+
import typing
58+
import decimal
59+
from pydantic import BaseModel, RootModel
60+
61+
62+
63+
class NamedStructure(BaseModel):
64+
"""
65+
This is a struct level documentation for named structure
66+
"""
67+
68+
a: str
69+
"""
70+
# `a` field docs
71+
"""
72+
73+
b: int
74+
"""
75+
# `b` field docs
76+
"""
77+
78+
c: SomeEnum
79+
"""
80+
# `c` field docs
81+
"""

0 commit comments

Comments
 (0)