MCPcopy Index your code
hub / github.com/python/cpython / _check_generic_specialization

Function _check_generic_specialization

Lib/typing.py:332–360  ·  view source on GitHub ↗

Check correct count for parameters of a generic cls (internal helper). This gives a nice error message in case of count mismatch.

(cls, arguments)

Source from the content-addressed store, hash-verified

330
331
332def _check_generic_specialization(cls, arguments):
333 """Check correct count for parameters of a generic cls (internal helper).
334
335 This gives a nice error message in case of count mismatch.
336 """
337 expected_len = len(cls.__parameters__)
338 if not expected_len:
339 raise TypeError(f"{cls} is not a generic class")
340 actual_len = len(arguments)
341 if actual_len != expected_len:
342 # deal with defaults
343 if actual_len < expected_len:
344 # If the parameter at index `actual_len` in the parameters list
345 # has a default, then all parameters after it must also have
346 # one, because we validated as much in _collect_type_parameters().
347 # That means that no error needs to be raised here, despite
348 # the number of arguments being passed not matching the number
349 # of parameters: all parameters that aren't explicitly
350 # specialized in this call are parameters with default values.
351 if cls.__parameters__[actual_len].has_default():
352 return
353
354 expected_len -= sum(p.has_default() for p in cls.__parameters__)
355 expect_val = f"at least {expected_len}"
356 else:
357 expect_val = expected_len
358
359 raise TypeError(f"Too {'many' if actual_len > expected_len else 'few'} arguments"
360 f" for {cls}; actual {actual_len}, expected {expect_val}")
361
362
363def _unpack_args(*args):

Callers 1

_generic_class_getitemFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…