Skip to content

[Lint] pyupgrade #2819

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 18 commits into from
Mar 3, 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
4 changes: 2 additions & 2 deletions .github/unittest/helpers/coverage_run_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def write_config(config_path: Path, argv: List[str]) -> None:
argv: Arguments passed to this script, which need to be converted to config file entries
"""
assert not config_path.exists(), "Temporary coverage config exists already"
cmdline = " ".join(shlex.quote(arg) for arg in argv[1:])
with open(str(config_path), "wt", encoding="utf-8") as fh:
cmdline = shlex.join(argv[1:])
with open(str(config_path), "w", encoding="utf-8") as fh:
fh.write(
f"""# .coveragerc to control coverage.py
[run]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
echo '::endgroup::'

echo '::group::Install lint tools'
pip install --progress-bar=off pre-commit
pip install --progress-bar=off pre-commit autoflake
echo '::endgroup::'

echo '::group::Lint Python source and configs'
Expand Down
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ repos:
hooks:
- id: pydocstyle
files: ^torchrl/

- repo: https://github.com/asottile/pyupgrade
rev: v3.9.0
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: local
hooks:
- id: autoflake
name: autoflake
entry: autoflake --in-place --remove-unused-variables --remove-all-unused-imports
language: system
types: [python]
4 changes: 3 additions & 1 deletion build_tools/setup_helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from .extension import CMakeBuild, get_ext_modules # noqa
from .extension import CMakeBuild, get_ext_modules

__all__ = ["CMakeBuild", "get_ext_modules"]
3 changes: 1 addition & 2 deletions build_tools/setup_helpers/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from setuptools import Extension
from setuptools.command.build_ext import build_ext


_THIS_DIR = Path(__file__).parent.resolve()
_ROOT_DIR = _THIS_DIR.parent.parent.resolve()
_TORCHRL_DIR = _ROOT_DIR / "torchrl"
Expand Down Expand Up @@ -130,7 +129,7 @@ def build_extension(self, ext):
# using -j in the build_ext call, not supported by pip or PyPA-build.
if hasattr(self, "parallel") and self.parallel:
# CMake 3.12+ only.
build_args += ["-j{}".format(self.parallel)]
build_args += [f"-j{self.parallel}"]

if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/reference/envs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ Recorders are transforms that register data as they come in, for logging purpose

Helpers
-------
.. currentmodule:: torchrl.envs.utils
.. currentmodule:: torchrl.envs

.. autosummary::
:toctree: generated/
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/objectives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ auto-completion to make their choice.
:template: rl_template_noinherit.rst

LossModule
add_random_module

DQN
---
Expand Down
2 changes: 2 additions & 0 deletions examples/rlhf/models/actor_critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations

from torchrl.modules.tensordict_module.actors import LMHeadActorValueOperator
from torchrl.modules.tensordict_module.common import VmapModule

Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ ignore-decorators =
test_*
; test/*.py
; .circleci/*

[autoflake]
per-file-ignores =
torchrl/trainers/helpers/envs.py *
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

def get_version():
version_txt = os.path.join(cwd, "version.txt")
with open(version_txt, "r") as f:
with open(version_txt) as f:
version = f.readline().strip()
if os.getenv("TORCHRL_BUILD_VERSION"):
version = os.getenv("TORCHRL_BUILD_VERSION")
Expand Down Expand Up @@ -64,8 +64,8 @@ def parse_args(argv: List[str]) -> argparse.Namespace:
def write_version_file(version):
version_path = os.path.join(cwd, "torchrl", "version.py")
with open(version_path, "w") as f:
f.write("__version__ = '{}'\n".format(version))
f.write("git_version = {}\n".format(repr(sha)))
f.write(f"__version__ = '{version}'\n")
f.write(f"git_version = {repr(sha)}\n")


def _get_pytorch_version(is_nightly, is_local):
Expand Down Expand Up @@ -185,7 +185,7 @@ def _main(argv):
version = get_version()
write_version_file(version)
TORCHRL_BUILD_VERSION = os.getenv("TORCHRL_BUILD_VERSION")
logging.info("Building wheel {}-{}".format(package_name, version))
logging.info(f"Building wheel {package_name}-{version}")
logging.info(f"TORCHRL_BUILD_VERSION is {TORCHRL_BUILD_VERSION}")

is_local = TORCHRL_BUILD_VERSION is None
Expand Down
2 changes: 1 addition & 1 deletion sota-implementations/a2c/a2c_atari.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@hydra.main(config_path="", config_name="config_atari", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821

from copy import deepcopy

Expand Down
2 changes: 1 addition & 1 deletion sota-implementations/a2c/a2c_mujoco.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@hydra.main(config_path="", config_name="config_mujoco", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821

from copy import deepcopy

Expand Down
5 changes: 1 addition & 4 deletions sota-implementations/cql/cql_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@

import hydra
import numpy as np

import torch
import tqdm
from tensordict.nn import CudaGraphModule

from torchrl._utils import timeit
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.objectives import group_optimizers
from torchrl.record.loggers import generate_exp_name, get_logger

from utils import (
dump_video,
log_metrics,
Expand All @@ -39,7 +36,7 @@


@hydra.main(config_path="", config_name="offline_config", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
# Create logger
exp_name = generate_exp_name("CQL-offline", cfg.logger.exp_name)
logger = None
Expand Down
4 changes: 1 addition & 3 deletions sota-implementations/cql/cql_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
import tqdm
from tensordict import TensorDict
from tensordict.nn import CudaGraphModule

from torchrl._utils import timeit
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.objectives import group_optimizers
from torchrl.record.loggers import generate_exp_name, get_logger

from utils import (
dump_video,
log_metrics,
Expand All @@ -42,7 +40,7 @@


@hydra.main(version_base="1.1", config_path="", config_name="online_config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
# Create logger
exp_name = generate_exp_name("CQL-online", cfg.logger.exp_name)
logger = None
Expand Down
6 changes: 1 addition & 5 deletions sota-implementations/cql/discrete_cql_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@

import hydra
import numpy as np

import torch
import torch.cuda
import tqdm
from tensordict.nn import CudaGraphModule

from torchrl._utils import timeit

from torchrl.envs.utils import ExplorationType, set_exploration_type

from torchrl.record.loggers import generate_exp_name, get_logger
from utils import (
log_metrics,
Expand All @@ -41,7 +37,7 @@


@hydra.main(version_base="1.1", config_path="", config_name="discrete_cql_config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
device = cfg.optim.device
if device in ("", None):
if torch.cuda.is_available():
Expand Down
6 changes: 1 addition & 5 deletions sota-implementations/crossq/crossq.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
import warnings

import hydra

import numpy as np

import torch
import torch.cuda
import tqdm
from tensordict import TensorDict
from tensordict.nn import CudaGraphModule

from torchrl._utils import timeit
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.objectives import group_optimizers

from torchrl.record.loggers import generate_exp_name, get_logger
from utils import (
log_metrics,
Expand All @@ -43,7 +39,7 @@


@hydra.main(version_base="1.1", config_path=".", config_name="config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
device = cfg.network.device
if device in ("", None):
if torch.cuda.is_available():
Expand Down
5 changes: 1 addition & 4 deletions sota-implementations/ddpg/ddpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
import warnings

import hydra

import numpy as np
import torch
import torch.cuda
import tqdm
from tensordict import TensorDict
from tensordict.nn import CudaGraphModule

from torchrl._utils import timeit

from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.objectives import group_optimizers
from torchrl.record.loggers import generate_exp_name, get_logger
Expand All @@ -41,7 +38,7 @@


@hydra.main(version_base="1.1", config_path="", config_name="config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
device = cfg.optim.device
if device in ("", None):
if torch.cuda.is_available():
Expand Down
4 changes: 1 addition & 3 deletions sota-implementations/decision_transformer/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
from tensordict.nn import CudaGraphModule
from torchrl._utils import logger as torchrl_logger, timeit
from torchrl.envs.libs.gym import set_gym_backend

from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.modules.tensordict_module import DecisionTransformerInferenceWrapper
from torchrl.record import VideoRecorder

from utils import (
dump_video,
log_metrics,
Expand All @@ -37,7 +35,7 @@


@hydra.main(config_path="", config_name="dt_config", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
set_gym_backend(cfg.env.backend).set()

model_device = cfg.optim.device
Expand Down
3 changes: 1 addition & 2 deletions sota-implementations/decision_transformer/online_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.modules.tensordict_module import DecisionTransformerInferenceWrapper
from torchrl.record import VideoRecorder

from utils import (
dump_video,
log_metrics,
Expand All @@ -34,7 +33,7 @@


@hydra.main(config_path="", config_name="odt_config", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
set_gym_backend(cfg.env.backend).set()

model_device = cfg.optim.device
Expand Down
2 changes: 1 addition & 1 deletion sota-implementations/discrete_sac/discrete_sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


@hydra.main(version_base="1.1", config_path="", config_name="config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
device = cfg.network.device
if device in ("", None):
if torch.cuda.is_available():
Expand Down
3 changes: 1 addition & 2 deletions sota-implementations/dqn/dqn_atari.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tqdm
from tensordict.nn import CudaGraphModule, TensorDictSequential
from torchrl._utils import timeit

from torchrl.collectors import SyncDataCollector
from torchrl.data import LazyMemmapStorage, TensorDictReplayBuffer
from torchrl.envs import ExplorationType, set_exploration_type
Expand All @@ -32,7 +31,7 @@


@hydra.main(config_path="", config_name="config_atari", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821

device = cfg.device
if device in ("", None):
Expand Down
3 changes: 1 addition & 2 deletions sota-implementations/dqn/dqn_cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import torch.nn
import torch.optim
import tqdm

from tensordict.nn import CudaGraphModule, TensorDictSequential
from torchrl._utils import timeit
from torchrl.collectors import SyncDataCollector
Expand All @@ -27,7 +26,7 @@


@hydra.main(config_path="", config_name="config_cartpole", version_base="1.1")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821

device = cfg.device
if device in ("", None):
Expand Down
4 changes: 2 additions & 2 deletions sota-implementations/dreamer/dreamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import torch
import torch.cuda
import tqdm

from dreamer_utils import (
_default_device,
dump_video,
Expand All @@ -27,7 +28,6 @@
from torchrl._utils import logger as torchrl_logger, timeit
from torchrl.envs.utils import ExplorationType, set_exploration_type
from torchrl.modules import RSSMRollout

from torchrl.objectives.dreamer import (
DreamerActorLoss,
DreamerModelLoss,
Expand All @@ -37,7 +37,7 @@


@hydra.main(version_base="1.1", config_path="", config_name="config")
def main(cfg: "DictConfig"): # noqa: F821
def main(cfg: DictConfig): # noqa: F821
# cfg = correct_for_frame_skip(cfg)

device = _default_device(cfg.networks.device)
Expand Down
Loading
Loading