The following issues were found

keras/mixed_precision/model_test.py
452 issues
Unable to import 'tensorflow.compat.v2'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Tests keras.Model works properly with mixed precision."""

import tensorflow.compat.v2 as tf

import os

from absl import flags
from absl.testing import parameterized

            

Reported by Pylint.

Unable to import 'absl'
Error

Line: 21 Column: 1

              
import os

from absl import flags
from absl.testing import parameterized
import numpy as np
from keras import backend
from keras import combinations
from keras import keras_parameterized

            

Reported by Pylint.

Unable to import 'absl.testing'
Error

Line: 22 Column: 1

              import os

from absl import flags
from absl.testing import parameterized
import numpy as np
from keras import backend
from keras import combinations
from keras import keras_parameterized
from keras import layers

            

Reported by Pylint.

TODO(b/121381184): Enable running the test in this case.
Error

Line: 659 Column: 3

                  strategy = strategy_fn()
    if (isinstance(strategy, tf.distribute.MirroredStrategy) and
        not tf.executing_eagerly()):
      # TODO(b/121381184): Enable running the test in this case.
      return

    # Create and run model.
    with strategy.scope():
      x = layers.Input(shape=(2,), batch_size=2, dtype=tf.float32)

            

Reported by Pylint.

Access to a protected member _create_all_weights of a client class
Error

Line: 713 Column: 5

                  model.load_weights(os.path.join(ckpt_dir, 'ckpt'))
    model.compile(opt, 'mse', run_eagerly=testing_utils.should_run_eagerly())
    model(np.zeros((2, 2)))  # Create model weights
    opt._create_all_weights(model.weights)
    expected_kernel = np.array([[9.229685, 10.901115], [10.370763, 9.757362]])
    expected_slot = np.array([[10.049943, 9.917691], [10.049943, 9.917691]])
    self.assertAllClose(self.evaluate(model.weights[0]), expected_kernel)
    self.assertAllClose(
        self.evaluate(opt.get_slot(model.weights[0], 'momentum')),

            

Reported by Pylint.

TODO(reedwm): Support and test saving model with a mixed_[b]float16 policy
Error

Line: 775 Column: 3

                    })
  def test_save_model_with_dynamic_loss_scaling(
      self, strategy_fn, h5=False, use_v1_loss_scale_optimizer=False):
    # TODO(reedwm): Support and test saving model with a mixed_[b]float16 policy
    # as well.
    strategy = strategy_fn()
    if (isinstance(strategy, tf.distribute.MirroredStrategy) and
        not tf.executing_eagerly()):
      # TODO(b/121381184): Enable running the test in this case.

            

Reported by Pylint.

TODO(b/121381184): Enable running the test in this case.
Error

Line: 780 Column: 3

                  strategy = strategy_fn()
    if (isinstance(strategy, tf.distribute.MirroredStrategy) and
        not tf.executing_eagerly()):
      # TODO(b/121381184): Enable running the test in this case.
      return

    # Create and run model.
    with strategy.scope():
      x = layers.Input(shape=(2,), batch_size=2, dtype=tf.float32)

            

Reported by Pylint.

TODO(reedwm): Always save/restore the loss scale with Model.save().
Error

Line: 828 Column: 3

                  # Currently the loss scale isn't always saved when the model is saved with
    # Model.save(). So we assert the loss scale either has the value when it was
    # saved, or the value it was initialized with.
    # TODO(reedwm): Always save/restore the loss scale with Model.save().
    self.assertIn(backend.get_value(model.optimizer.loss_scale), (1, 2))
    self.assertIn(backend.get_value(model.optimizer.dynamic_counter), (0, 1))

    # Test optimizer attributes and type
    self.assertEqual(model.optimizer.initial_scale, 1.)

            

Reported by Pylint.

standard import "import os" should be placed before "import tensorflow.compat.v2 as tf"
Error

Line: 19 Column: 1

              
import tensorflow.compat.v2 as tf

import os

from absl import flags
from absl.testing import parameterized
import numpy as np
from keras import backend

            

Reported by Pylint.

Bad indentation. Found 2 spaces, expected 4
Style

Line: 59 Column: 1

              

def create_mirrored_strategy():
  """Create a MirroredStrategy, using a GPU if it is available."""
  if tf.config.list_logical_devices('GPU'):
    return tf.distribute.MirroredStrategy(['cpu:0', 'gpu:0'])
  else:
    return tf.distribute.MirroredStrategy(['cpu:0'])


            

Reported by Pylint.

keras/optimizer_v1.py
450 issues
Bad option value 'g-classes-have-attributes'
Error

Line: 16 Column: 1

              # limitations under the License.
# ==============================================================================
# pylint: disable=invalid-name
# pylint: disable=g-classes-have-attributes
"""Legacy v1 optimizer classes.

For more examples see the base class `tf.compat.v1.keras.optimizers.Optimizer`.
"""


            

Reported by Pylint.

Unable to import 'tensorflow.compat.v2'
Error

Line: 22 Column: 1

              For more examples see the base class `tf.compat.v1.keras.optimizers.Optimizer`.
"""

import tensorflow.compat.v2 as tf
from keras import backend


class Optimizer:
  """Abstract optimizer base class.

            

Reported by Pylint.

Instance of 'Optimizer' has no 'clipnorm' member
Error

Line: 94 Column: 35

                                     'Common ops without gradient: '
                       'backend.argmax, backend.round, backend.eval.')
    if hasattr(self, 'clipnorm'):
      grads = [tf.clip_by_norm(g, self.clipnorm) for g in grads]
    if hasattr(self, 'clipvalue'):
      grads = [
          tf.clip_by_value(g, -self.clipvalue, self.clipvalue)
          for g in grads
      ]

            

Reported by Pylint.

Instance of 'Optimizer' has no 'clipvalue' member
Error

Line: 97 Column: 32

                    grads = [tf.clip_by_norm(g, self.clipnorm) for g in grads]
    if hasattr(self, 'clipvalue'):
      grads = [
          tf.clip_by_value(g, -self.clipvalue, self.clipvalue)
          for g in grads
      ]
    return grads

  def set_weights(self, weights):

            

Reported by Pylint.

Instance of 'Optimizer' has no 'clipvalue' member
Error

Line: 97 Column: 48

                    grads = [tf.clip_by_norm(g, self.clipnorm) for g in grads]
    if hasattr(self, 'clipvalue'):
      grads = [
          tf.clip_by_value(g, -self.clipvalue, self.clipvalue)
          for g in grads
      ]
    return grads

  def set_weights(self, weights):

            

Reported by Pylint.

Instance of 'Optimizer' has no 'clipnorm' member
Error

Line: 143 Column: 28

                def get_config(self):
    config = {}
    if hasattr(self, 'clipnorm'):
      config['clipnorm'] = self.clipnorm
    if hasattr(self, 'clipvalue'):
      config['clipvalue'] = self.clipvalue
    return config

  @classmethod

            

Reported by Pylint.

Instance of 'Optimizer' has no 'clipvalue' member
Error

Line: 145 Column: 29

                  if hasattr(self, 'clipnorm'):
      config['clipnorm'] = self.clipnorm
    if hasattr(self, 'clipvalue'):
      config['clipvalue'] = self.clipvalue
    return config

  @classmethod
  def from_config(cls, config):
    return cls(**config)

            

Reported by Pylint.

Bad option value 'g-no-augmented-assignment'
Error

Line: 189 Column: 1

              
    lr = self.lr
    if self.initial_decay > 0:
      lr = lr * (  # pylint: disable=g-no-augmented-assignment
          1. /
          (1. +
           self.decay * tf.cast(self.iterations,
                                      backend.dtype(self.decay))))
    # momentum

            

Reported by Pylint.

Bad option value 'g-no-augmented-assignment'
Error

Line: 264 Column: 1

              
    lr = self.lr
    if self.initial_decay > 0:
      lr = lr * (  # pylint: disable=g-no-augmented-assignment
          1. /
          (1. +
           self.decay * tf.cast(self.iterations,
                                      backend.dtype(self.decay))))


            

Reported by Pylint.

Bad option value 'g-no-augmented-assignment'
Error

Line: 340 Column: 1

              
    lr = self.lr
    if self.initial_decay > 0:
      lr = lr * (  # pylint: disable=g-no-augmented-assignment
          1. /
          (1. +
           self.decay * tf.cast(self.iterations,
                                      backend.dtype(self.decay))))


            

Reported by Pylint.

keras/mixed_precision/autocast_variable_test.py
448 issues
Unable to import 'tensorflow.compat.v2'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Tests for AutoCastVariable."""

import tensorflow.compat.v2 as tf

import os
import threading

from absl.testing import parameterized

            

Reported by Pylint.

Unable to import 'absl.testing'
Error

Line: 22 Column: 1

              import os
import threading

from absl.testing import parameterized
import numpy as np
from keras.mixed_precision import autocast_variable
from keras.optimizer_v2 import adadelta
from keras.optimizer_v2 import adagrad
from keras.optimizer_v2 import adam

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 168 Column: 47

                        self.assertEqual(self.evaluate(x.value()), 7)
          self.assertEqual(self.evaluate(x.read_value()), 7)
          self.assertTrue(x.trainable)
          self.assertEqual(x.synchronization, x._variable.synchronization)
          self.assertEqual(x.aggregation, x._variable.aggregation)
          self.assertEqual(self.evaluate(x.initialized_value()), 7)
          if not tf.executing_eagerly():
            if not tf.distribute.has_strategy():
              # These functions are not supported for DistributedVariables

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 169 Column: 43

                        self.assertEqual(self.evaluate(x.read_value()), 7)
          self.assertTrue(x.trainable)
          self.assertEqual(x.synchronization, x._variable.synchronization)
          self.assertEqual(x.aggregation, x._variable.aggregation)
          self.assertEqual(self.evaluate(x.initialized_value()), 7)
          if not tf.executing_eagerly():
            if not tf.distribute.has_strategy():
              # These functions are not supported for DistributedVariables
              x.load(9)

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 177 Column: 36

                            x.load(9)
              self.assertEqual(x.eval(), 9)
            self.assertEqual(self.evaluate(x.initial_value), 7)
            self.assertEqual(x.op, x._variable.op)
            self.assertEqual(x.graph, x._variable.graph)
          if not tf.distribute.has_strategy():
            # These attributes are not supported for DistributedVariables
            self.assertIsNone(x.constraint)
            self.assertEqual(x.initializer, x._variable.initializer)

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 178 Column: 39

                            self.assertEqual(x.eval(), 9)
            self.assertEqual(self.evaluate(x.initial_value), 7)
            self.assertEqual(x.op, x._variable.op)
            self.assertEqual(x.graph, x._variable.graph)
          if not tf.distribute.has_strategy():
            # These attributes are not supported for DistributedVariables
            self.assertIsNone(x.constraint)
            self.assertEqual(x.initializer, x._variable.initializer)
          self.assertEqual(evaluate(x.assign(8)), 8)

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 182 Column: 45

                        if not tf.distribute.has_strategy():
            # These attributes are not supported for DistributedVariables
            self.assertIsNone(x.constraint)
            self.assertEqual(x.initializer, x._variable.initializer)
          self.assertEqual(evaluate(x.assign(8)), 8)
          self.assertEqual(evaluate(x.assign_add(2)), 10)
          self.assertEqual(evaluate(x.assign_sub(3)), 7)
          self.assertEqual(x.name, x._variable.name)
          self.assertEqual(x.device, x._variable.device)

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 186 Column: 36

                        self.assertEqual(evaluate(x.assign(8)), 8)
          self.assertEqual(evaluate(x.assign_add(2)), 10)
          self.assertEqual(evaluate(x.assign_sub(3)), 7)
          self.assertEqual(x.name, x._variable.name)
          self.assertEqual(x.device, x._variable.device)
          self.assertEqual(x.shape, ())
          self.assertEqual(x.get_shape(), ())

        if not tf.distribute.has_strategy():

            

Reported by Pylint.

Access to a protected member _variable of a client class
Error

Line: 187 Column: 38

                        self.assertEqual(evaluate(x.assign_add(2)), 10)
          self.assertEqual(evaluate(x.assign_sub(3)), 7)
          self.assertEqual(x.name, x._variable.name)
          self.assertEqual(x.device, x._variable.device)
          self.assertEqual(x.shape, ())
          self.assertEqual(x.get_shape(), ())

        if not tf.distribute.has_strategy():
          # Test scatter_* methods. These are not supported for

            

Reported by Pylint.

Access to a protected member _cast_dtype of a client class
Error

Line: 441 Column: 21

                    var_dtype = None
      def f():
        nonlocal var_dtype
        var_dtype = x._cast_dtype
      thread = threading.Thread(target=f)
      thread.start()
      thread.join()
      self.assertEqual(var_dtype, tf.float32)


            

Reported by Pylint.

keras/saving/hdf5_format.py
446 issues
Unable to import 'tensorflow.compat.v2'
Error

Line: 18 Column: 1

              # pylint: disable=protected-access
"""Functions for saving and loading a Keras Model from HDF5 format."""

import tensorflow.compat.v2 as tf

import json
import os

import numpy as np

            

Reported by Pylint.

Unable to import 'tensorflow.python.platform'
Error

Line: 32 Column: 1

              from keras.saving.saved_model import json_utils
from keras.utils.generic_utils import LazyLoader
from keras.utils.io_utils import ask_to_proceed_with_overwrite
from tensorflow.python.platform import tf_logging as logging


# pylint: disable=g-import-not-at-top
try:
  import h5py

            

Reported by Pylint.

Bad option value 'g-import-not-at-top'
Error

Line: 35 Column: 1

              from tensorflow.python.platform import tf_logging as logging


# pylint: disable=g-import-not-at-top
try:
  import h5py
  HDF5_OBJECT_HEADER_LIMIT = 64512
except ImportError:
  h5py = None

            

Reported by Pylint.

Bad option value 'g-import-not-at-top'
Error

Line: 41 Column: 1

                HDF5_OBJECT_HEADER_LIMIT = 64512
except ImportError:
  h5py = None
# pylint: enable=g-import-not-at-top

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
sequential_lib = LazyLoader(

            

Reported by Pylint.

Bad option value 'g-inconsistent-quotes'
Error

Line: 45 Column: 1

              
# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
sequential_lib = LazyLoader(
    "sequential_lib", globals(),
    "keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


            

Reported by Pylint.

Bad option value 'g-inconsistent-quotes'
Error

Line: 49 Column: 1

              sequential_lib = LazyLoader(
    "sequential_lib", globals(),
    "keras.engine.sequential")
# pylint:enable=g-inconsistent-quotes


def save_model_to_hdf5(model, filepath, overwrite=True, include_optimizer=True):
  """Saves a model to a HDF5 file.


            

Reported by Pylint.

Bad option value 'g-import-not-at-top'
Error

Line: 646 Column: 1

                    f: HDF5 group.
      model: Model instance.
  """
  from keras import __version__ as keras_version  # pylint: disable=g-import-not-at-top
  save_attributes_to_hdf5_group(
      f, 'layer_names', [layer.name.encode('utf8') for layer in model.layers])
  f.attrs['backend'] = backend.backend().encode('utf8')
  f.attrs['keras_version'] = str(keras_version).encode('utf8')


            

Reported by Pylint.

TODO(b/134426265): Switch back to single-quotes to match the rest of the file
Error

Line: 43 Column: 3

                h5py = None
# pylint: enable=g-import-not-at-top

# TODO(b/134426265): Switch back to single-quotes to match the rest of the file
# once the issue with copybara is fixed.
# pylint:disable=g-inconsistent-quotes
sequential_lib = LazyLoader(
    "sequential_lib", globals(),
    "keras.engine.sequential")

            

Reported by Pylint.

TODO(psv) Add warning when we save models that contain non-serializable
Error

Line: 82 Column: 3

                  raise ImportError('`save_model()` using h5 format requires h5py. Could not '
                      'import h5py.')

  # TODO(psv) Add warning when we save models that contain non-serializable
  # entities like metrics added using `add_metric` and losses added using
  # `add_loss.`
  if len(model.weights) != len(model._undeduplicated_weights):
    logging.warning('Found duplicated `Variable`s in Model\'s `weights`. '
                    'This is usually caused by `Variable`s being shared by '

            

Reported by Pylint.

TODO(b/128683857): Add integration tests between tf.keras and external
Error

Line: 122 Column: 3

                  model_weights_group = f.create_group('model_weights')
    save_weights_to_hdf5_group(model_weights_group, model)

    # TODO(b/128683857): Add integration tests between tf.keras and external
    # Keras, to avoid breaking TF.js users.
    if (include_optimizer and model.optimizer and
        not isinstance(model.optimizer, optimizer_v1.TFOptimizer)):
      save_optimizer_weights_to_hdf5_group(f, model.optimizer)


            

Reported by Pylint.

keras/keras_parameterized_test.py
440 issues
Unable to import 'tensorflow.compat.v2'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Tests for Keras testing_utils."""

import tensorflow.compat.v2 as tf

import unittest

from absl.testing import parameterized


            

Reported by Pylint.

Unable to import 'absl.testing'
Error

Line: 21 Column: 1

              
import unittest

from absl.testing import parameterized

import keras
from keras import keras_parameterized
from keras import testing_utils


            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 57 Column: 21

                  ])

    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)
    self.assertIsInstance(models[2], keras.models.Sequential)


            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 58 Column: 22

              
    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)
    self.assertIsInstance(models[2], keras.models.Sequential)

    ts = unittest.makeSuite(ExampleTest)

            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 106 Column: 21

                  ])

    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)
    self.assertIsInstance(models[2], keras.models.Sequential)


            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 107 Column: 22

              
    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)
    self.assertIsInstance(models[2], keras.models.Sequential)

    ts = unittest.makeSuite(ExampleTest)

            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 147 Column: 21

                  ])

    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)

    ts = unittest.makeSuite(ExampleTest)

            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 148 Column: 22

              
    # Validate that the models are what they should be
    self.assertTrue(models[0]._is_graph_network)
    self.assertFalse(models[1]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)
    self.assertNotIsInstance(models[1], keras.models.Sequential)

    ts = unittest.makeSuite(ExampleTest)
    res = unittest.TestResult()

            

Reported by Pylint.

Access to a protected member _is_graph_network of a client class
Error

Line: 187 Column: 22

                  ])

    # Validate that the models are what they should be
    self.assertFalse(models[0]._is_graph_network)
    self.assertNotIsInstance(models[0], keras.models.Sequential)

    ts = unittest.makeSuite(ExampleTest)
    res = unittest.TestResult()
    ts.run(res)

            

Reported by Pylint.

Unused argument 'arg'
Error

Line: 437 Column: 26

              
      @parameterized.named_parameters(dict(testcase_name="_arg",
                                           arg=True))
      def testBody(self, arg):
        mode = "eager" if tf.executing_eagerly() else "graph"
        should_run_eagerly = testing_utils.should_run_eagerly()
        l.append((mode, should_run_eagerly, testing_utils.get_model_type()))

    e = ExampleTest()

            

Reported by Pylint.

keras/utils/data_utils.py
439 issues
Bad option value 'g-import-not-at-top'
Error

Line: 16 Column: 1

              # See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# pylint: disable=g-import-not-at-top
"""Utilities for file download and caching."""

import tensorflow.compat.v2 as tf

from abc import abstractmethod

            

Reported by Pylint.

Unable to import 'tensorflow.compat.v2'
Error

Line: 19 Column: 1

              # pylint: disable=g-import-not-at-top
"""Utilities for file download and caching."""

import tensorflow.compat.v2 as tf

from abc import abstractmethod
from contextlib import closing
import functools
import hashlib

            

Reported by Pylint.

Unable to import 'tensorflow.python.util.tf_export'
Error

Line: 45 Column: 1

              from keras.utils import tf_inspect
from keras.utils.generic_utils import Progbar
from keras.utils.io_utils import path_to_string
from tensorflow.python.util.tf_export import keras_export

# Required to support google internal urlretrieve
if True:  # This gets transformed to `if sys.version_info[0] == 2:` in OSS.  # pylint: disable=using-constant-test

  def urlretrieve(url, filename, reporthook=None, data=None):

            

Reported by Pylint.

Bad option value 'g-importing-member'
Error

Line: 87 Column: 1

                    for chunk in chunk_read(response, reporthook=reporthook):
        fd.write(chunk)
else:
  from urllib.request import urlretrieve  # pylint: disable=g-importing-member


def is_generator_or_sequence(x):
  """Check if `x` is a Keras generator type."""
  builtin_iterators = (str, list, tuple, dict, set, frozenset)

            

Reported by Pylint.

Audit url open for permitted schemes. Allowing use of file:/ or custom schemes is often unexpected.
Security blacklist

Line: 82
Suggestion: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b310-urllib-urlopen

                      else:
          break

    response = urlopen(url, data)
    with open(filename, 'wb') as fd:
      for chunk in chunk_read(response, reporthook=reporthook):
        fd.write(chunk)
else:
  from urllib.request import urlretrieve  # pylint: disable=g-importing-member

            

Reported by Bandit.

Probable insecure usage of temp file/directory.
Security

Line: 220
Suggestion: https://bandit.readthedocs.io/en/latest/plugins/b108_hardcoded_tmp_directory.html

                  hash_algorithm = 'md5'
  datadir_base = os.path.expanduser(cache_dir)
  if not os.access(datadir_base, os.W_OK):
    datadir_base = os.path.join('/tmp', '.keras')
  datadir = os.path.join(datadir_base, cache_subdir)
  _makedirs_exist_ok(datadir)

  fname = path_to_string(fname)
  if not fname:

            

Reported by Bandit.

Audit url open for permitted schemes. Allowing use of file:/ or custom schemes is often unexpected.
Security blacklist

Line: 276
Suggestion: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b310-urllib-urlopen

                  error_msg = 'URL fetch failure on {}: {} -- {}'
    try:
      try:
        urlretrieve(origin, fpath, dl_progress)
      except urllib.error.HTTPError as e:
        raise Exception(error_msg.format(origin, e.code, e.msg))
      except urllib.error.URLError as e:
        raise Exception(error_msg.format(origin, e.errno, e.reason))
    except (Exception, KeyboardInterrupt) as e:

            

Reported by Bandit.

Consider explicitly re-raising using the 'from' keyword
Error

Line: 278 Column: 9

                    try:
        urlretrieve(origin, fpath, dl_progress)
      except urllib.error.HTTPError as e:
        raise Exception(error_msg.format(origin, e.code, e.msg))
      except urllib.error.URLError as e:
        raise Exception(error_msg.format(origin, e.errno, e.reason))
    except (Exception, KeyboardInterrupt) as e:
      if os.path.exists(fpath):
        os.remove(fpath)

            

Reported by Pylint.

Consider explicitly re-raising using the 'from' keyword
Error

Line: 280 Column: 9

                    except urllib.error.HTTPError as e:
        raise Exception(error_msg.format(origin, e.code, e.msg))
      except urllib.error.URLError as e:
        raise Exception(error_msg.format(origin, e.errno, e.reason))
    except (Exception, KeyboardInterrupt) as e:
      if os.path.exists(fpath):
        os.remove(fpath)
      raise
    ProgressTracker.progbar = None

            

Reported by Pylint.

Use of insecure MD2, MD4, MD5, or SHA1 hash function.
Security blacklist

Line: 311
Suggestion: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b303-md5

                  return hashlib.sha256()

  # This is used only for legacy purposes.
  return hashlib.md5()


def _hash_file(fpath, algorithm='sha256', chunk_size=65535):
  """Calculates a file sha256 or md5 hash.


            

Reported by Bandit.

keras/layers/preprocessing/index_lookup.py
434 issues
Bad option value 'g-classes-have-attributes'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Keras index lookup preprocessing layer."""

# pylint: disable=g-classes-have-attributes
# pylint: disable=g-direct-tensorflow-import

import collections

from keras import backend

            

Reported by Pylint.

Bad option value 'g-direct-tensorflow-import'
Error

Line: 18 Column: 1

              """Keras index lookup preprocessing layer."""

# pylint: disable=g-classes-have-attributes
# pylint: disable=g-direct-tensorflow-import

import collections

from keras import backend
from keras.engine import base_layer_utils

            

Reported by Pylint.

Unable to import 'tensorflow.compat.v2'
Error

Line: 31 Column: 1

              from keras.utils import layer_utils
from keras.utils import tf_utils
import numpy as np
import tensorflow.compat.v2 as tf
from tensorflow.python.platform import tf_logging as logging

INT = "int"
MULTI_HOT = "multi_hot"
ONE_HOT = "one_hot"

            

Reported by Pylint.

Unable to import 'tensorflow.python.platform'
Error

Line: 32 Column: 1

              from keras.utils import tf_utils
import numpy as np
import tensorflow.compat.v2 as tf
from tensorflow.python.platform import tf_logging as logging

INT = "int"
MULTI_HOT = "multi_hot"
ONE_HOT = "one_hot"
COUNT = "count"

            

Reported by Pylint.

Unnecessary pass statement
Error

Line: 69 Column: 5

              
  def initialize(self, table):
    """Returns the table initialization op."""
    pass


class VocabWeightHandler(base_layer_utils.TrackableWeightHandler):
  """Adds the vocabulary as a layer weight during serialization."""


            

Reported by Pylint.

__init__ method from base class 'TrackableWeightHandler' is not called
Error

Line: 75 Column: 3

              class VocabWeightHandler(base_layer_utils.TrackableWeightHandler):
  """Adds the vocabulary as a layer weight during serialization."""

  def __init__(self, lookup_layer):
    self._layer = lookup_layer
    self._dtype = lookup_layer.dtype
    self._distribute_strategy = tf.distribute.get_strategy()

  @property

            

Reported by Pylint.

Parameters differ from overridden 'compute_output_signature' method
Error

Line: 291 Column: 3

                    out_depth = self.vocabulary_size()
    return tf.TensorShape([input_shape[0], out_depth])

  def compute_output_signature(self, input_spec):
    output_shape = self.compute_output_shape(input_spec.shape.as_list())
    output_dtype = (
        self._value_dtype if self.output_mode == INT else backend.floatx())
    return tf.TensorSpec(shape=output_shape, dtype=output_dtype)


            

Reported by Pylint.

TODO(mattdangerw): for better performance we should rewrite this entire
Error

Line: 400 Column: 3

                        "any `tf.function`s and with eager execution enabled.".format(
              self.__class__.__name__, self.name))

    # TODO(mattdangerw): for better performance we should rewrite this entire
    # function to operate on tensors and convert vocabulary to a tensor here.
    if tf.is_tensor(vocabulary):
      vocabulary = self._tensor_vocab_to_numpy(vocabulary)
    elif isinstance(vocabulary, (list, tuple)):
      vocabulary = np.array(vocabulary)

            

Reported by Pylint.

Parameters differ from overridden 'call' method
Error

Line: 609 Column: 3

                    self.token_document_counts.remove(self.token_document_counts.export()[0])
      self.num_documents.assign(0)

  def call(self, inputs):
    self._maybe_freeze_vocab_size()

    inputs = self._standardize_inputs(inputs, self._key_dtype)
    original_shape = inputs.shape
    # Some ops will not handle scalar input, so uprank to rank 1.

            

Reported by Pylint.

TODO(b/190445202): remove output rank restriction.
Error

Line: 638 Column: 3

                    if lookups.shape[-1] != 1:
        lookups = self._expand_dims(lookups, -1)

    # TODO(b/190445202): remove output rank restriction.
    if lookups.shape.rank > 2:
      raise ValueError(
          "Received input shape {}, which would result in output rank {}. "
          "Currently only outputs up to rank 2 are supported for "
          "`output_mode={}`.".format(original_shape, lookups.shape.rank,

            

Reported by Pylint.

keras/layers/core/core_test.py
433 issues
Bad option value 'g-bad-import-order'
Error

Line: 16 Column: 1

              # limitations under the License.
# ==============================================================================
"""Tests for Keras core layers."""
# pylint: disable=g-bad-import-order
import tensorflow.compat.v2 as tf

import textwrap

import keras

            

Reported by Pylint.

Unable to import 'tensorflow.compat.v2'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Tests for Keras core layers."""
# pylint: disable=g-bad-import-order
import tensorflow.compat.v2 as tf

import textwrap

import keras
from keras import keras_parameterized

            

Reported by Pylint.

Module 'keras.initializers' has no 'RandomUniform' member
Error

Line: 520 Column: 28

              
    layer = keras.layers.Dense(
        5,
        kernel_initializer=keras.initializers.RandomUniform(),
        bias_initializer=keras.initializers.RandomUniform(),
        dtype='float32')
    dense_outputs = layer(dense_inputs)
    sparse_outpus = layer(sparse_inputs)
    ragged_outputs = layer(ragged_inputs)

            

Reported by Pylint.

Module 'keras.initializers' has no 'RandomUniform' member
Error

Line: 521 Column: 26

                  layer = keras.layers.Dense(
        5,
        kernel_initializer=keras.initializers.RandomUniform(),
        bias_initializer=keras.initializers.RandomUniform(),
        dtype='float32')
    dense_outputs = layer(dense_inputs)
    sparse_outpus = layer(sparse_inputs)
    ragged_outputs = layer(ragged_inputs)


            

Reported by Pylint.

Access to a protected member _output_shape of a client class
Error

Line: 213 Column: 25

              
    layer = keras.layers.deserialize({'class_name': 'Lambda', 'config': config})
    self.assertAllEqual(layer.function(1), 2)
    self.assertAllEqual(layer._output_shape, (1, 1))
    self.assertAllEqual(layer.mask(1, True), True)

    layer = keras.layers.Lambda.from_config(config)
    self.assertAllEqual(layer.function(1), 2)
    self.assertAllEqual(layer._output_shape, (1, 1))

            

Reported by Pylint.

Access to a protected member _output_shape of a client class
Error

Line: 218 Column: 25

              
    layer = keras.layers.Lambda.from_config(config)
    self.assertAllEqual(layer.function(1), 2)
    self.assertAllEqual(layer._output_shape, (1, 1))
    self.assertAllEqual(layer.mask(1, True), True)

  def test_lambda_with_training_arg(self):

    def fn(x, training=True):

            

Reported by Pylint.

Access to a protected member _keras_mask of a client class
Error

Line: 254 Column: 26

                  expected_mask[:, -1] = 0.0

    self.assertAllClose(self.evaluate(out), expected_out)
    self.assertIsNotNone(out._keras_mask)
    self.assertAllClose(self.evaluate(out._keras_mask), expected_mask)

  def test_lambda_with_ragged_input(self):

    def add_one(inputs):

            

Reported by Pylint.

Access to a protected member _keras_mask of a client class
Error

Line: 255 Column: 39

              
    self.assertAllClose(self.evaluate(out), expected_out)
    self.assertIsNotNone(out._keras_mask)
    self.assertAllClose(self.evaluate(out._keras_mask), expected_mask)

  def test_lambda_with_ragged_input(self):

    def add_one(inputs):
      return inputs + 1.0

            

Reported by Pylint.

Access to a protected member _warn of a client class
Error

Line: 363 Column: 5

                  def patched_warn(msg):
      raise ValueError(msg)

    layer._warn = patched_warn

    with self.assertRaisesRegex(ValueError, expected_warning):
      model = testing_utils.get_model_from_layers([layer], input_shape=(1,))
      model(tf.ones((4, 1)))


            

Reported by Pylint.

Access to a protected member _keras_mask of a client class
Error

Line: 381 Column: 26

                  x = np.ones((10, 10))
    y = keras.layers.Masking(1.)(x)
    self.assertTrue(hasattr(y, '_keras_mask'))
    self.assertIsNotNone(y._keras_mask)
    self.assertAllClose(self.evaluate(y._keras_mask), np.zeros((10,)))

  def test_compute_mask_with_positional_mask_arg(self):

    class MyLayer(keras.layers.Layer):

            

Reported by Pylint.

keras/layers/convolutional_recurrent.py
433 issues
Bad option value 'g-classes-have-attributes'
Error

Line: 16 Column: 1

              # limitations under the License.
# ==============================================================================
# pylint: disable=protected-access
# pylint: disable=g-classes-have-attributes
"""Convolutional-recurrent layers."""

import tensorflow.compat.v2 as tf

import numpy as np

            

Reported by Pylint.

Unable to import 'tensorflow.compat.v2'
Error

Line: 19 Column: 1

              # pylint: disable=g-classes-have-attributes
"""Convolutional-recurrent layers."""

import tensorflow.compat.v2 as tf

import numpy as np

from keras import activations
from keras import backend

            

Reported by Pylint.

Unable to import 'tensorflow.python.util.tf_export'
Error

Line: 35 Column: 1

              from keras.utils import conv_utils
from keras.utils import generic_utils
from keras.utils import tf_utils
from tensorflow.python.util.tf_export import keras_export


class ConvRNN(RNN):
  """N-Dimensional Base class for convolutional-recurrent layers.


            

Reported by Pylint.

Attribute 'kernel_shape' defined outside __init__
Error

Line: 529 Column: 5

                        'The channel dimension of the inputs (last axis) should be defined. '
          f'Found None. Full input shape received: input_shape={input_shape}')
    input_dim = input_shape[channel_axis]
    self.kernel_shape = self.kernel_size + (input_dim, self.filters * 4)
    recurrent_kernel_shape = self.kernel_size + (self.filters, self.filters * 4)

    self.kernel = self.add_weight(
        shape=self.kernel_shape,
        initializer=self.kernel_initializer,

            

Reported by Pylint.

Attribute 'kernel' defined outside __init__
Error

Line: 532 Column: 5

                  self.kernel_shape = self.kernel_size + (input_dim, self.filters * 4)
    recurrent_kernel_shape = self.kernel_size + (self.filters, self.filters * 4)

    self.kernel = self.add_weight(
        shape=self.kernel_shape,
        initializer=self.kernel_initializer,
        name='kernel',
        regularizer=self.kernel_regularizer,
        constraint=self.kernel_constraint)

            

Reported by Pylint.

Attribute 'recurrent_kernel' defined outside __init__
Error

Line: 538 Column: 5

                      name='kernel',
        regularizer=self.kernel_regularizer,
        constraint=self.kernel_constraint)
    self.recurrent_kernel = self.add_weight(
        shape=recurrent_kernel_shape,
        initializer=self.recurrent_initializer,
        name='recurrent_kernel',
        regularizer=self.recurrent_regularizer,
        constraint=self.recurrent_constraint)

            

Reported by Pylint.

Attribute 'bias' defined outside __init__
Error

Line: 556 Column: 7

                        ])
      else:
        bias_initializer = self.bias_initializer
      self.bias = self.add_weight(
          shape=(self.filters * 4,),
          name='bias',
          initializer=bias_initializer,
          regularizer=self.bias_regularizer,
          constraint=self.bias_constraint)

            

Reported by Pylint.

Attribute 'bias' defined outside __init__
Error

Line: 563 Column: 7

                        regularizer=self.bias_regularizer,
          constraint=self.bias_constraint)
    else:
      self.bias = None
    self.built = True

  def call(self, inputs, states, training=None):
    h_tm1 = states[0]  # previous memory state
    c_tm1 = states[1]  # previous carry state

            

Reported by Pylint.

Parameters differ from overridden 'call' method
Error

Line: 566 Column: 3

                    self.bias = None
    self.built = True

  def call(self, inputs, states, training=None):
    h_tm1 = states[0]  # previous memory state
    c_tm1 = states[1]  # previous carry state

    # dropout matrices for input units
    dp_mask = self.get_dropout_mask_for_cell(inputs, training, count=4)

            

Reported by Pylint.

Parameters differ from overridden 'call' method
Error

Line: 843 Column: 3

                      **kwargs)
    self.activity_regularizer = regularizers.get(activity_regularizer)

  def call(self, inputs, mask=None, training=None, initial_state=None):
    return super(ConvLSTM, self).call(
        inputs, mask=mask, training=training, initial_state=initial_state)

  @property
  def filters(self):

            

Reported by Pylint.

keras/engine/sequential_test.py
429 issues
Unable to import 'tensorflow.compat.v2'
Error

Line: 17 Column: 1

              # ==============================================================================
"""Tests specific to `Sequential` model."""

import tensorflow.compat.v2 as tf

from absl.testing import parameterized
import numpy as np

import keras

            

Reported by Pylint.

Unable to import 'absl.testing'
Error

Line: 19 Column: 1

              
import tensorflow.compat.v2 as tf

from absl.testing import parameterized
import numpy as np

import keras
from tensorflow.python.framework import test_util
from keras import keras_parameterized

            

Reported by Pylint.

Unable to import 'tensorflow.python.framework'
Error

Line: 23 Column: 1

              import numpy as np

import keras
from tensorflow.python.framework import test_util
from keras import keras_parameterized
from keras import testing_utils


class TestSequential(keras_parameterized.TestCase):

            

Reported by Pylint.

TODO(kaftan) This test fails w/ run_with_all_keras_modes. File ticket
Error

Line: 153 Column: 3

                  self.assertTrue(model.built)
    self.assertEqual(len(model.weights), 2 * 2)

  # TODO(kaftan) This test fails w/ run_with_all_keras_modes. File ticket
  @parameterized.parameters((True,), (False,))
  def test_training_and_eval_methods_on_symbolic_tensors(self, deferred):
    with tf.Graph().as_default(), self.cached_session():

      def get_model():

            

Reported by Pylint.

Parameters differ from overridden 'call' method
Error

Line: 369 Column: 7

              
    class MultiOutputLayer(keras.layers.Layer):

      def call(self, inputs):
        return inputs, inputs

    with self.assertRaisesRegex(ValueError,
                                'should have a single output tensor'):
      keras.Sequential([MultiOutputLayer(input_shape=(3,))])

            

Reported by Pylint.

Access to a protected member _flatten_layers of a client class
Error

Line: 407 Column: 14

                  layer = keras.layers.Dense(1)
    model = keras.Sequential([layer])
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],
        layer)

    model.a = [keras.layers.Dense(3)]  # should not be added to the layers list.
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],

            

Reported by Pylint.

Access to a protected member _flatten_layers of a client class
Error

Line: 412 Column: 14

              
    model.a = [keras.layers.Dense(3)]  # should not be added to the layers list.
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],
        layer)

    layer2 = keras.layers.Dense(2)
    model.add(layer2)
    self.assertEqual(

            

Reported by Pylint.

Access to a protected member _flatten_layers of a client class
Error

Line: 418 Column: 14

                  layer2 = keras.layers.Dense(2)
    model.add(layer2)
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],
        layer2)

    model.a = [keras.layers.Dense(3)]  # should not be added to the layers list.
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],

            

Reported by Pylint.

Access to a protected member _flatten_layers of a client class
Error

Line: 423 Column: 14

              
    model.a = [keras.layers.Dense(3)]  # should not be added to the layers list.
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],
        layer2)

    model.pop()
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],

            

Reported by Pylint.

Access to a protected member _flatten_layers of a client class
Error

Line: 428 Column: 14

              
    model.pop()
    self.assertEqual(
        list(model._flatten_layers(include_self=False, recursive=False))[-1],
        layer)

  def test_config_preserves_input_layer(self):
    model = keras.Sequential([
        keras.Input((None,), name='my_embedding_input', dtype='int32'),

            

Reported by Pylint.