in packages/jsii-pacmak/lib/targets/python.ts [942:1025]
public emit(
code: CodeMaker,
context: EmitContext,
opts?: BasePropertyEmitOpts,
) {
const { renderAbstract = true, forceEmitBody = false } = opts ?? {};
const pythonType = toTypeName(this.type).pythonType(context);
code.line(`@${this.decorator}`);
code.line(`@jsii.member(jsii_name="${this.jsName}")`);
if (renderAbstract && this.abstract) {
code.line('@abc.abstractmethod');
}
openSignature(
code,
'def',
this.pythonName,
[this.implicitParameter],
pythonType,
// PyRight and MyPY both special-case @property, but not custom implementations such as our @classproperty...
// MyPY reports on the re-declaration, but PyRight reports on the initial declaration (duh!)
this.isStatic && !this.immutable
? 'pyright: ignore [reportGeneralTypeIssues,reportRedeclaration]'
: undefined,
);
this.generator.emitDocString(code, this.apiLocation, this.docs, {
documentableItem: `prop-${this.pythonName}`,
});
// NOTE: No parameters to validate here, this is a getter...
if (
(this.shouldEmitBody || forceEmitBody) &&
(!renderAbstract || !this.abstract)
) {
code.line(
`return typing.cast(${pythonType}, jsii.${this.jsiiGetMethod}(${this.implicitParameter}, "${this.jsName}"))`,
);
} else {
code.line('...');
}
code.closeBlock();
if (!this.immutable) {
code.line();
// PyRight and MyPY both special-case @property, but not custom implementations such as our @classproperty...
// MyPY reports on the re-declaration, but PyRight reports on the initial declaration (duh!)
code.line(
`@${this.pythonName}.setter${
this.isStatic ? ' # type: ignore[no-redef]' : ''
}`,
);
if (renderAbstract && this.abstract) {
code.line('@abc.abstractmethod');
}
openSignature(
code,
'def',
this.pythonName,
[this.implicitParameter, `value: ${pythonType}`],
'None',
);
if (
(this.shouldEmitBody || forceEmitBody) &&
(!renderAbstract || !this.abstract)
) {
emitParameterTypeChecks(
code,
context,
[`value: ${pythonType}`],
`${this.pythonParent.fqn ?? this.pythonParent.pythonName}#${
this.pythonName
}`,
);
// In case of a static setter, the 'cls' type is the class type but because we use a custom
// decorator to make the setter operate on classes instead of objects, pyright doesn't know about
// that and thinks the first argument is an instance instead of a class. Shut it up.
code.line(
`jsii.${this.jsiiSetMethod}(${this.implicitParameter}, "${this.jsName}", value) # pyright: ignore[reportArgumentType]`,
);
} else {
code.line('...');
}
code.closeBlock();
}
}