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

🧹 clean up and upgrade python syntax. #2372

Merged
merged 8 commits into from
Mar 10, 2022

Conversation

wiseaidev
Copy link
Contributor

@wiseaidev wiseaidev commented Mar 2, 2022

Signed-off-by: Harmouch101 [email protected]

What was wrong?

Related to Issue #

How was it fixed?

As of python3, 'Unicode' strings are the default. Hence no need for the prefix u. Similarly, __nonzero__ is a python2 syntax for defining boolness. I also used f-strings since this project support 3.6+.

So, I manually went through the codebase, updated:

  • old-school string formatting to f-strings.
  • list to set whenever possible because sets are more efficient. to avoid redundant calls of set.
  • Some other cleanups.
  • Some of the error messages:
        message = (
            f"\nCould not identify the intended function with name `"
            f"{fn_identifier}`, positional argument"
            f"{'s' if len(args) > 1 else ''} of type "
            f"`{tuple(map(type, args))}` and keyword argument"
            f"{'s' if len(kwargs) > 1 else ''} of type "
            f"`{valmap(type, kwargs)}`.\nFound {len(matching_identifiers)} "
            f"function{'s' if len(matching_identifiers) > 1 else ''} with "
            f"the name `{fn_identifier}`"
            f": {matching_function_signatures}{diagnosis}"
        )

output example:

"\nCould not identify the intended function with name `setBytes32Value`, positional argument of type `(<class 'str'>,)` and keyword argument of type `{}`.\nFound 1 function with the name `setBytes32Value`: ['setBytes32Value(bytes32[])']\nFunction invocation failed due to no matching argument types.".

Todo:

Cute Animal Picture

🐶

Signed-off-by: Harmouch101 <[email protected]>
Signed-off-by: Harmouch101 <[email protected]>
@wiseaidev
Copy link
Contributor Author

Apparently, f-strings seem to be the fastest string formatting method in microbenchmarks as Raymond Hettinger showed.

@wiseaidev wiseaidev force-pushed the rm-py2-sytax branch 8 times, most recently from 0b74c14 to 8a2e061 Compare March 8, 2022 18:46
Signed-off-by: Harmouch101 <[email protected]>
@wiseaidev wiseaidev changed the title 🧹 clean up some python2 syntax. 🧹 clean up and upgrade python syntax. Mar 9, 2022
@wiseaidev
Copy link
Contributor Author

I think this PR is ready for reviews from the warriors @fselmo, @kclowes, @pipermerriam ...

Copy link
Collaborator

@fselmo fselmo left a comment

Choose a reason for hiding this comment

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

Hey @Harmouch101. Wow. Thanks for the hefty sweeping! I think in the future it might be nicer to break the different changes up into separate PRs for large loads like this but bravo 👍 and thank you 🙏 . We'd been steering towards f-strings in general, changing it where it made sense, and this certainly helps us get there faster 😅.

I left some nit comments where it's nice that f-strings are shorter since we can fit messages in one line rather than line split.

I think I found just one misrepresentation / typo from the old .format() to f-string format that I didn't preface with a "nit:" tag.

I left a comment on the pluralization logic for strings in general. I think that though the pluralization being accurate is nice, I'm not sure that outweighs the same logic being repeated across all strings in the code base where things might need to be plural. I think it might be better to have static messages in general too with just the expected variables being dynamic - for consistency to the end-user.

I could've totally overlooked something too so let me know. Thanks again though for another very thorough PR.


edit: I forgot to mention it but I'm a big fan of the list -> set changes where it made sense 👌

web3/_utils/caching.py Show resolved Hide resolved
web3/_utils/blocks.py Show resolved Hide resolved
web3/_utils/abi.py Outdated Show resolved Hide resolved
web3/_utils/abi.py Outdated Show resolved Hide resolved
web3/_utils/empty.py Show resolved Hide resolved
web3/providers/auto.py Outdated Show resolved Hide resolved
web3/providers/eth_tester/defaults.py Outdated Show resolved Hide resolved
web3/providers/ipc.py Outdated Show resolved Hide resolved
web3/providers/auto.py Outdated Show resolved Hide resolved
tests/ethpm/validation/test_manifest.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@kclowes kclowes left a comment

Choose a reason for hiding this comment

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

LGTM after the comments from @fselmo are addressed! Thanks @Harmouch101!

Signed-off-by: Harmouch101 <[email protected]>
Signed-off-by: Harmouch101 <[email protected]>
Signed-off-by: Harmouch101 <[email protected]>
Signed-off-by: Harmouch101 <[email protected]>
Signed-off-by: Harmouch101 <[email protected]>
@wiseaidev
Copy link
Contributor Author

@fselmo, Just ping to let you know I have done the changes requested.

@fselmo
Copy link
Collaborator

fselmo commented Mar 10, 2022

@fselmo, Just ping to let you know I have done the changes requested.

Just getting back to this. Thanks again for these changes @Harmouch101 👌

@fselmo fselmo merged commit dca1795 into ethereum:master Mar 10, 2022
@wiseaidev
Copy link
Contributor Author

Hey @fselmo , just a suggestion about the commit messages. I think for the future it would be better to squash all commits into a single one cause some of my commit messages doesn't make sense for the whole code base, rather for this specific PR. Therefore, i suggest that the contribution file should mention that the authors of PRs must squash their commits when they finish pushing all the necessary code for a specific PR. Or, actually, to make life easier for new contributors, the task can be done by maintainers.

Regards.

@fselmo
Copy link
Collaborator

fselmo commented Mar 12, 2022

Hey @Harmouch101. Agreed. We usually do squash commits. I overlooked it before merging this time.

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.

3 participants