Literal True for first/last options

This commit is contained in:
svlandeg 2023-07-06 12:26:17 +02:00
parent b615964be7
commit 915245ab8e
2 changed files with 13 additions and 8 deletions

View File

@ -979,6 +979,7 @@ class Errors(metaclass=ErrorsWithCodes):
E4007 = ("Span {var} {value} must be {op} Span {existing_var} "
"{existing_value}.")
E4008 = ("Span {pos}_char {value} does not correspond to a token {pos}.")
E4009 = ("The '{attr}' parameter should be 'None' or 'True', but found '{value}'.")
RENAMED_LANGUAGE_CODES = {"xx": "mul", "is": "isl"}

View File

@ -757,8 +757,8 @@ class Language:
*,
before: Optional[Union[str, int]] = None,
after: Optional[Union[str, int]] = None,
first: Optional[bool] = None,
last: Optional[bool] = None,
first: Optional[Literal[True]] = None,
last: Optional[Literal[True]] = None,
source: Optional["Language"] = None,
config: Dict[str, Any] = SimpleFrozenDict(),
raw_config: Optional[Config] = None,
@ -777,8 +777,8 @@ class Language:
component directly before.
after (Union[str, int]): Name or index of the component to insert new
component directly after.
first (bool): If True, insert component first in the pipeline.
last (bool): If True, insert component last in the pipeline.
first (True or None): If True, insert component first in the pipeline.
last (True or None): If True, insert component last in the pipeline.
source (Language): Optional loaded nlp object to copy the pipeline
component from.
config (Dict[str, Any]): Config parameters to use for this component.
@ -823,18 +823,22 @@ class Language:
self,
before: Optional[Union[str, int]] = None,
after: Optional[Union[str, int]] = None,
first: Optional[bool] = None,
last: Optional[bool] = None,
first: Optional[Literal[True]] = None,
last: Optional[Literal[True]] = None,
) -> int:
"""Determine where to insert a pipeline component based on the before/
after/first/last values.
before (str): Name or index of the component to insert directly before.
after (str): Name or index of component to insert directly after.
first (bool): If True, insert component first in the pipeline.
last (bool): If True, insert component last in the pipeline.
first (True or None): If True, insert component first in the pipeline.
last (True or None): If True, insert component last in the pipeline.
RETURNS (int): The index of the new pipeline component.
"""
if first is not None and first is not True:
raise ValueError(Errors.E4009.format(attr="first", value=first))
if last is not None and last is not True:
raise ValueError(Errors.E4009.format(attr="last", value=last))
all_args = {"before": before, "after": after, "first": first, "last": last}
if sum(arg is not None for arg in [before, after, first, last]) >= 2:
raise ValueError(