(factory, nargs: int)
| 184 | |
| 185 | |
| 186 | def _accepts_n_positional_args(factory, nargs: int) -> bool: |
| 187 | try: |
| 188 | signature = inspect.signature(factory) |
| 189 | parameters = tuple(signature.parameters.values()) |
| 190 | except (TypeError, ValueError): |
| 191 | try: |
| 192 | signature = inspect.signature(factory.__init__) |
| 193 | parameters = tuple(signature.parameters.values())[1:] |
| 194 | except (AttributeError, TypeError, ValueError): |
| 195 | if inspect.isclass(factory) and issubclass(factory, (Serializer, CythonSerializer)): |
| 196 | return nargs == 2 |
| 197 | raise TypeError(f"Unable to inspect serializer constructor for {factory!r}") |
| 198 | min_args = 0 |
| 199 | max_args = 0 |
| 200 | has_varargs = False |
| 201 | for parameter in parameters: |
| 202 | if parameter.kind in (inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD): |
| 203 | max_args += 1 |
| 204 | if parameter.default is inspect._empty: |
| 205 | min_args += 1 |
| 206 | elif parameter.kind == inspect.Parameter.VAR_POSITIONAL: |
| 207 | has_varargs = True |
| 208 | if nargs < min_args: |
| 209 | return False |
| 210 | if has_varargs: |
| 211 | return True |
| 212 | return nargs <= max_args |
| 213 | |
| 214 | |
| 215 | def _construct_serializer(serializer_factory, type_resolver, cls): |
no test coverage detected