Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
@0x/abi-gen: Update python wrappers generation to work with leading…
Browse files Browse the repository at this point in the history
… underscores in identifiers.'
  • Loading branch information
merklejerk committed Jun 8, 2020
1 parent 9cf1c50 commit d0714d8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 46 deletions.
9 changes: 9 additions & 0 deletions packages/abi-gen/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "5.3.0",
"changes": [
{
"note": "Allow identifiers with leading underscores in python wrappers",
"pr": 2591
}
]
},
{
"version": "5.2.2",
"changes": [
Expand Down
11 changes: 10 additions & 1 deletion packages/abi-gen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ if (args.language === 'TypeScript') {
registerPartials();

function makeLanguageSpecificName(methodName: string): string {
return args.language === 'Python' ? changeCase.snake(methodName) : methodName;
if (args.language === 'Python') {
let snakeCased = changeCase.snake(methodName);
// Move leading underscores to the end.
const m = /^(_*).+?(_*)$/.exec(methodName);
if (m) {
snakeCased = `${snakeCased}${m[1] || m[2]}`;
}
return snakeCased;
}
return methodName;
}

if (_.isEmpty(abiFileNames)) {
Expand Down
13 changes: 9 additions & 4 deletions packages/abi-gen/src/python_handlebars_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,15 @@ export function registerPythonHelpers(): void {
}
return '';
});
Handlebars.registerHelper(
'toPythonClassname',
(sourceName: string) => new Handlebars.SafeString(changeCase.pascal(sourceName)),
);
Handlebars.registerHelper('toPythonClassname', (sourceName: string) => {
let pascalCased = changeCase.pascal(sourceName);
// Retain trailing underscores.
const m = /^.+?(_*)$/.exec(sourceName);
if (m) {
pascalCased = `${pascalCased}${m[1]}`;
}
return new Handlebars.SafeString(pascalCased);
});
Handlebars.registerHelper(
'makeOutputsValue',
/**
Expand Down
11 changes: 6 additions & 5 deletions packages/abi-gen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ export const utils = {
'max',
'round',
];
if (
pythonReservedWords.includes(snakeCased) ||
pythonBuiltins.includes(snakeCased) ||
/*changeCase strips leading underscores :(*/ input[0] === '_'
) {
if (pythonReservedWords.includes(snakeCased) || pythonBuiltins.includes(snakeCased)) {
snakeCased = `_${snakeCased}`;
}
// Retain trailing underscores.
const m = /^.+?(_*)$/.exec(input);
if (m) {
snakeCased = `${snakeCased}${m[1]}`;
}
return snakeCased;
},
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

class {{toPythonClassname this.languageSpecificName}}Method(ContractMethod):
class {{toPythonClassname this.languageSpecificName}}Method(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the {{this.name}} method."""

def __init__(self, web3_or_provider: Union[Web3, BaseProvider], contract_address: str, contract_function: ContractFunction{{#if inputs}}, validator: Validator=None{{/if}}):
Expand Down
92 changes: 60 additions & 32 deletions packages/abi-gen/test-cli/output/python/abi_gen_dummy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ class AbiGenDummyNestedStruct(TypedDict):
description: str


class AcceptsAnArrayOfBytesMethod(ContractMethod):
class AcceptsAnArrayOfBytesMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the acceptsAnArrayOfBytes method."""

def __init__(
Expand Down Expand Up @@ -225,7 +227,7 @@ def estimate_gas(
return self._underlying_method(a).estimateGas(tx_params.as_dict())


class AcceptsBytesMethod(ContractMethod):
class AcceptsBytesMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the acceptsBytes method."""

def __init__(
Expand Down Expand Up @@ -267,7 +269,9 @@ def estimate_gas(
return self._underlying_method(a).estimateGas(tx_params.as_dict())


class ComplexInputComplexOutputMethod(ContractMethod):
class ComplexInputComplexOutputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the complexInputComplexOutput method."""

def __init__(
Expand Down Expand Up @@ -329,7 +333,7 @@ def estimate_gas(
)


class EcrecoverFnMethod(ContractMethod):
class EcrecoverFnMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the ecrecoverFn method."""

def __init__(
Expand Down Expand Up @@ -414,7 +418,7 @@ def estimate_gas(
)


class EmitSimpleEventMethod(ContractMethod):
class EmitSimpleEventMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the emitSimpleEvent method."""

def __init__(
Expand Down Expand Up @@ -457,7 +461,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class MethodAcceptingArrayOfArrayOfStructsMethod(ContractMethod):
class MethodAcceptingArrayOfArrayOfStructsMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the methodAcceptingArrayOfArrayOfStructs method."""

def __init__(
Expand Down Expand Up @@ -509,7 +515,9 @@ def estimate_gas(
)


class MethodAcceptingArrayOfStructsMethod(ContractMethod):
class MethodAcceptingArrayOfStructsMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the methodAcceptingArrayOfStructs method."""

def __init__(
Expand Down Expand Up @@ -559,7 +567,9 @@ def estimate_gas(
)


class MethodReturningArrayOfStructsMethod(ContractMethod):
class MethodReturningArrayOfStructsMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the methodReturningArrayOfStructs method."""

def __init__(
Expand Down Expand Up @@ -598,7 +608,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class MethodReturningMultipleValuesMethod(ContractMethod):
class MethodReturningMultipleValuesMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the methodReturningMultipleValues method."""

def __init__(
Expand Down Expand Up @@ -632,7 +644,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:

class MethodUsingNestedStructWithInnerStructNotUsedElsewhereMethod(
ContractMethod
):
): # pylint: disable=invalid-name
"""Various interfaces to the methodUsingNestedStructWithInnerStructNotUsedElsewhere method."""

def __init__(
Expand Down Expand Up @@ -665,7 +677,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class MultiInputMultiOutputMethod(ContractMethod):
class MultiInputMultiOutputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the multiInputMultiOutput method."""

def __init__(
Expand Down Expand Up @@ -747,7 +761,7 @@ def estimate_gas(
)


class NestedStructInputMethod(ContractMethod):
class NestedStructInputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the nestedStructInput method."""

def __init__(
Expand Down Expand Up @@ -791,7 +805,7 @@ def estimate_gas(
return self._underlying_method(n).estimateGas(tx_params.as_dict())


class NestedStructOutputMethod(ContractMethod):
class NestedStructOutputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the nestedStructOutput method."""

def __init__(
Expand Down Expand Up @@ -824,7 +838,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class NoInputNoOutputMethod(ContractMethod):
class NoInputNoOutputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the noInputNoOutput method."""

def __init__(
Expand Down Expand Up @@ -854,7 +868,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class NoInputSimpleOutputMethod(ContractMethod):
class NoInputSimpleOutputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the noInputSimpleOutput method."""

def __init__(
Expand Down Expand Up @@ -885,7 +901,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class NonPureMethodMethod(ContractMethod):
class NonPureMethodMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the nonPureMethod method."""

def __init__(
Expand Down Expand Up @@ -929,7 +945,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class NonPureMethodThatReturnsNothingMethod(ContractMethod):
class NonPureMethodThatReturnsNothingMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the nonPureMethodThatReturnsNothing method."""

def __init__(
Expand Down Expand Up @@ -972,7 +990,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class OverloadedMethod2Method(ContractMethod):
class OverloadedMethod2Method(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the overloadedMethod method."""

def __init__(
Expand Down Expand Up @@ -1014,7 +1032,7 @@ def estimate_gas(
return self._underlying_method(a).estimateGas(tx_params.as_dict())


class OverloadedMethod1Method(ContractMethod):
class OverloadedMethod1Method(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the overloadedMethod method."""

def __init__(
Expand Down Expand Up @@ -1056,7 +1074,9 @@ def estimate_gas(
return self._underlying_method(a).estimateGas(tx_params.as_dict())


class PureFunctionWithConstantMethod(ContractMethod):
class PureFunctionWithConstantMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the pureFunctionWithConstant method."""

def __init__(
Expand Down Expand Up @@ -1085,7 +1105,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class RequireWithConstantMethod(ContractMethod):
class RequireWithConstantMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the requireWithConstant method."""

def __init__(
Expand Down Expand Up @@ -1113,7 +1135,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class RevertWithConstantMethod(ContractMethod):
class RevertWithConstantMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the revertWithConstant method."""

def __init__(
Expand Down Expand Up @@ -1141,7 +1163,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class SimpleInputNoOutputMethod(ContractMethod):
class SimpleInputNoOutputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the simpleInputNoOutput method."""

def __init__(
Expand Down Expand Up @@ -1189,7 +1213,9 @@ def estimate_gas(
)


class SimpleInputSimpleOutputMethod(ContractMethod):
class SimpleInputSimpleOutputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the simpleInputSimpleOutput method."""

def __init__(
Expand Down Expand Up @@ -1238,7 +1264,7 @@ def estimate_gas(
)


class SimplePureFunctionMethod(ContractMethod):
class SimplePureFunctionMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the simplePureFunction method."""

def __init__(
Expand Down Expand Up @@ -1267,7 +1293,9 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class SimplePureFunctionWithInputMethod(ContractMethod):
class SimplePureFunctionWithInputMethod(
ContractMethod
): # pylint: disable=invalid-name
"""Various interfaces to the simplePureFunctionWithInput method."""

def __init__(
Expand Down Expand Up @@ -1312,7 +1340,7 @@ def estimate_gas(
return self._underlying_method(x).estimateGas(tx_params.as_dict())


class SimpleRequireMethod(ContractMethod):
class SimpleRequireMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the simpleRequire method."""

def __init__(
Expand Down Expand Up @@ -1340,7 +1368,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class SimpleRevertMethod(ContractMethod):
class SimpleRevertMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the simpleRevert method."""

def __init__(
Expand Down Expand Up @@ -1368,7 +1396,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class StructInputMethod(ContractMethod):
class StructInputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the structInput method."""

def __init__(
Expand Down Expand Up @@ -1410,7 +1438,7 @@ def estimate_gas(
return self._underlying_method(s).estimateGas(tx_params.as_dict())


class StructOutputMethod(ContractMethod):
class StructOutputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the structOutput method."""

def __init__(
Expand Down Expand Up @@ -1446,7 +1474,7 @@ def estimate_gas(self, tx_params: Optional[TxParams] = None) -> int:
return self._underlying_method().estimateGas(tx_params.as_dict())


class WithAddressInputMethod(ContractMethod):
class WithAddressInputMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the withAddressInput method."""

def __init__(
Expand Down Expand Up @@ -1537,7 +1565,7 @@ def estimate_gas(
)


class WithdrawMethod(ContractMethod):
class WithdrawMethod(ContractMethod): # pylint: disable=invalid-name
"""Various interfaces to the withdraw method."""

def __init__(
Expand Down
Loading

0 comments on commit d0714d8

Please sign in to comment.