Skip to content

update python syntax >=3.6 #4585

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

Merged
merged 20 commits into from
Oct 28, 2021
Merged

update python syntax >=3.6 #4585

merged 20 commits into from
Oct 28, 2021

Conversation

Borda
Copy link
Contributor

@Borda Borda commented Oct 10, 2021

In fact, I was just adding a new hook to pre-commit pyupgrade which generated all these changes
also I have added python_requires=">=3.6", to the seup.py

An alternative would be enable pre-commit bot and then it would be clear that most of the changes comes just from the pre-commit hooks, see https://pre-commit.ci/

@facebook-github-bot
Copy link

Hi @Borda!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks for the RP @Borda ,

Overall I'm happy with the changes, we tend to favour f-string over formatted strings lately, and it's nice for everything to be consistent. The only issue is that it will add another "noisy" commit to git blame on github, but IMHO it's still worth it.

One thing I'm not sure about is the commit hook, considering that some of these changes are making torchscript unhappy (see comment below), we might not be able to enforce this with a hook.

Let's also ping @pmeier @fmassa @datumbox @prabhat00155 to see what they think.

@@ -923,7 +921,7 @@ def _get_inverse_affine_matrix(
# Thus, the inverse is M^-1 = C * RSS^-1 * C^-1 * T^-1

rot = math.radians(angle)
sx, sy = [math.radians(s) for s in shear]
sx, sy = (math.radians(s) for s in shear)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like torchscript in unhappy here, I think we should revert to a list

@datumbox
Copy link
Contributor

I was about to write something similar to Nicolas. I like the fact we modernize the code and think it's worth it. I wouldn't add it as a precommit for the reasons mentioned above.

Not sure if we should try to split it in parts or do it all in one go.

@NicolasHug
Copy link
Member

Not sure if we should try to split it in parts or do it all in one go.

I think it's best to do it in one go to avoid too many noisy commits. As long as the tests are passing it should be OK. Hopefully we can trust pyupgrade not to do anything insane

@Borda
Copy link
Contributor Author

Borda commented Oct 11, 2021

One thing I'm not sure about is the commit hook, considering that some of these changes are making torchscript unhappy (see comment below), we might not be able to enforce this with a hook.

I may have related thought as each statement (code line) can be written in many ways, is the actual optimal, personally, I would keep any of these hook/bots and try to rewrite some small number of lines to complain of hook and also to be fine with you and torchscript... what do you think?

I was about to write something similar to Nicolas. I like the fact we modernize the code and think it's worth it. I wouldn't add it as a precommit for the reasons mentioned above.

I may add that these changes are one-time as it is applied codebase-wide... later it is the same as Flak8 when you write new code and it does not compliment you edit it, but in fact you may not ever see what code structure that author had in mind at the tie he started his contribution (compare to the final submission/PR version)

Hopefully we can trust pyupgrade not to do anything insane

if anything went wrong in future, you can just drop it 🐰

@NicolasHug
Copy link
Member

I might be missing something, but I don't think the changes here are related to flake8. flake8 won't complain with non-f-strings, or when classes inherit from object, etc.

For the rest, if there is a simple and easy way to make both the hook and torchscript happy, then we can consider the hook for inclusion, provided that it runs fast, etc.

But torchscript support trumps everything else, so if having the hook means that we can't make torchscript happy, unfortunately that means we need to remove it

@Borda
Copy link
Contributor Author

Borda commented Oct 11, 2021

I might be missing something, but I don't think the changes here are related to flake8. flake8 won't complain with non-f-strings, or when classes inherit from object, etc.

not at all, I made it just as a parallel example, sorry for confusion

For the rest, if there is a simple and easy way to make both the hook and torchscript happy, then we can consider the hook for inclusion, provided that it runs fast, etc.

I will check it this week

@NicolasHug
Copy link
Member

Thanks @Borda ,

Slightly related, I noticed that we started having string "that look" "like this", for example

"Expected target boxes to be of type " "Tensor, got {:}.".format(type(boxes)))

I think they come from the recent black formatting.

git grep '\" \"' torchvision references | wc -l
58

We should try to fix those before applying pyupgrade because I think it actually prevents pyupgrade from converting those to f-strings

@Borda
Copy link
Contributor Author

Borda commented Oct 11, 2021

@NicolasHug thay may be another step, so far you do not enforce much code formating, right?

Slightly related, I noticed that we started having string "that look" "like this", for example

this comes from placing a two-line string to a single line which would be otherwise caught sooner with any formatting tool like Black of YAPF... what do you think about adding any of them in future?
Mind be interesting: Thoughts on Why Code Formatting is even more important for Open-Source

@NicolasHug
Copy link
Member

@NicolasHug thay may be another step, so far you do not enforce much code formating, right?

We do enforce black-formatting now, since last week.

But I think we should fix those "weird" "strings" before we apply pyupgrade because they prevent pyupgrade from working properly on those strings. To keep the noisy commits to a minimum we should also do it in the same PR (this one :) )

@Borda
Copy link
Contributor Author

Borda commented Oct 11, 2021

We do enforce black-formatting now, since last week.

do you have it also in pre-commit hooks?
I see this one


but I don't think that it is the optimal one, rather use

  - repo: https://github.com/psf/black
    rev: 21.9b0
    hooks:
      - id: black
        name: Format code

I can add it in separate PR

@Borda Borda mentioned this pull request Oct 11, 2021
@NicolasHug
Copy link
Member

ufmt is a combination of black and usort, we have to rely on this (and not on the individual formatters) because of compatibility reasons with FB's internal linters.

@Borda Borda requested a review from NicolasHug October 11, 2021 12:26
@fmassa
Copy link
Member

fmassa commented Oct 11, 2021

Ok with changing to use f-strings, although it's a pity that it will be a bit harder for git blame. But fine with me.

Copy link
Contributor

@prabhat00155 prabhat00155 left a comment

Choose a reason for hiding this comment

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

Thanks @Borda for the PR. Reference scripts are not tested by unit tests, did you verify that everything runs fine?

@Borda
Copy link
Contributor Author

Borda commented Oct 12, 2021

Reference scripts are not tested by unit tests, did you verify that everything runs fine?

seems they require multi-node/GPU machines which I do not have in my disposal :(

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @Borda ,

This LGTM with a minor comment below.

I checked the classification reference, it's working fine. The rest of the references should be OK as well - there's not torchscript stuff there anyway, and I think this is what was causing the few issues earlier.

I'm OK to keep the new hook for now as it runs pretty quickly. However if it starts getting in our way and forces us to fix things like in 418ad23, I think we will need to remove it.

Thanks!

@datumbox
Copy link
Contributor

@Borda Thanks a lot for the contribution. I believe this PR improves and modernizes the code-base.

Though I understand that these changes came from pyupgrade and it might be a good idea to run it in every PR to ensure consistency, in the past we've decided against modifying the code without ones knowledge. A good reason for why we don't want this is JIT-scriptability. Earlier versions of your PR were rewritten from the tool in a non-JIT compliant way. Of course this got caught by the tests, but still it might be dangerous.

I think it might be best to remove the hook from this PR and give us a bit more time to discuss whether we would like to revisit our past decision for automatic code modifications prior commit. This could happen on a separate PR that would be easier to invert if need be. Let me know what you think?

@Borda
Copy link
Contributor Author

Borda commented Oct 13, 2021

I think it might be best to remove the hook from this PR and give us a bit more time to discuss whether we would like to revisit our past decision for automatic code modifications prior commit. This could happen on a separate PR that would be easier to invert if need be. Let me know what you think?

resolved conflicts and commented out the hook so later you can just uncomment it back...

@Borda Borda requested a review from prabhat00155 October 14, 2021 16:48
@Borda
Copy link
Contributor Author

Borda commented Oct 14, 2021

seems to be failing on Segmentation fault :/

@datumbox
Copy link
Contributor

@Borda It's not your PR, our CI is toasted... It should be resolved tomorrow using the latest nightly of PyTorch. Apologies for the inconvenience.

@Borda
Copy link
Contributor Author

Borda commented Oct 26, 2021

@datumbox fixed and resolved conflicts...

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

I went through the entire PR quickly. Unfortunately this is very hard because of the volume of files involved. I think overall looks OK, the if/elif marked above seemed to appear only to a couple of places so perhaps it was not reverted everywhere originally.

The changes on the code are for the best; I agree it produced a much cleaner result. I did find a couple of issues with the fstrings though (see below), which might be indicating issues with the automated tool used to refactor the code. Let me know your thoughts.

@Borda
Copy link
Contributor Author

Borda commented Oct 26, 2021

I went through the entire PR quickly.

@datumbox thank you for your review, I have fixed all issues...

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

Thanks for the changes!

@NicolasHug
Copy link
Member

NicolasHug commented Oct 28, 2021

@Borda thanks a lot for the PR and for bearing with us. There are still some minor cases where the script didn't use f-strings. I'll take care of these manually, and then merge the PR.

No need to do anything more on your end :)

Thanks a lot!

@Borda
Copy link
Contributor Author

Borda commented Oct 28, 2021

I'll take care of these manually, and then merge the PR.
No need to do anything more on your end :)

Thank you!

@NicolasHug
Copy link
Member

Failure is unrelated and will be taken care of by #4775

Merging, thanks a lot @Borda!

@github-actions
Copy link

Hey @NicolasHug!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

@Borda Borda deleted the pre-commit/pyupgrade branch October 28, 2021 12:12
facebook-github-bot pushed a commit that referenced this pull request Nov 1, 2021
…ing pyupgrade (#4585)

Summary: Co-authored-by: Nicolas Hug <[email protected]>

Reviewed By: datumbox

Differential Revision: D32064703

fbshipit-source-id: 5d8a0823844ec36e82d94cddaf5dd25ac18327f3
cyyever pushed a commit to cyyever/vision that referenced this pull request Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants