@@ -138,7 +138,11 @@ export class PythonRenderer extends ConvenienceRenderer {
138138
139139 if ( hasNull !== null ) {
140140 let rest : string [ ] = [ ] ;
141- if ( ! this . getAlphabetizeProperties ( ) && this . pyOptions . features . dataClasses && _isRootTypeDef ) {
141+ if (
142+ ! this . getAlphabetizeProperties ( ) &&
143+ ( this . pyOptions . features . dataClasses || this . pyOptions . pydanticBaseModel ) &&
144+ _isRootTypeDef
145+ ) {
142146 // Only push "= None" if this is a root level type def
143147 // otherwise we may get type defs like List[Optional[int] = None]
144148 // which are invalid
@@ -177,6 +181,9 @@ export class PythonRenderer extends ConvenienceRenderer {
177181
178182 protected declarationLine ( t : Type ) : Sourcelike {
179183 if ( t instanceof ClassType ) {
184+ if ( this . pyOptions . pydanticBaseModel ) {
185+ return [ "class " , this . nameForNamedType ( t ) , "(" , this . withImport ( "pydantic" , "BaseModel" ) , "):" ] ;
186+ }
180187 return [ "class " , this . nameForNamedType ( t ) , ":" ] ;
181188 }
182189
@@ -196,7 +203,7 @@ export class PythonRenderer extends ConvenienceRenderer {
196203 }
197204
198205 protected emitClassMembers ( t : ClassType ) : void {
199- if ( this . pyOptions . features . dataClasses ) return ;
206+ if ( this . pyOptions . features . dataClasses || this . pyOptions . pydanticBaseModel ) return ;
200207
201208 const args : Sourcelike [ ] = [ ] ;
202209 this . forEachClassProperty ( t , "none" , ( name , _ , cp ) => {
@@ -236,7 +243,7 @@ export class PythonRenderer extends ConvenienceRenderer {
236243 properties : ReadonlyMap < string , ClassProperty > ,
237244 propertyNames : ReadonlyMap < string , Name >
238245 ) : ReadonlyMap < string , ClassProperty > {
239- if ( this . pyOptions . features . dataClasses ) {
246+ if ( this . pyOptions . features . dataClasses || this . pyOptions . pydanticBaseModel ) {
240247 return mapSortBy ( properties , ( p : ClassProperty ) => {
241248 return ( p . type instanceof UnionType && nullableFromUnion ( p . type ) != null ) || p . isOptional ? 1 : 0 ;
242249 } ) ;
@@ -246,7 +253,7 @@ export class PythonRenderer extends ConvenienceRenderer {
246253 }
247254
248255 protected emitClass ( t : ClassType ) : void {
249- if ( this . pyOptions . features . dataClasses ) {
256+ if ( this . pyOptions . features . dataClasses && ! this . pyOptions . pydanticBaseModel ) {
250257 this . emitLine ( "@" , this . withImport ( "dataclasses" , "dataclass" ) ) ;
251258 }
252259
0 commit comments