AttributeError: 'Namespace' object has no attribute '_flags' when running "jupyter kernelspec list --json"

I can list kernels using the command jupyter kernelspec list. However, if I use the command jupyter kernelspec list --json, the following error occurs:

Traceback (most recent call last):
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/bin/jupyter-kernelspec", line 8, in <module>
    sys.exit(KernelSpecApp.launch_instance())
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 845, in launch_instance
    app.initialize(argv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 334, in initialize
    self.parse_command_line(argv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 685, in parse_command_line
    return self.initialize_subcommand(subc, subargv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 604, in initialize_subcommand
    self.subapp.initialize(argv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/jupyter_core/application.py", line 235, in initialize
    self.parse_command_line(argv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
    return method(app, *args, **kwargs)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/application.py", line 709, in parse_command_line
    self.cli_config = deepcopy(loader.load_config())
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/loader.py", line 851, in load_config
    self._parse_args(argv)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/loader.py", line 908, in _parse_args
    self.parsed_data = self.parser.parse_args(to_parse)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py", line 1822, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/loader.py", line 768, in parse_known_args
    return super().parse_known_args(args, namespace)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py", line 1850, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py", line 2064, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py", line 2004, in consume_optional
    take_action(action, args, option_string)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/argparse.py", line 1932, in take_action
    action(self, namespace, argument_values, option_string)
  File "/usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets/config/loader.py", line 933, in __call__
    namespace._flags.append(self.flag)
AttributeError: 'Namespace' object has no attribute '_flags'

This was fixed in traitlets 5.1.1: FIX: account for changes in py310 by tacaswell · Pull Request #678 · ipython/traitlets · GitHub

try upgrading traitlets: pip install 'traitlets>=5.1.1'

I have updated traitlets.

Requirement already satisfied: traitlets>=4.1.0 in /usr/local/lib/python3.9/site-packages (from ipykernel->jupyter) (5.1.1)

The same error still occurs.

There are two versions of traitlets. One is in /usr/local/lib/python3.9/site-packages/traitlets-5.1.1.dist-info. The other one is in /usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets-5.1.0.dist-info. It seems that the Jupyter is using traitlets located in /usr/local/Cellar/jupyterlab/3.1.14_1/libexec/lib/python3.9/site-packages/traitlets-5.1.0.dist-info.

I updated the traitlets located inside jupyterlab. It works now.