Raise TypeError instead of using asserting with condition of types. (#5536)

Note that assert are being removed in optimized with compiling to
optimised byte code (python -o producing *.pyo files).
This commit is contained in:
Arnaud Gelas 2021-01-19 13:39:43 +01:00 committed by GitHub
parent a9d9f33a86
commit 8566f697ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 5 deletions

View File

@ -372,7 +372,9 @@ def save_hparams_to_yaml(config_yaml, hparams: Union[dict, Namespace]) -> None:
OmegaConf.save(OmegaConf.create(hparams), fp, resolve=True)
return
assert isinstance(hparams, dict)
if not isinstance(hparams, dict):
raise TypeError("hparams must be dictionary")
hparams_allowed = {}
# drop paramaters which contain some strange datatypes as fsspec
for k, v in hparams.items():

View File

@ -100,11 +100,14 @@ class Result(Dict):
def _assert_tensor_metric(self, name: str, potential_metric: Union[bool, Tensor, None, Any]):
if potential_metric is not None and not isinstance(potential_metric, bool):
assert isinstance(potential_metric, Tensor), f'{name} must be a torch.Tensor'
if not isinstance(potential_metric, Tensor):
raise TypeError(f'{name} must be a torch.Tensor')
def _assert_grad_tensor_metric(self, name: str, x: Union[torch.Tensor, Any], additional_err: str = ''):
if x is not None:
assert isinstance(x, Tensor), f'{name} must be a torch.Tensor'
if not isinstance(x, Tensor):
raise TypeError(f'{name} must be a torch.Tensor')
m = f'{name} must have a computational graph.'
if additional_err:

View File

@ -155,7 +155,9 @@ class HookResultStore:
primary_dict[opt_idx][batch_idx].append(result)
def append(self, result, dataloader_idx: Optional[int] = None, extra_info: Optional[dict] = None) -> None:
assert isinstance(result, Result)
if not isinstance(result, Result):
raise TypeError(f'{result} must be Result')
if dataloader_idx is None:
dataloader_idx = 0

View File

@ -30,7 +30,9 @@ def determine_root_gpu_device(gpus: List[int]) -> Optional[int]:
if gpus is None:
return None
assert isinstance(gpus, list), "gpus should be a list"
if not isinstance(gpus, list):
raise TypeError("gpus should be a list")
assert len(gpus) > 0, "gpus should be a non empty list"
# set root gpu