Skip to content

Commit

Permalink
replace isinstance(xxx, type) with inspect.isclass
Browse files Browse the repository at this point in the history
  • Loading branch information
HAOCHENYE committed May 11, 2023
1 parent 9446442 commit 3b75f07
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions mmcv/cnn/bricks/conv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
from typing import Dict, Optional

from mmengine.registry import MODELS
Expand Down Expand Up @@ -35,8 +36,8 @@ def build_conv_layer(cfg: Optional[Dict], *args, **kwargs) -> nn.Module:
cfg_ = cfg.copy()

layer_type = cfg_.pop('type')
if isinstance(layer_type, type):
return layer_type(*args, **kwargs, **cfg_)
if inspect.isclass(layer_type):
return layer_type(*args, **kwargs, **cfg_) # type: ignore
# Switch registry to the target scope. If `conv_layer` cannot be found
# in the registry, fallback to search `conv_layer` in the
# mmengine.MODELS.
Expand Down
3 changes: 2 additions & 1 deletion mmcv/cnn/bricks/padding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
from typing import Dict

import torch.nn as nn
Expand Down Expand Up @@ -27,7 +28,7 @@ def build_padding_layer(cfg: Dict, *args, **kwargs) -> nn.Module:

cfg_ = cfg.copy()
padding_type = cfg_.pop('type')
if isinstance(padding_type, type):
if inspect.isclass(padding_type):
return padding_type(*args, **kwargs, **cfg_)
# Switch registry to the target scope. If `padding_layer` cannot be found
# in the registry, fallback to search `padding_layer` in the
Expand Down
3 changes: 2 additions & 1 deletion mmcv/cnn/bricks/upsample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
from typing import Dict

import torch
Expand Down Expand Up @@ -76,7 +77,7 @@ def build_upsample_layer(cfg: Dict, *args, **kwargs) -> nn.Module:

layer_type = cfg_.pop('type')

if isinstance(layer_type, type):
if inspect.isclass(layer_type):
upsample = layer_type
# Switch registry to the target scope. If `upsample` cannot be found
# in the registry, fallback to search `upsample` in the
Expand Down
5 changes: 3 additions & 2 deletions tests/test_cnn/test_build_layers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
from importlib import import_module

import numpy as np
Expand Down Expand Up @@ -190,7 +191,7 @@ def test_build_activation_layer():
for module_name in ['activation', 'hsigmoid', 'hswish', 'swish']:
act_module = import_module(f'mmcv.cnn.bricks.{module_name}')
for key, value in act_module.__dict__.items():
if isinstance(value, type) and issubclass(value, nn.Module):
if inspect.isclass(value) and issubclass(value, nn.Module):
act_names.append(key)

with pytest.raises(TypeError):
Expand Down Expand Up @@ -235,7 +236,7 @@ def test_build_padding_layer():
for module_name in ['padding']:
pad_module = import_module(f'mmcv.cnn.bricks.{module_name}')
for key, value in pad_module.__dict__.items():
if isinstance(value, type) and issubclass(value, nn.Module):
if inspect.isclass(value) and issubclass(value, nn.Module):
pad_names.append(key)

with pytest.raises(TypeError):
Expand Down

0 comments on commit 3b75f07

Please sign in to comment.