Exemplo n.º 1
0
 def compute_value(
     cls, raw_value: Optional[Iterable[str]], *, address: Address
 ) -> Tuple[Requirement, ...]:
     value = super().compute_value(raw_value, address=address)
     invalid_type_error = InvalidFieldTypeException(
         address,
         cls.alias,
         value,
         expected_type="an iterable of pip-style requirement strings (e.g. a list)",
     )
     if isinstance(value, str) or not isinstance(value, collections.abc.Iterable):
         raise invalid_type_error
     result = []
     for v in value:
         # We allow passing a pre-parsed `Requirement`. This is intended for macros which might
         # have already parsed so that we can avoid parsing multiple times.
         if isinstance(v, Requirement):
             result.append(v)
         elif isinstance(v, str):
             try:
                 parsed = Requirement.parse(v)
             except Exception as e:
                 raise InvalidFieldException(
                     format_invalid_requirement_string_error(
                         v,
                         e,
                         description_of_origin=(
                             f"the '{cls.alias}' field for the target {address}"
                         ),
                     )
                 )
             result.append(parsed)
         else:
             raise invalid_type_error
     return tuple(result)
Exemplo n.º 2
0
 def compute_value(
     cls,
     raw_value: Optional[Union[Iterable[str], Dict[str, Iterable[str]]]],
     *,
     address: Address,
 ) -> Optional[Union[Tuple[str, ...], FrozenDict[str, Tuple[str, ...]]]]:
     value_or_default = super().compute_value(raw_value, address=address)
     invalid_field_type_exception = InvalidFieldTypeException(
         address,
         cls.alias,
         value_or_default,
         expected_type=
         ("either an iterable of strings or a dictionary of platforms to iterable of "
          "strings"),
     )
     if isinstance(value_or_default, dict):
         try:
             return FrozenDict({
                 platform: tuple(sorted(ensure_str_list(lib_names)))
                 for platform, lib_names in value_or_default.items()
             })
         except ValueError:
             raise invalid_field_type_exception
     try:
         ensure_str_list(value_or_default)
     except ValueError:
         raise invalid_field_type_exception
     return tuple(sorted(value_or_default))
Exemplo n.º 3
0
 def compute_value(cls, raw_value: Optional[Dict[str, Any]], *,
                   address: Address) -> Optional[Dict[str, Any]]:
     value_or_default = super().compute_value(raw_value, address=address)
     if value_or_default is not None and not isinstance(
             value_or_default, dict):
         raise InvalidFieldTypeException(address,
                                         cls.alias,
                                         value_or_default,
                                         expected_type="a dictionary")
     return value_or_default
Exemplo n.º 4
0
 def compute_value(cls, raw_value: Optional[str],
                   address: Address) -> Optional[ConsoleScript]:
     value = super().compute_value(raw_value, address)
     if value is None:
         return None
     if not isinstance(value, str):
         raise InvalidFieldTypeException(address,
                                         cls.alias,
                                         value,
                                         expected_type="a string")
     return ConsoleScript(value)
Exemplo n.º 5
0
 def compute_value(cls, raw_value: Optional[str],
                   address: Address) -> EntryPoint:
     value = super().compute_value(raw_value, address)
     if not isinstance(value, str):
         raise InvalidFieldTypeException(address,
                                         cls.alias,
                                         value,
                                         expected_type="a string")
     try:
         return EntryPoint.parse(value, provenance=f"for {address}")
     except ValueError as e:
         raise InvalidFieldException(str(e))
Exemplo n.º 6
0
 def compute_value(  # type: ignore[override]
         cls, raw_value: Optional[Union[str, HTTPSource]],
         address: Address) -> Optional[Union[str, HTTPSource]]:
     if raw_value is None or isinstance(raw_value, str):
         return super().compute_value(raw_value, address)
     elif not isinstance(raw_value, HTTPSource):
         raise InvalidFieldTypeException(
             address,
             cls.alias,
             raw_value,
             expected_type="a string or an `http_source` object",
         )
     return raw_value
Exemplo n.º 7
0
 def compute_value(cls, raw_value: Optional[Iterable[str]], *,
                   address: Address) -> Tuple[Requirement, ...]:
     value = super().compute_value(raw_value, address=address)
     invalid_type_error = InvalidFieldTypeException(
         address,
         cls.alias,
         value,
         expected_type=
         "an iterable of pip-style requirement strings (e.g. a list)",
     )
     if isinstance(value, (str, PythonRequirement)) or not isinstance(
             value, collections.abc.Iterable):
         raise invalid_type_error
     result = []
     for v in value:
         # We allow passing a pre-parsed `Requirement`. This is intended for macros which might
         # have already parsed so that we can avoid parsing multiple times.
         if isinstance(v, Requirement):
             result.append(v)
         elif isinstance(v, str):
             try:
                 parsed = Requirement.parse(v)
             except Exception as e:
                 raise InvalidFieldException(
                     format_invalid_requirement_string_error(
                         v,
                         e,
                         description_of_origin=
                         (f"the '{cls.alias}' field for the target {address}"
                          ),
                     ))
             result.append(parsed)
         elif isinstance(v, PythonRequirement):
             extra_suggestions = ""
             if v.repository:
                 extra_suggestions += (
                     f"\n\nInstead of setting 'repository={v.repository}`, add this to the "
                     "option `repos` in the `[python-repos]` options scope."
                 )
             warn_or_error(
                 removal_version="2.1.0.dev0",
                 deprecated_entity_description="Using `pants_requirement`",
                 hint=
                 (f"In the '{cls.alias}' field for {address}, use '{str(v.requirement)}' "
                  f"instead of 'pants_requirement('{str(v.requirement)}').{extra_suggestions}"
                  ),
             )
             result.append(v.requirement)
         else:
             raise invalid_type_error
     return tuple(result)
Exemplo n.º 8
0
 def compute_value(
     cls, raw_value: Optional[Union[str, Dict[str, str]]], *, address: Address
 ) -> Optional[Union[str, FrozenDict[str, str]]]:
     value_or_default = super().compute_value(raw_value, address=address)
     if value_or_default is None:
         return None
     if isinstance(value_or_default, str):
         return value_or_default
     if isinstance(value_or_default, dict):
         return FrozenDict(value_or_default)
     raise InvalidFieldTypeException(
         address,
         cls.alias,
         value_or_default,
         expected_type="either a string or a dictionary of executable name to local file name.",
     )
Exemplo n.º 9
0
 def compute_value(cls, raw_value: Optional[str],
                   address: Address) -> Optional[EntryPoint]:
     value = super().compute_value(raw_value, address)
     if value is None:
         return None
     if not isinstance(value, str):
         raise InvalidFieldTypeException(address,
                                         cls.alias,
                                         value,
                                         expected_type="a string")
     if value in {"<none>", "<None>"}:
         warn_or_error(
             "2.9.0.dev0",
             "using `<none>` for the `entry_point` field",
             ("Rather than setting `entry_point='<none>' for the pex_binary target "
              f"{address}, simply leave off the field."),
         )
         return None
     try:
         return EntryPoint.parse(value, provenance=f"for {address}")
     except ValueError as e:
         raise InvalidFieldException(str(e))