Skip to content

Commit

Permalink
Simplify annotations as we are now python>=3.9 (#426)
Browse files Browse the repository at this point in the history
Starting from 3.9,
- using List, Type, Set, ... from typing is deprecated in favor of list,
  type, set, ... which can now be followed by []
- all classes existing in collections.abc like Callable should also
  directy be imported from there (from typing import Callable is now
  deprecated)

So we change the annotations accordingly, removing unnecessary imports from typing.
  • Loading branch information
nhuet authored Oct 4, 2024
1 parent 31d9bad commit 88ca799
Show file tree
Hide file tree
Showing 88 changed files with 698 additions and 727 deletions.
3 changes: 2 additions & 1 deletion examples/full_multisolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
to the proper certificate (https://stackoverflow.com/a/31060428).
"""
from collections.abc import Callable
from dataclasses import dataclass
from math import sqrt
from typing import Callable, Optional
from typing import Optional

import gymnasium as gym
import numpy as np
Expand Down
3 changes: 2 additions & 1 deletion examples/gym_jsbsim_greedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 typing import Any, Callable
from collections.abc import Callable
from typing import Any

import folium
import gym_jsbsim
Expand Down
3 changes: 2 additions & 1 deletion examples/gym_jsbsim_iw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 typing import Any, Callable, Optional
from collections.abc import Callable
from typing import Any, Optional

import folium
import gym_jsbsim
Expand Down
3 changes: 2 additions & 1 deletion examples/gym_jsbsim_riw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import bisect
import json
import math
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

import folium
import gym_jsbsim
Expand Down
3 changes: 2 additions & 1 deletion examples/gym_line_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from collections.abc import Callable
from math import exp, fabs, sqrt
from typing import Any, Callable, Optional
from typing import Optional

import gymnasium as gym
import numpy as np
Expand Down
3 changes: 2 additions & 1 deletion examples/nocycle_grid_goal_mdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import getopt
import sys
from collections.abc import Iterable
from enum import IntEnum
from math import sqrt
from typing import Iterable, NamedTuple, Optional
from typing import NamedTuple, Optional

from skdecide import (
DiscreteDistribution,
Expand Down
14 changes: 7 additions & 7 deletions examples/optuna_gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import annotations

import logging
from typing import Any, Dict, List, Tuple, Type, Union
from typing import Any, Union

import gymnasium as gym
from discrete_optimization.generic_tools.hyperparameters.hyperparameter import (
Expand Down Expand Up @@ -95,18 +95,18 @@ def heuristic(domain, state):

def bee1_features(
domain: Union[GymDomainForWidthSolvers, GymDomain], state: "State"
) -> Tuple[int, Any]:
) -> tuple[int, Any]:
return domain.bee1_features(state)


def bee2_features(
domain: Union[GymDomainForWidthSolvers, GymDomain], state: "State"
) -> Tuple[int, Any]:
) -> tuple[int, Any]:
return domain.bee2_features(state)


# Fixed kwargs per solver: either hyperparameters we do not want to search, or other parameters like time limits
kwargs_fixed_by_solver: Dict[Type[Solver], Dict[str, Any]] = {
kwargs_fixed_by_solver: dict[type[Solver], dict[str, Any]] = {
Astar: dict(
parallel=False,
verbose=False,
Expand All @@ -123,7 +123,7 @@ def bee2_features(


# Add new hyperparameters to some solvers
additional_hyperparameters_by_solver: Dict[Type[Solver], List[Hyperparameter]] = {
additional_hyperparameters_by_solver: dict[type[Solver], list[Hyperparameter]] = {
StableBaseline: [
# defined only if $algo_class \in [PPO]$
FloatHyperparameter(
Expand All @@ -148,8 +148,8 @@ def bee2_features(


# Restrict some hyperparameters choices, for some solvers (making use of `kwargs_by_name` of `suggest_with_optuna`)
suggest_optuna_kwargs_by_name_by_solver: Dict[
Type[Solver], Dict[str, Dict[str, Any]]
suggest_optuna_kwargs_by_name_by_solver: dict[
type[Solver], dict[str, dict[str, Any]]
] = {
StableBaseline: {
# restrict the choices of algo classes
Expand Down
3 changes: 2 additions & 1 deletion examples/optuna_gym_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 typing import Callable, Optional
from collections.abc import Callable
from typing import Optional

import gymnasium as gym

Expand Down
10 changes: 5 additions & 5 deletions examples/optuna_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import logging
import math
from typing import Any, Dict, List, Type
from typing import Any

from discrete_optimization.generic_tools.hyperparameters.hyperparameter import (
CategoricalHyperparameter,
Expand Down Expand Up @@ -85,7 +85,7 @@ def state_features(domain: Maze, state: State):


# Fixed kwargs per solver: either hyperparameters we do not want to search, or other parameters like time limits
kwargs_fixed_by_solver: Dict[Type[Solver], Dict[str, Any]] = {
kwargs_fixed_by_solver: dict[type[Solver], dict[str, Any]] = {
Astar: dict(
parallel=False,
verbose=False,
Expand Down Expand Up @@ -120,7 +120,7 @@ def state_features(domain: Maze, state: State):


# Add new hyperparameters to some solvers
additional_hyperparameters_by_solver: Dict[Type[Solver], List[Hyperparameter]] = {
additional_hyperparameters_by_solver: dict[type[Solver], list[Hyperparameter]] = {
# ex 1: ent_coef for StableBaselines for PPO algo only
StableBaseline: [
# defined only if $algo_class \in [PPO]$
Expand Down Expand Up @@ -156,8 +156,8 @@ def state_features(domain: Maze, state: State):


# Restrict some hyperparameters choices, for some solvers (making use of `kwargs_by_name` of `suggest_with_optuna`)
suggest_optuna_kwargs_by_name_by_solver: Dict[
Type[Solver], Dict[str, Dict[str, Any]]
suggest_optuna_kwargs_by_name_by_solver: dict[
type[Solver], dict[str, dict[str, Any]]
] = {
StableBaseline: {
# restrict the choices of algo classes
Expand Down
20 changes: 10 additions & 10 deletions examples/optuna_maze_explored_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import logging
import math
from typing import Any, Dict, List, Tuple, Type
from typing import Any

from discrete_optimization.generic_tools.hyperparameters.hyperparameter import (
CategoricalHyperparameter,
Expand Down Expand Up @@ -81,7 +81,7 @@ def state_features(domain: Maze, state: State):


# Fixed kwargs per solver: either hyperparameters we do not want to search, or other parameters like time limits
kwargs_fixed_by_solver: Dict[Type[Solver], Dict[str, Any]] = {
kwargs_fixed_by_solver: dict[type[Solver], dict[str, Any]] = {
Astar: dict(
parallel=False,
verbose=False,
Expand Down Expand Up @@ -111,7 +111,7 @@ def state_features(domain: Maze, state: State):
}

# Add new hyperparameters to some solvers
additional_hyperparameters_by_solver: Dict[Type[Solver], List[Hyperparameter]] = {
additional_hyperparameters_by_solver: dict[type[Solver], list[Hyperparameter]] = {
# ex: heuristic for A* and co
Astar: [
CategoricalHyperparameter(
Expand All @@ -135,8 +135,8 @@ def state_features(domain: Maze, state: State):


# Restrict some hyperparameters choices, for some solvers (making use of `kwargs_by_name` of `suggest_with_optuna`)
suggest_optuna_kwargs_by_name_by_solver: Dict[
Type[Solver], Dict[str, Dict[str, Any]]
suggest_optuna_kwargs_by_name_by_solver: dict[
type[Solver], dict[str, dict[str, Any]]
] = {
StableBaseline: {
# restrict the choices of algo classes
Expand All @@ -152,11 +152,11 @@ def state_features(domain: Maze, state: State):

def objective(
solver: Solver,
episodes: List[
Tuple[
List[D.T_agent[D.T_observation]],
List[D.T_agent[D.T_concurrency[D.T_event]]],
List[D.T_agent[Value[D.T_value]]],
episodes: list[
tuple[
list[D.T_agent[D.T_observation]],
list[D.T_agent[D.T_concurrency[D.T_event]]],
list[D.T_agent[Value[D.T_value]]],
]
],
) -> float:
Expand Down
Loading

0 comments on commit 88ca799

Please sign in to comment.