Skip to content

Fixed crash in protocol check when subject method has no positional params#551

Merged
agronholm merged 4 commits intoagronholm:masterfrom
HackedRico:fix-empty-list-error
May 1, 2026
Merged

Fixed crash in protocol check when subject method has no positional params#551
agronholm merged 4 commits intoagronholm:masterfrom
HackedRico:fix-empty-list-error

Conversation

@HackedRico
Copy link
Copy Markdown
Contributor

Changes

Fixes #550

check_signature_compatible in src/typeguard/_checkers.py calls .pop(0) on protocol_args and subject_args to strip the implicit self, but does so unconditionally whenever the corresponding side is classified as "instance". If the subject method's inspect.signature(...) reports zero positional-or-keyword parameters and no *args (e.g. a callable instance assigned at class scope, a functools.partial with all positional args bound, or a method proxied by a code-generation framework), subject_args is empty and pop(0) raises IndexError instead of letting the function fall through to its existing zip_longest comparison and emit a TypeCheckError.

This guards each pop with a non-empty check. After the change, the previously-crashing input raises TypeCheckError("... has too few positional arguments") along the same path as the equivalent N → N-1 mismatch with one more arg, restoring check_type's documented contract that callers can catch via except TypeCheckError.

Regression test added under tests/test_checkers.py::TestProtocol::test_subject_method_no_positional_params.

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) added which would fail without your patch
  • You've updated the documentation (in docs/, in case of behavior changes or new
    features)
  • You've added a new changelog entry (in docs/versionhistory.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

  • Fixed crash in protocol check when subject method has no positional params (#550 <https://github.com/agronholm/typeguard/issues/550>_; PR by @HackedRico)

@agronholm
Copy link
Copy Markdown
Owner

You checked the checkbox for adding a new changelog entry when you clearly haven't done so. What gives?

@coveralls
Copy link
Copy Markdown

coveralls commented May 1, 2026

Coverage Status

coverage: 94.911%. remained the same — HackedRico:fix-empty-list-error into agronholm:master

@HackedRico
Copy link
Copy Markdown
Contributor Author

Fixed

@agronholm agronholm merged commit eb77bac into agronholm:master May 1, 2026
11 checks passed
@agronholm
Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexError: pop from empty list in check_signature_compatible when subject method has zero positional params

3 participants