from_params
allennlp.common.from_params
T#
T = TypeVar("T", bound="FromParams")
takes_arg#
def takes_arg(obj, arg: str) -> bool
Checks whether the provided obj takes a certain arg. If it's a class, we're really checking whether its constructor does. If it's a function or method, we're checking the object itself. Otherwise, we raise an error.
takes_kwargs#
def takes_kwargs(obj) -> bool
Checks whether a provided object takes in any positional arguments. Similar to takes_arg, we do this for both the init function of the class or a function / method Otherwise, we raise an error
can_construct_from_params#
def can_construct_from_params(type_: Type) -> bool
is_base_registrable#
def is_base_registrable(cls) -> bool
Checks whether this is a class that directly inherits from Registrable, or is a subclass of such a class.
remove_optional#
def remove_optional(annotation: type)
Optional[X] annotations are actually represented as Union[X, NoneType]. For our purposes, the "Optional" part is not interesting, so here we throw it away.
infer_params#
def infer_params(
cls: Type[T],
constructor: Union[Callable[..., T], Callable[[T], None]] = None
) -> Dict[str, Any]
create_kwargs#
def create_kwargs(
constructor: Callable[..., T],
cls: Type[T],
params: Params,
**extras
) -> Dict[str, Any]
Given some class, a Params
object, and potentially other keyword arguments,
create a dict of keyword args suitable for passing to the class's constructor.
The function does this by finding the class's constructor, matching the constructor
arguments to entries in the params
object, and instantiating values for the parameters
using the type annotation and possibly a from_params method.
Any values that are provided in the extras
will just be used as is.
For instance, you might provide an existing Vocabulary
this way.
create_extras#
def create_extras(
cls: Type[T],
extras: Dict[str, Any]
) -> Dict[str, Any]
Given a dictionary of extra arguments, returns a dictionary of kwargs that actually are a part of the signature of the cls.from_params (or cls) method.
pop_and_construct_arg#
def pop_and_construct_arg(
class_name: str,
argument_name: str,
annotation: Type,
default: Any,
params: Params,
**extras
) -> Any
Does the work of actually constructing an individual argument for
create_kwargs
.
Here we're in the inner loop of iterating over the parameters to a particular constructor,
trying to construct just one of them. The information we get for that parameter is its name,
its type annotation, and its default value; we also get the full set of Params
for
constructing the object (which we may mutate), and any extras
that the constructor might
need.
We take the type annotation and default value here separately, instead of using an
inspect.Parameter
object directly, so that we can handle Union
types using recursion on
this method, trying the different annotation types in the union in turn.
construct_arg#
def construct_arg(
class_name: str,
argument_name: str,
popped_params: Params,
annotation: Type,
default: Any,
**extras
) -> Any
The first two parameters here are only used for logging if we encounter an error.
FromParams#
class FromParams
Mixin to give a from_params method to classes. We create a distinct base class for this because sometimes we want non-Registrable classes to be instantiatable from_params.
from_params#
class FromParams:
| ...
| @classmethod
| def from_params(
| cls: Type[T],
| params: Params,
| constructor_to_call: Callable[..., T] = None,
| constructor_to_inspect: Union[Callable[..., T], Callable[[T], None]] = None,
| **extras
| ) -> T
This is the automatic implementation of from_params
. Any class that subclasses
FromParams
(or Registrable
, which itself subclasses FromParams
) gets this
implementation for free. If you want your class to be instantiated from params in the
"obvious" way -- pop off parameters and hand them to your constructor with the same names --
this provides that functionality.
If you need more complex logic in your from from_params
method, you'll have to implement
your own method that overrides this one.
The constructor_to_call
and constructor_to_inspect
arguments deal with a bit of
redirection that we do. We allow you to register particular @classmethods
on a class as
the constructor to use for a registered name. This lets you, e.g., have a single
Vocabulary
class that can be constructed in two different ways, with different names
registered to each constructor. In order to handle this, we need to know not just the class
we're trying to construct (cls
), but also what method we should inspect to find its
arguments (constructor_to_inspect
), and what method to call when we're done constructing
arguments (constructor_to_call
). These two methods are the same when you've used a
@classmethod
as your constructor, but they are different
when you use the default
constructor (because you inspect __init__
, but call cls()
).