Repository: iterative/shtab

Issue:
AssertionError from assert isinstance(sub.choices, dict)
Hello,
here is a valid example parser:

```python
#!/usr/bin/env python
import argparse

def parser():
        p = argparse.ArgumentParser()
        p.add_argument("arg1", choices=("foo", "bar"))
        return p


if __name__ == "__main__":
        p = parser()
        args = p.parse_args()
        print(args)
```

shtab fails because it expected another choices type (btw lists instead of tuple don't work neither).

I got it working with the following fix:

```diff
diff --git a/shtab/__init__.py b/shtab/__init__.py
index 4543999..d9d5117 100644
--- a/shtab/__init__.py
+++ b/shtab/__init__.py
@@ -13,7 +13,7 @@ from argparse import (
     _VersionAction,
 )
 from functools import total_ordering
-
+from collections.abc import Sequence, MappingView
 
 def get_version_dist(name=__name__):
     from pkg_resources import DistributionNotFound, get_distribution
@@ -204,8 +204,10 @@ def get_bash_commands(root_parser, root_prefix, choice_functions=None):
                             ),
                             file=fd,
                         )
+                    elif isinstance(sub.choices, (MappingView, Sequence)):
+                        commands.append(cmd)
                     else:
-                        assert isinstance(sub.choices, dict)
+                        assert isinstance(sub.choices, dict), f"expected choices dict, got {cmd} from {sub.choices!r}"
                         log.debug("subcommand:%s", cmd)
                         if sub.choices[cmd].add_help:
                             commands.append(cmd)
```

Is this the correct solution to the problem?

Discussion:

Patch:
diff --git a/shtab/__init__.py b/shtab/__init__.py
index 4543999..a0125ec 100644
--- a/shtab/__init__.py
+++ b/shtab/__init__.py
@@ -204,8 +204,7 @@ def get_bash_commands(root_parser, root_prefix, choice_functions=None):
                             ),
                             file=fd,
                         )
-                    else:
-                        assert isinstance(sub.choices, dict)
+                    elif isinstance(sub.choices, dict):
                         log.debug("subcommand:%s", cmd)
                         if sub.choices[cmd].add_help:
                             commands.append(cmd)
@@ -215,6 +214,8 @@ def get_bash_commands(root_parser, root_prefix, choice_functions=None):
                             )
                         else:
                             log.debug("skip:subcommand:%s", cmd)
+                    else:
+                        commands.append(cmd)
             else:
                 log.debug("uncompletable:{}:{}".format(prefix, sub.dest))
 


Test patch:
diff --git a/tests/test_shtab.py b/tests/test_shtab.py
index 464b73e..bd9c938 100644
--- a/tests/test_shtab.py
+++ b/tests/test_shtab.py
@@ -6,6 +6,7 @@ Currently runnable via nosetests, e.g.:
 """
 import logging
 import subprocess
+from argparse import ArgumentParser
 
 import pytest
 
@@ -69,6 +70,14 @@ def test_choices():
     assert "" not in shtab.Required.FILE
 
 
+@pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
+def test_main(shell, caplog):
+    with caplog.at_level(logging.INFO):
+        main(["-s", shell, "shtab.main.get_main_parser"])
+
+    assert not caplog.record_tuples
+
+
 @pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
 def test_complete(shell, caplog):
     parser = get_main_parser()
@@ -84,8 +93,17 @@ def test_complete(shell, caplog):
 
 
 @pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
-def test_main(shell, caplog):
+def test_positional_choices(shell, caplog):
+    parser = ArgumentParser(prog="test")
+    parser.add_argument("posA", choices=["one", "two"])
     with caplog.at_level(logging.INFO):
-        main(["-s", shell, "shtab.main.get_main_parser"])
+        completion = shtab.complete(parser, shell=shell)
+    print(completion)
+
+    if shell == "bash":
+        shell = Bash(completion)
+        # ideally should check completion
+        # https://github.com/iterative/shtab/issues/11
+        shell.compgen('-W "$_shtab_test_options_"', "", "-h --help")
 
     assert not caplog.record_tuples