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

#86drtepaj - Verify if type annotation using the literal types instea… #1239

Merged
merged 1 commit into from
Apr 23, 2024

Conversation

jplippi
Copy link
Contributor

@jplippi jplippi commented Apr 22, 2024

Summary or solution description
Changed the usage of deprecated methods from typing, specifically List, Dict and Tuple, to use the native implementations.
Sequence, Union, Optional and Mapping will be changed in different issues

…d of the typing aliases work in smart contracts
@melanke
Copy link
Contributor

melanke commented Apr 22, 2024

@jplippi jplippi requested a review from meevee98 April 22, 2024 23:06
@@ -318,7 +318,7 @@ def properties(tokenId: bytes) -> Dict[Any, Any]:
"""
metaBytes = cast(str, get_meta(tokenId))
expect(len(metaBytes) != 0, 'No metadata available for token')
metaObject = cast(Dict[str, str], json_deserialize(metaBytes))
metaObject = cast(dict[str, str], json_deserialize(metaBytes))

Choose a reason for hiding this comment

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

The cast function from the typing module does not support the native dict type for casting. Please revert this change and continue using Dict from the typing module.

@@ -378,7 +378,7 @@ def internal_deploy(owner: UInt160):
storage.put_bool(PAUSED, False)
storage.put_int(TOKEN_COUNT, 0)

auth: List[UInt160] = []
auth: list[UInt160] = []

Choose a reason for hiding this comment

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

The type hint for the list auth should be List[UInt160] instead of list[UInt160]. The List type hint is more descriptive and provides better type checking than the generic list type hint.



def Main(op: str, args: list) -> List[int]:
def Main(op: str, args: list) -> list[int]:
a = [1, 2, 3]
list.append(a)

Choose a reason for hiding this comment

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

The list.append() method requires an element as an argument to append to the list. Please provide an argument.



def Main(op: str, args: list) -> List[int]:
def Main(op: str, args: list) -> list[int]:
a = [1, 2, 3]
a.append(4, 5)

Choose a reason for hiding this comment

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

The append method should only take one argument. If you want to add multiple elements, consider using the extend method or appending them one by one.



def Main(op: str, args: list) -> Tuple[int]:
def Main(op: str, args: list) -> tuple[int]:
a = (1, 2, 3)

Choose a reason for hiding this comment

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

In Python, tuples are immutable and do not have an append method. Please revise this to use a different data structure or modify the tuple in a way that doesn't involve appending.



def Main(a: Tuple[int, ...]) -> int:
def Main(a: tuple[int, ...]) -> int:
a[0] = 1

Choose a reason for hiding this comment

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

Tuples in Python are immutable, so you cannot change their values after they are created. The line a[0] = 1 will raise a TypeError. Consider using a list if you need to change the values.


from boa3.builtin.compile_time import public


@public
def Main(value: Any) -> int:
x = cast(Dict[str, int], value)
x = cast(dict[str, int], value)

Choose a reason for hiding this comment

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

The dict type does not support indexing in Python. Please use typing.Dict for type hinting a dictionary.

from boa3.builtin.compile_time import public


@public
def Main(a: Tuple[int]):
def Main(a: tuple[int]):
b: str = a[0]

Choose a reason for hiding this comment

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

The type of the variable a is tuple[int] which means it is a tuple of integers. However, you are trying to assign the first element of this tuple to a string variable b. This will cause a type mismatch error. Please ensure the types are compatible.



def Main(a: Tuple[int]):
def Main(a: tuple[int]):
a[0] = '1'

Choose a reason for hiding this comment

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

Python's built-in tuple type does not support type hinting for its elements. Please use typing.Tuple for type hinting in function arguments.

from boa3.builtin.compile_time import public


@public
def Main(a: [int]) -> [int]: # should be List[int] instead of [int]
def Main(a: [int]) -> [int]: # should be list[int] instead of [int]

Choose a reason for hiding this comment

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

The type hinting for the parameter a and the return type of the function Main is incorrect. It should be list[int] instead of [int]. Please correct it.

@coveralls
Copy link
Collaborator

Coverage Status

coverage: 91.942%. remained the same
when pulling 63ba12e on CU-86drtepaj
into 7b1359f on development.

@meevee98 meevee98 merged commit 18749c1 into development Apr 23, 2024
5 checks passed
@meevee98 meevee98 deleted the CU-86drtepaj branch April 23, 2024 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants