Skip to content

Commit 0364ed8

Browse files
Merge pull request #281 from simoncozens/main
2 parents 7c8c3f4 + c65b209 commit 0364ed8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

core/src/language/python.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::language::SupportedLanguage;
12
use crate::parser::ParsedData;
23
use crate::rust_types::{RustEnumShared, RustItem, RustType, RustTypeFormatError, SpecialRustType};
34
use crate::topsort::topsort;
@@ -448,9 +449,12 @@ impl Python {
448449
let is_optional = field.ty.is_optional() || field.has_default;
449450
// currently, if a field has a serde default value, it must be an Option
450451
let not_optional_but_default = !field.ty.is_optional() && field.has_default;
451-
let python_type = self
452-
.format_type(&field.ty, generic_types)
453-
.map_err(std::io::Error::other)?;
452+
let python_type: String = match field.type_override(SupportedLanguage::Python) {
453+
Some(type_override) => type_override.to_owned(),
454+
None => self
455+
.format_type(&field.ty, generic_types)
456+
.map_err(std::io::Error::other)?,
457+
};
454458
let python_field_name = python_property_aware_rename(&field.id.original);
455459
let is_aliased = python_field_name != field.id.renamed;
456460
let custom_translations = json_translation_for_type(&python_type);

docs/src/usage/annotations.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ This would generate the following Kotlin code:
103103
typealias Options = String
104104
```
105105

106+
This decorator can also be applied to fields. If you want to precisely specify
107+
the serialized type in the foreign language, you can use the `(type = "...")` decorator:
106108

109+
```rust
110+
#[typeshare]
111+
struct MyStruct {
112+
#[typeshare(typescript(type = "Record<string, any>"))]
113+
#[typeshare(python(type = "dict"))]
114+
my_field: MyCustomType,
115+
}
116+
```
107117

108118
## The `#[serde]` Attribute
109119

0 commit comments

Comments
 (0)