All the emitter framework changes I made to get the COI demo working#49
All the emitter framework changes I made to get the COI demo working#49steverice wants to merge 2 commits intofeature/model-interfacefrom
Conversation
| ? getExtendsType($, props.type) | ||
| : undefined; | ||
| let basesType = props.bases ? props.bases : (globalBasesType ?? undefined); | ||
| function createBasesType( |
There was a problem hiding this comment.
The changes in this function are aimed at allowing us to specify additional bases for the purpose of adding Generic.
Those bases are generated in ClassDeclaration.
| if ( | ||
| !props.type.isFinished && | ||
| (props.type.node as TemplateDeclarationNode)?.templateParameters | ||
| ) { | ||
| const templateParameters = (props.type.node as TemplateDeclarationNode)?.templateParameters; | ||
| typeVars = ( | ||
| <> | ||
| <For each={templateParameters} hardline> | ||
| {(node) => { | ||
| const typeVar = ( | ||
| <py.FunctionCallExpression | ||
| target={typingModule["."].TypeVar} | ||
| args={[<py.Atom jsValue={node.id.sv} />]} | ||
| /> | ||
| ); | ||
| return <py.VariableDeclaration name={node.id.sv} initializer={typeVar} />; | ||
|
|
||
| // ("${node.id.sv}")`; | ||
| // return <py.Declaration name={String(node.id.sv)}></py.Declaration>; | ||
| }} | ||
| </For> | ||
| </> | ||
| ); | ||
| for (const templateParamter of templateParameters) { | ||
| typeArgs.push(templateParamter.id.sv); | ||
| } | ||
| } |
There was a problem hiding this comment.
This part is all aimed at determining if there are template parameters that are unfulfilled and, if so, adding them as type arguments to a Generic base.
| </> | ||
| ); | ||
| for (const templateParamter of templateParameters) { | ||
| typeArgs.push(templateParamter.id.sv); |
There was a problem hiding this comment.
This isn't great, we're just making a list of strings. These should probably be references but I didn't have time to figure out how to make that work.
| const output = ( | ||
| <Output | ||
| program={context.program} | ||
| externals={[ef.abcModule, ef.dataclassesModule, ef.typingModule, py.enumModule]} |
There was a problem hiding this comment.
This part was critical, as the stdlib references didn't work without it.
I'm not sure if it's expected that we'll always need to do this in emitters? Or if something should be happening with references or scopes or what have you so it happens automatically.
| </For> | ||
| <For each={type.models}> | ||
| {(name, type) => { | ||
| return <ef.ClassDeclaration type={type} abstract={!type.isFinished} />; |
There was a problem hiding this comment.
tbf I don't understand what isFinished actually means.
| } | ||
|
|
||
| function isTemplateVar(type: Type): boolean { | ||
| return (type as TemplatedTypeBase).templateMapper !== undefined; |
| reportPythonDiagnostic($.program, { code: "python-unsupported-type", target: type }); | ||
| break; | ||
| case "TemplateParameter": | ||
| return code`${String((type.node as TemplateParameterDeclarationNode).id.sv)}`; |
There was a problem hiding this comment.
Probably can do a lot better than a string! Should be a reference.
| if (isTemplateVar(type)) { | ||
| return <TypeExpression type={type.templateMapper?.args[0] as Type} />; | ||
| } |
There was a problem hiding this comment.
The weird thing is that models used as template arguments are treated as models here.
| const typeNode = isOptional ? ( | ||
| <py.TypeReference | ||
| refkey={typingModule["."].Optional} | ||
| typeArgs={[unpackedTypeNode]} | ||
| ></py.TypeReference> | ||
| ) : ( | ||
| unpackedTypeNode | ||
| ); |
There was a problem hiding this comment.
Adds support for wrapping types in Optional[] if the model property is optional.
| <Show when={!!typeVars}> | ||
| {typeVars} | ||
| <hbr /> | ||
| <line /> | ||
| </Show> |
There was a problem hiding this comment.
The typeVars should probably go at the top of the module, but I didn't want to figure out how to do that.
Even then, I'm not sure there is a defined style for where TypeVars should go.
| if ( | ||
| !props.type.isFinished && | ||
| (props.type.node as TemplateDeclarationNode)?.templateParameters | ||
| ) { |
There was a problem hiding this comment.
@mauriciogardini this is what I was referring to here #45 (comment)
No description provided.