python / python/cpython

Passing wrong number of arguments to generic produces runtime errors

Open
#157,810 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-typing type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

In pypa/hatch#2437, we see a change to a generic type, adding an extra parameter, causing a run-time failure, and since this was used in a build plugin, it cause a build failure and my package to fail to install.

pdm.exceptions.BuildError: Build backend raised error: Showing the last 10 lines of the build output:
    class GitInfoBuildHook(BuildHookInterface[Any]):
                           ~~~~~~~~~~~~~~~~~~^^^^^
  File "/usr/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 1083, in _generic_class_getitem
    _check_generic(cls, params, len(cls.__parameters__))
  File "/tmp/pdm-build-env-4xoutxun-shared/lib/python3.12/site-packages/typing_extensions.py", line 3274, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments"
TypeError: Too few arguments for <class 'hatchling.builders.hooks.plugin.interface.BuildHookInterface'>; actual 1, expected at least 2

I find this behavior fairly surprising; I would only expect type errors in an annotation to cause errors when doing typechecking, not runtime errors like this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported generic-parameter failure through the typing.py path shown in the traceback, then compare the corresponding validation in typing_extensions.py. The issue is resolved when the intended behavior for incorrect generic argument counts is established and covered by a regression test without breaking valid runtime typing use.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.