Repository: ipython/ipython

Issue:
TypeError when using `store` magic command.
<!-- This is the repository for IPython command line, if you can try to make sure this question/bug/feature belong here and not on one of the Jupyter repositories. 

If it's a generic Python/Jupyter question, try other forums or discourse.jupyter.org.

If you are unsure, it's ok to post here, though, there are few maintainer so you might not get a fast response. 

-->
I'm using `ipython==8.17.2`, and I'm seeing the following traceback when running `%store -r objects_path` in a Jupyter notebook:

```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 get_ipython().run_line_magic('store', '-r objects_path')
      3 if "objects_path" not in dir():
      4     objects_path = './data/data_objects/'

File ~/.pyenv/versions/3.10.8/envs/my_env/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2454, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2452     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2453 with self.builtin_trap:
-> 2454     result = fn(*args, **kwargs)
   2456 # The code below prevents the output from being displayed
   2457 # when using magics with decorator @output_can_be_silenced
   2458 # when the last Python token in the expression is a ';'.
   2459 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/.pyenv/versions/3.10.8/envs/my_env/lib/python3.10/site-packages/IPython/extensions/storemagic.py:148, in StoreMagics.store(self, parameter_s)
    146 for arg in args:
    147     try:
--> 148         obj = db['autorestore/' + arg]
    149     except KeyError:
    150         try:

TypeError: 'PickleShareDB' object is not subscriptable
```

I'm now aware that I need to install the `pickleshare` library, but based on the changes in https://github.com/ipython/ipython/pull/14217/files, I'm wondering if a warning message should be raised instead - it would certainly be more informative.


Discussion:
Probably related, I got this error:

```
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/extensions.py", line 76, in load_extension
    return self._load_extension(module_str)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/extensions.py", line 91, in _load_extension
    mod = import_module(module_str)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'storemagic'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/shellapp.py", line 282, in init_extensions
    self.shell.extension_manager.load_extension(ext)
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/extensions.py", line 80, in load_extension
    return self._load_extension("IPython.extensions." + module_str)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/extensions.py", line 93, in _load_extension
    if self._call_load_ipython_extension(mod):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/extensions.py", line 145, in _call_load_ipython_extension
    mod.load_ipython_extension(self.shell)
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/extensions/storemagic.py", line 235, in load_ipython_extension
    ip.register_magics(StoreMagics)
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/core/magic.py", line 454, in register
    m = m(shell=self.shell)
        ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/extensions/storemagic.py", line 76, in __init__
    restore_data(self.shell)
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/extensions/storemagic.py", line 55, in restore_data
    refresh_variables(ip)
  File "/opt/homebrew/Cellar/ipython/8.18.1/libexec/lib/python3.12/site-packages/IPython/extensions/storemagic.py", line 37, in refresh_variables
    for key in db.keys('autorestore/*'):
               ^^^^^^^
AttributeError: 'PickleShareDB' object has no attribute 'keys'
UsageError: Line magic function `%store` not found.
```

I tried in three different environments, twice in different docker dev containers (Linux) and once on my local system (Mac).  It's always the same error. The version I've used is the current latest, 8.18.1. I've downgraded to 8.17.2 and it's still the same. For now I got rid of the error by commenting out this line in my iPython config:

```
c.StoreMagics.autorestore = True
```

Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -47,16 +47,29 @@ class PickleShareDB:  # type: ignore [no-redef]
         def __init__(self, path):
             pass
 
-        def get(self, key, default):
+        def get(self, key, default=None):
             warn(
-                f"using {key} requires you to install the `pickleshare` library.",
+                f"This is now an optional IPython functionality, using {key} requires you to install the `pickleshare` library.",
                 stacklevel=2,
             )
             return default
 
+        def __getitem__(self, key):
+            warn(
+                f"This is now an optional IPython functionality, using {key} requires you to install the `pickleshare` library.",
+                stacklevel=2,
+            )
+            return None
+
         def __setitem__(self, key, value):
             warn(
-                f"using {key} requires you to install the `pickleshare` library.",
+                f"This is now an optional IPython functionality, setting {key} requires you to install the `pickleshare` library.",
+                stacklevel=2,
+            )
+
+        def __delitem__(self, key):
+            warn(
+                f"This is now an optional IPython functionality, deleting {key} requires you to install the `pickleshare` library.",
                 stacklevel=2,
             )
 
diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py
--- a/IPython/extensions/storemagic.py
+++ b/IPython/extensions/storemagic.py
@@ -145,7 +145,7 @@ def store(self, parameter_s=''):
             if args:
                 for arg in args:
                     try:
-                        obj = db['autorestore/' + arg]
+                        obj = db["autorestore/" + arg]
                     except KeyError:
                         try:
                             restore_aliases(ip, alias=arg)


Test patch: