Skip to content

Consider upgrade to Python 3.8+ syntax with pyupgrade #94040

@XuehaiPan

Description

@XuehaiPan

🚀 The feature, motivation and pitch

PyTorch now supports Python 3.8+ only.

pytorch/setup.py

Lines 216 to 222 in ca84508

import platform
python_min_version = (3, 8, 0)
python_min_version_str = '.'.join(map(str, python_min_version))
if sys.version_info < python_min_version:
print("You are using Python {}. Python >={} is required.".format(platform.python_version(),
python_min_version_str))
sys.exit(-1)

But there are still many legacy Python syntaxes in the codebase. Such as:

Redundant object inherence:

- class A(object):
+ class A:
      ...

super call:

  class A:
      pass

  class B(A):
      def __init__(self):
-         super(B, self).__init__()
+         super().__init__()

string format:

- '%s' % x
+ f"{x}"
- '{}'.format(x)
+ f"{x}"

There is a tool pyupgrade that can automatically update the code. The migration can be easily done by:

pyupgraede --py38-plus $(find torch -name '*.py' -o -name '*.pyi')
lintrunner -a

It would be nice if you consider pre-commit integration to apply this for future code changes.

Alternatives

No response

Additional context

No response

cc @malfet @seemethere @ezyang @bhosmer @smessmer @ljk53 @bdhirsh

Metadata

Metadata

Assignees

No one assigned

    Labels

    better-engineeringRelatively self-contained tasks for better engineering contributorsmodule: buildBuild system issuesmodule: internalsRelated to internal abstractions in c10 and ATentriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions