Skip to content
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
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ repos:
# flake8-tidy-imports is used for banned-modules, not actually tidying
additional_dependencies: [flake8-comprehensions==3.2.2, flake8-tidy-imports==4.1.0, flake8-bugbear==20.1.4]

- repo: https://github.com/asottile/pyupgrade
rev: v2.7.0
hooks:
- id: pyupgrade
args: [--py3-plus]
exclude: .*barracuda.py
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't want to run this on the barracuda scripts (similar to how we don't run pylint on them).


- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion gym-unity/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run(self):
tag = os.getenv("CIRCLE_TAG")

if tag != EXPECTED_TAG:
info = "Git tag: {0} does not match the expected tag of this app: {1}".format(
info = "Git tag: {} does not match the expected tag of this app: {}".format(
Copy link
Contributor

Choose a reason for hiding this comment

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

should we convert this to an actual f-string? f"Git tag:...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was saving that for the next PR. Adding --py36-plus should convert these automatically. The only ones I rewrote were the ones in environment.py (because one of them was broken).

tag, EXPECTED_TAG
)
sys.exit(info)
Expand Down
2 changes: 1 addition & 1 deletion ml-agents-envs/mlagents_envs/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mlagents_envs.communicator_objects.unity_input_pb2 import UnityInputProto


class Communicator(object):
class Communicator:
def __init__(self, worker_id=0, base_port=5005):
"""
Python side of the communication. Must be used in pair with the right Unity Communicator equivalent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mlagents_envs.communicator_objects import unity_message_pb2 as mlagents__envs_dot_communicator__objects_dot_unity__message__pb2


class UnityToExternalProtoStub(object):
class UnityToExternalProtoStub:
# missing associated documentation comment in .proto file
pass

Expand All @@ -21,7 +21,7 @@ def __init__(self, channel):
)


class UnityToExternalProtoServicer(object):
class UnityToExternalProtoServicer:
# missing associated documentation comment in .proto file
pass

Expand Down
17 changes: 8 additions & 9 deletions ml-agents-envs/mlagents_envs/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ def behavior_specs(self) -> MappingType[str, BehaviorSpec]:
def _assert_behavior_exists(self, behavior_name: str) -> None:
if behavior_name not in self._env_specs:
raise UnityActionException(
"The group {0} does not correspond to an existing agent group "
"in the environment".format(behavior_name)
f"The group {behavior_name} does not correspond to an existing "
f"agent group in the environment"
)

def set_actions(self, behavior_name: BehaviorName, action: np.ndarray) -> None:
Expand All @@ -339,9 +339,9 @@ def set_actions(self, behavior_name: BehaviorName, action: np.ndarray) -> None:
expected_shape = (len(self._env_state[behavior_name][0]), spec.action_size)
if action.shape != expected_shape:
raise UnityActionException(
"The behavior {0} needs an input of dimension {1} for "
"(<number of agents>, <action size>) but received input of "
"dimension {2}".format(behavior_name, expected_shape, action.shape)
f"The behavior {behavior_name} needs an input of dimension "
f"{expected_shape} for (<number of agents>, <action size>) but "
f"received input of dimension {action.shape}"
)
if action.dtype != expected_type:
action = action.astype(expected_type)
Expand All @@ -357,10 +357,9 @@ def set_action_for_agent(
expected_shape = (spec.action_size,)
if action.shape != expected_shape:
raise UnityActionException(
f"The Agent {0} with BehaviorName {1} needs an input of dimension "
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This combination of an f-string with numbered .format() exposed a bug in pyupgrade: asottile/pyupgrade#329

f"{2} but received input of dimension {3}".format(
agent_id, behavior_name, expected_shape, action.shape
)
f"The Agent {agent_id} with BehaviorName {behavior_name} needs "
f"an input of dimension {expected_shape} but received input of "
f"dimension {action.shape}"
)
expected_type = np.float32 if spec.is_action_continuous() else np.int32
if action.dtype != expected_type:
Expand Down
2 changes: 1 addition & 1 deletion ml-agents-envs/mlagents_envs/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ class UnityWorkerInUseException(UnityException):

def __init__(self, worker_id):
message = self.MESSAGE_TEMPLATE.format(str(worker_id))
super(UnityWorkerInUseException, self).__init__(message)
super().__init__(message)
2 changes: 1 addition & 1 deletion ml-agents-envs/mlagents_envs/rpc_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def check_port(self, port):
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(("localhost", port))
except socket.error:
except OSError:
raise UnityWorkerInUseException(self.worker_id)
finally:
s.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SamplerTypes(IntEnum):
MULTIRANGEUNIFORM = 2

def __init__(self) -> None:
channel_id = uuid.UUID(("534c891e-810f-11ea-a9d0-822485860400"))
channel_id = uuid.UUID("534c891e-810f-11ea-a9d0-822485860400")
super().__init__(channel_id)

def on_message_received(self, msg: IncomingMessage) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FloatPropertiesChannel(SideChannel):
def __init__(self, channel_id: uuid.UUID = None) -> None:
self._float_properties: Dict[str, float] = {}
if channel_id is None:
channel_id = uuid.UUID(("60ccf7d0-4f7e-11ea-b238-784f4387d1f7"))
channel_id = uuid.UUID("60ccf7d0-4f7e-11ea-b238-784f4387d1f7")
super().__init__(channel_id)

def on_message_received(self, msg: IncomingMessage) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def process_side_channel_message(self, data: bytes) -> None:
)
if len(message_data) != message_len:
raise UnityEnvironmentException(
"The message received by the side channel {0} was "
"The message received by the side channel {} was "
"unexpectedly short. Make sure your Unity Environment "
"sending side channel data properly.".format(channel_id)
)
Expand Down
4 changes: 2 additions & 2 deletions ml-agents-envs/mlagents_envs/tests/test_side_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def test_raw_bytes():
sender = RawBytesChannel(guid)
receiver = RawBytesChannel(guid)

sender.send_raw_data("foo".encode("ascii"))
sender.send_raw_data("bar".encode("ascii"))
sender.send_raw_data(b"foo")
sender.send_raw_data(b"bar")

data = SideChannelManager([sender]).generate_side_channel_messages()
SideChannelManager([receiver]).process_side_channel_message(data)
Expand Down
2 changes: 1 addition & 1 deletion ml-agents-envs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run(self):
tag = os.getenv("CIRCLE_TAG")

if tag != EXPECTED_TAG:
info = "Git tag: {0} does not match the expected tag of this app: {1}".format(
info = "Git tag: {} does not match the expected tag of this app: {}".format(
tag, EXPECTED_TAG
)
sys.exit(info)
Expand Down
4 changes: 2 additions & 2 deletions ml-agents/mlagents/trainers/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(self):
super().__init__()

def __str__(self):
return ", ".join(["'{0}' : {1}".format(k, str(self[k])) for k in self.keys()])
return ", ".join(["'{}' : {}".format(k, str(self[k])) for k in self.keys()])

def reset_agent(self) -> None:
"""
Expand Down Expand Up @@ -275,7 +275,7 @@ def resequence_and_append(
key_list = list(self.keys())
if not self.check_length(key_list):
raise BufferException(
"The length of the fields {0} were not of same length".format(key_list)
"The length of the fields {} were not of same length".format(key_list)
)
for field_key in key_list:
target_buffer[field_key].extend(
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def load_config(config_path: str) -> Dict[str, Any]:
try:
with open(config_path) as data_file:
return _load_config(data_file)
except IOError:
except OSError:
abs_path = os.path.abspath(config_path)
raise TrainerConfigError(f"Config file could not be found at {abs_path}.")
except UnicodeDecodeError:
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/components/bc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mlagents.trainers.policy.tf_policy import TFPolicy


class BCModel(object):
class BCModel:
def __init__(
self, policy: TFPolicy, learning_rate: float = 3e-4, anneal_steps: int = 0
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mlagents.trainers.policy.tf_policy import TFPolicy


class CuriosityModel(object):
class CuriosityModel:
def __init__(
self, policy: TFPolicy, encoding_size: int = 128, learning_rate: float = 3e-4
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
EPSILON = 1e-7


class GAILModel(object):
class GAILModel:
def __init__(
self,
policy: TFPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create_reward_signal(
"""
rcls = NAME_TO_CLASS.get(name)
if not rcls:
raise UnityTrainerException("Unknown reward signal type {0}".format(name))
raise UnityTrainerException("Unknown reward signal type {}".format(name))

class_inst = rcls(policy, settings)
return class_inst
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/ghost/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
:param artifact_path: Path to store artifacts from this trainer.
"""

super(GhostTrainer, self).__init__(
super().__init__(
brain_name, trainer_settings, training, artifact_path, reward_buff_cap
)

Expand Down
4 changes: 2 additions & 2 deletions ml-agents/mlagents/trainers/policy/tf_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _load_graph(self, model_path: str, reset_global_steps: bool = False) -> None
ckpt = tf.train.get_checkpoint_state(model_path)
if ckpt is None:
raise UnityPolicyException(
"The model {0} could not be loaded. Make "
"The model {} could not be loaded. Make "
"sure you specified the right "
"--run-id and that the previous run you are loading from had the same "
"behavior names.".format(model_path)
Expand All @@ -167,7 +167,7 @@ def _load_graph(self, model_path: str, reset_global_steps: bool = False) -> None
self.saver.restore(self.sess, ckpt.model_checkpoint_path)
except tf.errors.NotFoundError:
raise UnityPolicyException(
"The model {0} was found but could not be loaded. Make "
"The model {} was found but could not be loaded. Make "
"sure the model is from the same version of ML-Agents, has the same behavior parameters, "
"and is using the same trainer configuration as the current run.".format(
model_path
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/ppo/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
:param seed: The seed the model will be initialized with
:param artifact_path: The directory within which to store artifacts from this trainer.
"""
super(PPOTrainer, self).__init__(
super().__init__(
brain_name, trainer_settings, training, artifact_path, reward_buff_cap
)
self.hyperparameters: PPOSettings = cast(
Expand Down
6 changes: 3 additions & 3 deletions ml-agents/mlagents/trainers/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def add_property(
) -> None:
if property_type == StatsPropertyType.HYPERPARAMETERS:
logger.info(
"""Hyperparameters for behavior name {0}: \n{1}""".format(
"""Hyperparameters for behavior name {}: \n{}""".format(
category, self._dict_to_str(value, 0)
)
)
Expand All @@ -150,7 +150,7 @@ def _dict_to_str(self, param_dict: Dict[str, Any], num_tabs: int) -> str:
[
"\t"
+ " " * num_tabs
+ "{0}:\t{1}".format(
+ "{}:\t{}".format(
x, self._dict_to_str(param_dict[x], num_tabs + 1)
)
for x in param_dict
Expand Down Expand Up @@ -226,7 +226,7 @@ def _dict_to_tensorboard(self, name: str, input_dict: Dict[str, Any]) -> str:
s_op = tf.summary.text(
name,
tf.convert_to_tensor(
([[str(x), str(input_dict[x])] for x in input_dict])
[[str(x), str(input_dict[x])] for x in input_dict]
),
)
s = sess.run(s_op)
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/tests/test_training_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_globaltrainingstatus(tmpdir):
GlobalTrainingStatus.set_parameter_state("Category1", StatusType.LESSON_NUM, 3)
GlobalTrainingStatus.save_state(path_dir)

with open(path_dir, "r") as fp:
with open(path_dir) as fp:
test_json = json.load(fp)

assert "Category1" in test_json
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/trainer/rl_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RLTrainer(Trainer): # pylint: disable=abstract-method
"""

def __init__(self, *args, **kwargs):
super(RLTrainer, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
# collected_rewards is a dictionary from name of reward signal to a dictionary of agent_id to cumulative reward
# used for reporting only. We always want to report the environment reward to Tensorboard, regardless
# of what reward signals are actually present.
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/trainer_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from mlagents.trainers.agent_processor import AgentManager


class TrainerController(object):
class TrainerController:
def __init__(
self,
trainer_factory: TrainerFactory,
Expand Down
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/training_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def load_state(path: str) -> None:
:param path: Path to the JSON file containing the state.
"""
try:
with open(path, "r") as f:
with open(path) as f:
loaded_dict = json.load(f)
# Compare the metadata
_metadata = loaded_dict[StatusType.STATS_METADATA.value]
Expand Down
3 changes: 1 addition & 2 deletions ml-agents/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from io import open
import os
import sys

Expand All @@ -25,7 +24,7 @@ def run(self):
tag = os.getenv("CIRCLE_TAG")

if tag != EXPECTED_TAG:
info = "Git tag: {0} does not match the expected tag of this app: {1}".format(
info = "Git tag: {} does not match the expected tag of this app: {}".format(
tag, EXPECTED_TAG
)
sys.exit(info)
Expand Down
1 change: 0 additions & 1 deletion ml-agents/tests/yamato/check_coverage_percent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import sys
import os

Expand Down
2 changes: 1 addition & 1 deletion utils/validate_release_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def check_file(filename: str, global_allow_pattern: Pattern) -> List[str]:
Validate a single file and return any offending lines.
"""
bad_lines = []
with open(filename, "r") as f:
with open(filename) as f:
for line in f:
if not RELEASE_PATTERN.search(line):
continue
Expand Down
4 changes: 2 additions & 2 deletions utils/validate_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def set_version(


def set_package_version(new_version: str) -> None:
with open(MLAGENTS_PACKAGE_JSON_PATH, "r") as f:
with open(MLAGENTS_PACKAGE_JSON_PATH) as f:
package_json = json.load(f)
if "version" in package_json:
package_json["version"] = new_version
Expand All @@ -104,7 +104,7 @@ def set_package_version(new_version: str) -> None:


def set_extension_package_version(new_version: str) -> None:
with open(MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH, "r") as f:
with open(MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH) as f:
package_json = json.load(f)
package_json["dependencies"]["com.unity.ml-agents"] = new_version
with open(MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH, "w") as f:
Expand Down