Skip to content

Commit 1e884a1

Browse files
committed
Drop pylint for flake8 + fixes
1 parent fbf3dec commit 1e884a1

23 files changed

+74
-63
lines changed

.flake8

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[flake8]
2+
ignore =
3+
E203, # black and flake8 disagree on whitespace before ':'
4+
E501, # line too long (> 79 characters)
5+
W503, # black and flake8 disagree on how to place operators
6+
F403, # 'from module import *' used; unable to detect undefined names
7+
8+
per-file-ignores =
9+
# imported but unused
10+
__init__.py: F401
11+
12+
max-line-length = 88
13+
14+
# maximum McCabe complexity
15+
max-complexity = 12
16+
17+
exclude =
18+
build

.gitlab-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ pylint:
5353
before_script:
5454
- python --version
5555
- pip install compressai --find-links=dist/
56-
- pip install pylint
56+
- pip install flake8 flake8-bugbear flake8-comprehensions
5757
script:
58-
- pylint compressai examples
58+
- make check-flake8
5959
tags:
6060
- docker
6161

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ help: ## Show this message
1515

1616

1717
# Check style and linting
18-
.PHONY: check-black check-isort check-mypy static-analysis
18+
.PHONY: check-black check-isort check-flake8 check-mypy static-analysis
1919

2020
check-black: ## Run black checks
2121
@echo "--> Running black checks"
@@ -25,11 +25,15 @@ check-isort: ## Run isort checks
2525
@echo "--> Running isort checks"
2626
@isort --check-only $(src_dirs)
2727

28+
check-flake8: ## Run flake8 checks
29+
@echo "--> Running flake8 checks"
30+
@flake8 $(src_dirs)
31+
2832
check-mypy: ## Run mypy checks
2933
@echo "--> Running mypy checks"
3034
@mypy
3135

32-
static-analysis: check-black check-isort check-mypy ## Run all static checks
36+
static-analysis: check-black check-isort check-flake8 check-mypy ## Run all static checks
3337

3438

3539
# Apply styling

compressai/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def set_entropy_coder(entropy_coder):
3939
Args:
4040
entropy_coder (string): Name of the entropy coder
4141
"""
42-
global _entropy_coder # pylint: disable=W0603
42+
global _entropy_coder
4343
if entropy_coder not in _available_entropy_coders:
4444
raise ValueError(
4545
f'Invalid entropy coder "{entropy_coder}", choose from'

compressai/entropy_models/entropy_models.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
from torch import Tensor
1212

13-
# isort: off; pylint: disable=E0611,E0401
1413
from compressai._CXX import pmf_to_quantized_cdf as _pmf_to_quantized_cdf
1514
from compressai.ops import LowerBound
1615

17-
# isort: on; pylint: enable=E0611,E0401
18-
1916

2017
class _EntropyCoder:
2118
"""Proxy class to an actual entropy coder class."""
@@ -33,12 +30,12 @@ def __init__(self, method):
3330
)
3431

3532
if method == "ans":
36-
from compressai import ans # pylint: disable=E0611
33+
from compressai import ans
3734

3835
encoder = ans.RansEncoder()
3936
decoder = ans.RansDecoder()
4037
elif method == "rangecoder":
41-
import range_coder # pylint: disable=E0401
38+
import range_coder
4239

4340
encoder = range_coder.RangeEncoder()
4441
decoder = range_coder.RangeDecoder()
@@ -354,7 +351,7 @@ def _get_medians(self) -> Tensor:
354351
def update(self, force: bool = False) -> bool:
355352
# Check if we need to update the bottleneck parameters, the offsets are
356353
# only computed and stored when the conditonal model is update()'d.
357-
if self._offset.numel() > 0 and not force: # pylint: disable=E0203
354+
if self._offset.numel() > 0 and not force:
358355
return False
359356

360357
medians = self.quantiles[:, 0, 1]
@@ -580,7 +577,7 @@ def update_scale_table(self, scale_table, force=False):
580577
# updated.
581578
if self._offset.numel() > 0 and not force:
582579
return False
583-
device = self.scale_table.device # pylint: disable=E0203
580+
device = self.scale_table.device
584581
self.scale_table = self._prepare_scale_table(scale_table).to(device)
585582
self.update()
586583
return True

compressai/layers/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,3 @@
1414

1515
from .gdn import *
1616
from .layers import *
17-
18-
__all__ = [
19-
"GDN",
20-
"GDN1",
21-
"AttentionBlock",
22-
"MaskedConv2d",
23-
"ResidualBlock",
24-
"ResidualBlockUpsample",
25-
"ResidualBlockWithStride",
26-
"conv3x3",
27-
"subpel_conv3x3",
28-
]

compressai/layers/gdn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from compressai.ops.parametrizers import NonNegativeParametrizer
2222

23+
__all__ = ["GDN", "GDN1"]
24+
2325

2426
class GDN(nn.Module):
2527
r"""Generalized Divisive Normalization layer.

compressai/layers/layers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
from .gdn import GDN
2323

24+
__all__ = [
25+
"AttentionBlock",
26+
"MaskedConv2d",
27+
"ResidualBlock",
28+
"ResidualBlockUpsample",
29+
"ResidualBlockWithStride",
30+
"conv3x3",
31+
"subpel_conv3x3",
32+
]
33+
2434

2535
class MaskedConv2d(nn.Conv2d):
2636
r"""Masked 2D convolution implementation, mask future "unseen" pixels.

compressai/models/priors.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
import torch.nn as nn
2020
import torch.nn.functional as F
2121

22-
# pylint: disable=E0611,E0401
2322
from compressai.ans import BufferedRansEncoder, RansDecoder
2423
from compressai.entropy_models import EntropyBottleneck, GaussianConditional
2524
from compressai.layers import GDN, MaskedConv2d
2625

2726
from .utils import conv, deconv, update_registered_buffers
2827

29-
# pylint: enable=E0611,E0401
30-
31-
3228
__all__ = [
3329
"CompressionModel",
3430
"FactorizedPrior",
@@ -186,9 +182,7 @@ def decompress(self, strings, shape):
186182
SCALES_LEVELS = 64
187183

188184

189-
def get_scale_table(
190-
min=SCALES_MIN, max=SCALES_MAX, levels=SCALES_LEVELS
191-
): # pylint: disable=W0622
185+
def get_scale_table(min=SCALES_MIN, max=SCALES_MAX, levels=SCALES_LEVELS):
192186
return torch.exp(torch.linspace(math.log(min), math.log(max), levels))
193187

194188

compressai/utils/find_close/__main__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# pylint: disable=line-too-long
1615
"""
1716
Find the closest codec quality parameter to reach a given metric (bpp, ms-ssim,
1817
or psnr).
@@ -21,7 +20,6 @@
2120
* :code:`python -m compressai.utils.find_close webp ~/picture.png 0.5 --metric bpp`
2221
* :code:`python -m compressai.utils.find_close jpeg ~/picture.png 35 --metric psnr --save`
2322
"""
24-
# pylint: enable=line-too-long
2523

2624
import argparse
2725
import sys
@@ -47,7 +45,7 @@ def get_codec_q_bounds(codec: Codec) -> Tuple[bool, int, int]:
4745
upper = 64
4846
rev = True
4947
else:
50-
assert False, codec
48+
raise ValueError(f"Invalid codec {codec}")
5149
return rev, lower, upper
5250

5351

compressai/utils/update_model/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def load_checkpoint(filepath: Path) -> Dict[str, torch.Tensor]:
6363

6464

6565
description = """
66-
Export a trained model to a new checkpoint with an updated CDFs parameters and a
66+
Export a trained model to a new checkpoint with an updated CDFs parameters and a
6767
hash prefix, so that it can be loaded later via `load_state_dict_from_url`.
6868
""".strip()
6969

compressai/zoo/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from .image import *
15+
from .image import (
16+
bmshj2018_factorized,
17+
bmshj2018_hyperprior,
18+
cheng2020_anchor,
19+
cheng2020_attn,
20+
mbt2018,
21+
mbt2018_mean,
22+
)
1623
from .pretrained import load_pretrained as load_state_dict
1724

1825
models = {

examples/train.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ def configure_optimizers(net, args):
8282
"""Separate parameters for the main optimizer and the auxiliary optimizer.
8383
Return two optimizers"""
8484

85-
parameters = set(
85+
parameters = {
8686
n
8787
for n, p in net.named_parameters()
8888
if not n.endswith(".quantiles") and p.requires_grad
89-
)
90-
aux_parameters = set(
89+
}
90+
aux_parameters = {
9191
n
9292
for n, p in net.named_parameters()
9393
if n.endswith(".quantiles") and p.requires_grad
94-
)
94+
}
9595

9696
# Make sure we don't have an intersection of parameters
9797
params_dict = dict(net.named_parameters())
@@ -102,11 +102,11 @@ def configure_optimizers(net, args):
102102
assert len(union_params) - len(params_dict.keys()) == 0
103103

104104
optimizer = optim.Adam(
105-
(params_dict[n] for n in sorted(list(parameters))),
105+
(params_dict[n] for n in sorted(parameters)),
106106
lr=args.learning_rate,
107107
)
108108
aux_optimizer = optim.Adam(
109-
(params_dict[n] for n in sorted(list(aux_parameters))),
109+
(params_dict[n] for n in sorted(aux_parameters)),
110110
lr=args.aux_learning_rate,
111111
)
112112
return optimizer, aux_optimizer

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ ignore_missing_imports = True
2929

3030
[mypy-scipy.*]
3131
ignore_missing_imports = True
32+
33+
[mypy-numpy.*]
34+
ignore_missing_imports = True

pyproject.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ line_length = 88
2929
known_third_party = "PIL,pytorch_msssim,torchvision,torch"
3030
skip_gitignore = true
3131

32-
[tool.pylint.messages_control]
33-
disable = "C0330,C0326,bad-continuation,C0103,C0304,W0221,R0902,R0914,C0114,W0511,C0116,W0201,C0415,R0401,R0201,R0913,R0903,R0801"
34-
35-
[tool.pylint.format]
36-
max-line-length = "88"
37-
38-
[tool.pylint.typecheck]
39-
ignored-modules = "torch"
40-
extension-pkg-whitelist = "compressai.ans,compressai._cxx,range_coder"
41-
4232
[tool.pytest.ini_options]
4333
markers = [
4434
"pretrained: download and check pretrained models (slow, deselect with '-m \"not pretrained\"')",

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ def get_extensions():
8484

8585
TEST_REQUIRES = ["pytest", "pytest-cov"]
8686
DEV_REQUIRES = TEST_REQUIRES + [
87-
"pylint",
87+
"flake8",
88+
"flake8-bugbear",
89+
"flake8-comprehensions",
8890
"black",
8991
"isort",
9092
]

tests/test_bench_codec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_bench_codec(capsys, codec):
6161
assert expected["name"] == output["name"]
6262

6363
for key in ("psnr", "ms-ssim", "bpp"):
64-
if not key in expected["results"]:
64+
if key not in expected["results"]:
6565
continue
6666
assert np.allclose(
6767
expected["results"][key], output["results"][key], rtol=1e-5, atol=1e-5

tests/test_entropy_models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
EntropyModel,
2323
GaussianConditional,
2424
)
25-
from compressai.models.priors import FactorizedPrior
2625
from compressai.zoo import bmshj2018_factorized, bmshj2018_hyperprior
2726

2827

@@ -90,10 +89,10 @@ def test_forward(self, entropy_model):
9089

9190
def test_invalid_coder(self):
9291
with pytest.raises(ValueError):
93-
entropy_model = EntropyModel(entropy_coder="huffman")
92+
EntropyModel(entropy_coder="huffman")
9493

9594
with pytest.raises(ValueError):
96-
entropy_model = EntropyModel(entropy_coder=0xFF)
95+
EntropyModel(entropy_coder=0xFF)
9796

9897
def test_invalid_inputs(self, entropy_model):
9998
with pytest.raises(TypeError):

tests/test_eval_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_eval_model_pretrained(capsys, model, quality, metric, entropy_estimatio
101101
assert expected[key] == output[key]
102102

103103
for key in ("psnr", "ms-ssim", "bpp"):
104-
if not key in expected["results"]:
104+
if key not in expected["results"]:
105105
continue
106106
assert np.allclose(
107107
expected["results"][key], output["results"][key], rtol=1e-5, atol=1e-5

tests/test_init.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
def test_import_errors():
1717
import compressai
1818

19+
del compressai
20+
1921

2022
def test_version():
2123
from compressai.version import __version__

tests/test_scripting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ def test_masked_conv_A(self):
4343
conv = MaskedConv2d(3, 3, 3, padding=1)
4444

4545
with pytest.raises(RuntimeError):
46-
m = torch.jit.script(conv)
46+
torch.jit.script(conv)

tests/test_update_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_valid_name(tmpdir):
8383
assert len(stdout) == 0
8484
assert len(stderr) == 0
8585

86-
files = sorted(list(Path(tmpdir).glob("*.pth.tar")))
86+
files = sorted(Path(tmpdir).glob("*.pth.tar"))
8787
assert len(files) == 2
8888

8989
assert files[0].name == "model.pth.tar"

tests/test_waseda.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pytest
16-
import torch
17-
1815
from compressai.models.waseda import Cheng2020Anchor
1916
from compressai.zoo import cheng2020_anchor
2017

0 commit comments

Comments
 (0)