MCPcopy Create free account
hub / github.com/NVIDIA/DALI / accepted_arg_count

Function accepted_arg_count

dali/python/nvidia/dali/_utils/external_source_impl.py:236–281  ·  view source on GitHub ↗

Checks the number of accepted arguments by the callable, and validates if it is either 0 or 1 positional argument allowed by the external source. Raises ------ TypeError Indicates that the `source` callable accepts wrong number of type of arguments.

(callable)

Source from the content-addressed store, hash-verified

234
235
236def accepted_arg_count(callable):
237 """Checks the number of accepted arguments by the callable, and validates if it is either
238 0 or 1 positional argument allowed by the external source.
239
240 Raises
241 ------
242 TypeError
243 Indicates that the `source` callable accepts wrong number of type of arguments.
244 """
245
246 if not (inspect.isfunction(callable) or inspect.ismethod(callable)) and hasattr(
247 callable, "__call__"
248 ):
249 callable = callable.__call__
250 # Extracting the `__call__` for a method causes the signature to report `self` as a parameter,
251 # so we have to subtract it.
252 if not inspect.ismethod(callable):
253 implicit_args = 0
254 else:
255 implicit_args = 1
256 callable = callable.__func__
257 signature = inspect.signature(callable)
258 # TODO(klecki): Do we mention that callable is one of the alternatives?
259 error_msg = (
260 "The `source` callable must accept either 0 or 1 positional arguments to indicate "
261 "whether it accepts the batch or sample indexing information."
262 )
263 for p in signature.parameters.values():
264 if p.kind == inspect.Parameter.VAR_POSITIONAL:
265 raise TypeError(
266 error_msg + f" Found var-positional argument `*{p.name}` which is not allowed."
267 )
268 if p.kind == inspect.Parameter.VAR_KEYWORD:
269 raise TypeError(
270 error_msg + f" Found var-keyword argument `**{p.name}` which is not allowed."
271 )
272 if p.kind == inspect.Parameter.KEYWORD_ONLY:
273 raise TypeError(
274 error_msg + f" Found keyword-only argument `{p.name}` which is not allowed."
275 )
276 result = len(signature.parameters) - implicit_args
277 if result not in [0, 1]:
278 raise TypeError(
279 error_msg + " Found more than one positional argument, which is not allowed."
280 )
281 return result
282
283
284def get_callback_from_source(source, cycle, batch_info=False):

Callers 2

test_accepted_arg_countFunction · 0.90
get_callback_from_sourceFunction · 0.85

Calls 1

valuesMethod · 0.80

Tested by 1

test_accepted_arg_countFunction · 0.72