Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyright to precommit and format files #181

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
extend-ignore = E501, E722, E203
exclude = transaction_pb2*
2 changes: 1 addition & 1 deletion .github/workflows/linter-flake8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
pip install -r requirements-dev.txt
- name: Lint with flake8
run: |
flake8 multiversx_sdk
flake8 multiversx_sdk --exclude=transaction_pb2*
continue-on-error: true
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ repos:
args:
- --in-place
- --remove-all-unused-imports
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.392
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PR description, please add a link towards a page that recommends (endorses) this extension.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

hooks:
- id: pyright
15 changes: 10 additions & 5 deletions multiversx_sdk/abi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
from multiversx_sdk.abi.option_value import OptionValue
from multiversx_sdk.abi.optional_value import OptionalValue
from multiversx_sdk.abi.serializer import Serializer
from multiversx_sdk.abi.small_int_values import (I8Value, I16Value, I32Value,
I64Value, U8Value, U16Value,
U32Value, U64Value)
from multiversx_sdk.abi.small_int_values import (
I8Value,
I16Value,
I32Value,
I64Value,
U8Value,
U16Value,
U32Value,
U64Value,
)
from multiversx_sdk.abi.string_value import StringValue
from multiversx_sdk.abi.struct_value import StructValue
from multiversx_sdk.abi.token_identifier_value import TokenIdentifierValue
Expand All @@ -27,7 +34,6 @@
__all__ = [
"Abi",
"AbiDefinition",

"AddressValue",
"ArrayValue",
"BigIntValue",
Expand All @@ -53,7 +59,6 @@
"StructValue",
"TokenIdentifierValue",
"TupleValue",

"MultiValue",
"OptionalValue",
"VariadicValues",
Expand Down
88 changes: 51 additions & 37 deletions multiversx_sdk/abi/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
from types import SimpleNamespace
from typing import Any, cast

from multiversx_sdk.abi.abi_definition import (AbiDefinition,
EndpointDefinition,
EnumDefinition, EventDefinition,
EventTopicDefinition,
ParameterDefinition,
StructDefinition)
from multiversx_sdk.abi.abi_definition import (
AbiDefinition,
EndpointDefinition,
EnumDefinition,
EventDefinition,
EventTopicDefinition,
ParameterDefinition,
StructDefinition,
)
from multiversx_sdk.abi.address_value import AddressValue
from multiversx_sdk.abi.array_value import ArrayValue
from multiversx_sdk.abi.biguint_value import BigUIntValue
Expand All @@ -21,14 +24,22 @@
from multiversx_sdk.abi.fields import Field
from multiversx_sdk.abi.interface import IPayloadHolder
from multiversx_sdk.abi.list_value import ListValue
from multiversx_sdk.abi.managed_decimal_signed_value import ManagedDecimalSignedValue
from multiversx_sdk.abi.managed_decimal_value import ManagedDecimalValue
from multiversx_sdk.abi.multi_value import MultiValue
from multiversx_sdk.abi.option_value import OptionValue
from multiversx_sdk.abi.optional_value import OptionalValue
from multiversx_sdk.abi.serializer import Serializer
from multiversx_sdk.abi.small_int_values import (I8Value, I16Value, I32Value,
I64Value, U8Value, U16Value,
U32Value, U64Value)
from multiversx_sdk.abi.small_int_values import (
I8Value,
I16Value,
I32Value,
I64Value,
U8Value,
U16Value,
U32Value,
U64Value,
)
from multiversx_sdk.abi.string_value import StringValue
from multiversx_sdk.abi.struct_value import StructValue
from multiversx_sdk.abi.token_identifier_value import TokenIdentifierValue
Expand All @@ -37,8 +48,6 @@
from multiversx_sdk.abi.type_formula_parser import TypeFormulaParser
from multiversx_sdk.abi.variadic_values import VariadicValues

from multiversx_sdk.abi.managed_decimal_signed_value import ManagedDecimalSignedValue


class Abi:
def __init__(self, definition: AbiDefinition) -> None:
Expand All @@ -58,31 +67,26 @@ def __init__(self, definition: AbiDefinition) -> None:

self.constructor_prototype = EndpointPrototype(
input_parameters=self._create_endpoint_input_prototypes(definition.constructor),
output_parameters=self._create_endpoint_output_prototypes(definition.constructor)
output_parameters=self._create_endpoint_output_prototypes(definition.constructor),
)

self.upgrade_constructor_prototype = EndpointPrototype(
input_parameters=self._create_endpoint_input_prototypes(definition.upgrade_constructor),
output_parameters=self._create_endpoint_output_prototypes(definition.upgrade_constructor)
output_parameters=self._create_endpoint_output_prototypes(definition.upgrade_constructor),
)

for endpoint in definition.endpoints:
input_prototype = self._create_endpoint_input_prototypes(endpoint)
output_prototype = self._create_endpoint_output_prototypes(endpoint)

endpoint_prototype = EndpointPrototype(
input_parameters=input_prototype,
output_parameters=output_prototype
)
endpoint_prototype = EndpointPrototype(input_parameters=input_prototype, output_parameters=output_prototype)

self.endpoints_prototypes_by_name[endpoint.name] = endpoint_prototype

for event in definition.events:
prototype = self._create_event_input_prototypes(event)

event_prototype = EventPrototype(
fields=prototype
)
event_prototype = EventPrototype(fields=prototype)

self.events_prototypes_by_name[event.identifier] = event_prototype

Expand All @@ -101,12 +105,8 @@ def _create_custom_type_prototype(self, name: str) -> Any:

def _create_enum_prototype(self, enum_definition: EnumDefinition) -> Any:
return EnumValue(
fields_provider=lambda discriminant: self._provide_fields_for_enum_prototype(
discriminant, enum_definition
),
names_to_discriminants={
v.name: v.discriminant for v in enum_definition.variants
},
fields_provider=lambda discriminant: self._provide_fields_for_enum_prototype(discriminant, enum_definition),
names_to_discriminants={v.name: v.discriminant for v in enum_definition.variants},
)

def _create_explicit_enum_prototype(self) -> Any:
Expand All @@ -127,7 +127,9 @@ def _provide_fields_for_enum_prototype(self, discriminant: int, enum_definition:

return fields_prototypes

raise ValueError(f"cannot provide fields from enum {enum_definition.name}: variant with discriminant {discriminant} not found")
raise ValueError(
f"cannot provide fields from enum {enum_definition.name}: variant with discriminant {discriminant} not found"
)

def _create_struct_prototype(self, struct_definition: StructDefinition) -> Any:
fields_prototypes: list[Field] = []
Expand Down Expand Up @@ -185,9 +187,16 @@ def encode_endpoint_input_parameters(self, endpoint_name: str, values: list[Any]
endpoint_prototype = self._get_endpoint_prototype(endpoint_name)
return self._do_encode_endpoint_input_parameters(endpoint_name, endpoint_prototype, values)

def _do_encode_endpoint_input_parameters(self, endpoint_name: str, endpoint_prototype: 'EndpointPrototype', values: list[Any]):
def _do_encode_endpoint_input_parameters(
self,
endpoint_name: str,
endpoint_prototype: "EndpointPrototype",
values: list[Any],
):
if len(values) != len(endpoint_prototype.input_parameters):
raise ValueError(f"for {endpoint_name}, invalid value length: expected {len(endpoint_prototype.input_parameters)}, got {len(values)}")
raise ValueError(
f"for {endpoint_name}, invalid value length: expected {len(endpoint_prototype.input_parameters)}, got {len(values)}"
)

input_values = deepcopy(endpoint_prototype.input_parameters)
input_values_as_native_object_holders = cast(list[IPayloadHolder], input_values)
Expand Down Expand Up @@ -249,15 +258,15 @@ def _get_custom_type_prototype(self, type_name: str) -> Any:

return type_prototype

def _get_endpoint_prototype(self, endpoint_name: str) -> 'EndpointPrototype':
def _get_endpoint_prototype(self, endpoint_name: str) -> "EndpointPrototype":
endpoint_prototype = self.endpoints_prototypes_by_name.get(endpoint_name)

if not endpoint_prototype:
raise ValueError(f"endpoint '{endpoint_name}' not found")

return endpoint_prototype

def _get_event_prototype(self, event_name: str) -> 'EventPrototype':
def _get_event_prototype(self, event_name: str) -> "EventPrototype":
event_prototype = self.events_prototypes_by_name.get(event_name)

if not event_prototype:
Expand Down Expand Up @@ -303,7 +312,9 @@ def _create_prototype(self, type_formula: TypeFormula) -> Any:
if name == "CodeMetadata":
return CodeMetadataValue()
if name == "tuple":
return TupleValue([self._create_prototype(type_parameter) for type_parameter in type_formula.type_parameters])
return TupleValue(
[self._create_prototype(type_parameter) for type_parameter in type_formula.type_parameters]
)
if name == "Option":
type_parameter = type_formula.type_parameters[0]
return OptionValue(self._create_prototype(type_parameter))
Expand All @@ -313,7 +324,10 @@ def _create_prototype(self, type_formula: TypeFormula) -> Any:
if name.startswith("array"):
type_parameter = type_formula.type_parameters[0]
length = int(name[5:])
return ArrayValue(length=length, item_creator=lambda: self._create_prototype(type_parameter))
return ArrayValue(
length=length,
item_creator=lambda: self._create_prototype(type_parameter),
)
if name == "optional":
# The prototype of an optional is provided a value (the placeholder).
type_parameter = type_formula.type_parameters[0]
Expand All @@ -325,16 +339,16 @@ def _create_prototype(self, type_formula: TypeFormula) -> Any:
type_parameter = type_formula.type_parameters[0]
return CountedVariadicValues([], item_creator=lambda: self._create_prototype(type_parameter))
if name == "multi":
return MultiValue([self._create_prototype(type_parameter) for type_parameter in type_formula.type_parameters])
return MultiValue(
[self._create_prototype(type_parameter) for type_parameter in type_formula.type_parameters]
)
if name == "ManagedDecimal":
scale = type_formula.type_parameters[0].name

if scale == "usize":
return ManagedDecimalValue(scale=0, is_variable=True)
else:
return ManagedDecimalValue(scale=int(scale), is_variable=False)


if name == "ManagedDecimalSigned":
scale = type_formula.type_parameters[0].name

Expand All @@ -348,7 +362,7 @@ def _create_prototype(self, type_formula: TypeFormula) -> Any:
return deepcopy(type_prototype)

@classmethod
def load(cls, path: Path) -> 'Abi':
def load(cls, path: Path) -> "Abi":
definition = AbiDefinition.load(path)
return cls(definition)

Expand Down
Loading
Loading