(cls, func, /, *args, **keywords)
| 311 | return phcount, merger |
| 312 | |
| 313 | def _partial_new(cls, func, /, *args, **keywords): |
| 314 | if issubclass(cls, partial): |
| 315 | base_cls = partial |
| 316 | if not callable(func): |
| 317 | raise TypeError("the first argument must be callable") |
| 318 | else: |
| 319 | base_cls = partialmethod |
| 320 | # func could be a descriptor like classmethod which isn't callable |
| 321 | if not callable(func) and not hasattr(func, "__get__"): |
| 322 | raise TypeError(f"the first argument {func!r} must be a callable " |
| 323 | "or a descriptor") |
| 324 | if args and args[-1] is Placeholder: |
| 325 | raise TypeError("trailing Placeholders are not allowed") |
| 326 | for value in keywords.values(): |
| 327 | if value is Placeholder: |
| 328 | raise TypeError("Placeholder cannot be passed as a keyword argument") |
| 329 | if isinstance(func, base_cls): |
| 330 | pto_phcount = func._phcount |
| 331 | tot_args = func.args |
| 332 | if args: |
| 333 | tot_args += args |
| 334 | if pto_phcount: |
| 335 | # merge args with args of `func` which is `partial` |
| 336 | nargs = len(args) |
| 337 | if nargs < pto_phcount: |
| 338 | tot_args += (Placeholder,) * (pto_phcount - nargs) |
| 339 | tot_args = func._merger(tot_args) |
| 340 | if nargs > pto_phcount: |
| 341 | tot_args += args[pto_phcount:] |
| 342 | phcount, merger = _partial_prepare_merger(tot_args) |
| 343 | else: # works for both pto_phcount == 0 and != 0 |
| 344 | phcount, merger = pto_phcount, func._merger |
| 345 | keywords = {**func.keywords, **keywords} |
| 346 | func = func.func |
| 347 | else: |
| 348 | tot_args = args |
| 349 | phcount, merger = _partial_prepare_merger(tot_args) |
| 350 | |
| 351 | self = object.__new__(cls) |
| 352 | self.func = func |
| 353 | self.args = tot_args |
| 354 | self.keywords = keywords |
| 355 | self._phcount = phcount |
| 356 | self._merger = merger |
| 357 | return self |
| 358 | |
| 359 | def _partial_repr(self): |
| 360 | cls = type(self) |
nothing calls this directly
no test coverage detected
searching dependent graphs…