Check correct count for parameters of a generic cls (internal helper). This gives a nice error message in case of count mismatch.
(cls, arguments)
| 308 | |
| 309 | |
| 310 | def _check_generic_specialization(cls, arguments): |
| 311 | """Check correct count for parameters of a generic cls (internal helper). |
| 312 | |
| 313 | This gives a nice error message in case of count mismatch. |
| 314 | """ |
| 315 | expected_len = len(cls.__parameters__) |
| 316 | if not expected_len: |
| 317 | raise TypeError(f"{cls} is not a generic class") |
| 318 | actual_len = len(arguments) |
| 319 | if actual_len != expected_len: |
| 320 | # deal with defaults |
| 321 | if actual_len < expected_len: |
| 322 | # If the parameter at index `actual_len` in the parameters list |
| 323 | # has a default, then all parameters after it must also have |
| 324 | # one, because we validated as much in _collect_type_parameters(). |
| 325 | # That means that no error needs to be raised here, despite |
| 326 | # the number of arguments being passed not matching the number |
| 327 | # of parameters: all parameters that aren't explicitly |
| 328 | # specialized in this call are parameters with default values. |
| 329 | if cls.__parameters__[actual_len].has_default(): |
| 330 | return |
| 331 | |
| 332 | expected_len -= sum(p.has_default() for p in cls.__parameters__) |
| 333 | expect_val = f"at least {expected_len}" |
| 334 | else: |
| 335 | expect_val = expected_len |
| 336 | |
| 337 | raise TypeError(f"Too {'many' if actual_len > expected_len else 'few'} arguments" |
| 338 | f" for {cls}; actual {actual_len}, expected {expect_val}") |
| 339 | |
| 340 | |
| 341 | def _unpack_args(*args): |
no test coverage detected