Repository: python/mypy

Issue:
`__init_subclass__` with `classmethod` raises IndexError in stubtest
<!--
If you're not sure whether what you're experiencing is a mypy bug, please see the "Question and Help" form instead.
Please consider:

- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html
- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported
- asking on gitter chat: https://gitter.im/python/typing
-->

**Bug Report**

<!--
If you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues

If the project you encountered the issue in is open source, please provide a link to the project.
-->

Stubtest raises `IndexError: list index out of range` when a `__init_subclass__` method with the `classmethod` decorator is used.

**To Reproduce**

https://github.com/rayzchen/stubtest-bug

Run `python -m mypy.stubtest mod`

**Actual Behavior**

```
init subclass
Traceback (most recent call last):
  File "D:\RayChen\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\RayChen\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 1879, in <module>
    sys.exit(main())
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 1875, in main
    return test_stubs(parse_options(sys.argv[1:]))
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 1748, in test_stubs
    for error in test_module(module):
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 218, in test_module
    yield from verify(stub, runtime, [module_name])
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 396, in verify_mypyfile
    yield from verify(stub_entry, runtime_entry, object_path + [entry])
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 534, in verify_typeinfo
    yield from verify(stub_to_verify, runtime_attr, object_path + [entry])
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 1254, in verify_decorator
    yield from verify(func, runtime, object_path)
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 974, in verify_funcitem
    stub_sig = Signature.from_funcitem(stub)
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 730, in from_funcitem
    stub_args = maybe_strip_cls(stub.name, stub.arguments)
  File "D:\RayChen\Programs\Python\Python310\lib\site-packages\mypy\stubtest.py", line 674, in maybe_strip_cls
    if args[0].variable.name == "cls":
IndexError: list index out of range
```

**Your Environment**

<!-- Include as many relevant details about the environment you experienced the bug in -->

- Mypy version used: 1.3.0
- Mypy command-line flags: None
- Mypy configuration options from `mypy.ini` (and other config files): None
- Python version used: 3.10.6

<!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->



Discussion:

Resolving patch:
diff --git a/mypy/stubtest.py b/mypy/stubtest.py
--- a/mypy/stubtest.py
+++ b/mypy/stubtest.py
@@ -668,7 +668,7 @@ def _verify_arg_default_value(
 
 
 def maybe_strip_cls(name: str, args: list[nodes.Argument]) -> list[nodes.Argument]:
-    if name in ("__init_subclass__", "__class_getitem__"):
+    if args and name in ("__init_subclass__", "__class_getitem__"):
         # These are implicitly classmethods. If the stub chooses not to have @classmethod, we
         # should remove the cls argument
         if args[0].variable.name == "cls":


Test patch: