diff --git a/core/src/language/python.rs b/core/src/language/python.rs index f82cc05e..fdff7498 100644 --- a/core/src/language/python.rs +++ b/core/src/language/python.rs @@ -277,15 +277,9 @@ impl Language for Python { .add_import("typing".to_string(), "List".to_string()); Ok(format!("List[{}]", self.format_type(rtype, generic_types)?)) } + //= Field(default=None) // We add optionality above the type formatting level - SpecialRustType::Option(rtype) => { - self.module - .add_import("pydantic".to_owned(), "Field".to_owned()); - Ok(format!( - "{} = Field(default=None)", - self.format_type(rtype, generic_types)? - )) - } + SpecialRustType::Option(rtype) => self.format_type(rtype, generic_types), SpecialRustType::HashMap(rtype1, rtype2) => { self.module .add_import("typing".to_string(), "Dict".to_string()); @@ -666,9 +660,11 @@ impl Python { .format_type(&field.ty, generic_types) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; if field.ty.is_optional() || field.has_default { - python_type = format!("Optional[{}]", python_type); + python_type = format!("Optional[{}] = Field(default=None)", python_type); self.module .add_import("typing".to_string(), "Optional".to_string()); + self.module + .add_import("pydantic".to_owned(), "Field".to_owned()); } let mut default = None; if field.has_default {