Exemplo n.º 1
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Translocates (moves) the :paramref:`~forge.insert.parameter` into a
        new position in the signature.

        No validation is performed on the updated :class:`~forge.FSignature`,
        allowing it to be used as an intermediate revision in the context of
        :class:`~forge.compose`.

        :param previous: the :class:`~forge.FSignature` to modify
        :returns: a modified instance of :class:`~forge.FSignature`
        """
        try:
            selected = next(findparam(previous, self.selector))
        except StopIteration:
            raise ValueError("No parameter matched selector '{}'".format(
                self.selector))

        if self.before:
            try:
                before = next(findparam(previous, self.before))
            except StopIteration:
                raise ValueError("No parameter matched selector '{}'".format(
                    self.before))

            parameters = []
            for param in previous:
                if param is before:
                    parameters.append(selected)
                elif param is selected:
                    continue
                parameters.append(param)
        elif self.after:
            try:
                after = next(findparam(previous, self.after))
            except StopIteration:
                raise ValueError("No parameter matched selector '{}'".format(
                    self.after))

            parameters = []
            for param in previous:
                if param is not selected:
                    parameters.append(param)
                if param is after:
                    parameters.append(selected)
        else:
            parameters = [param for param in previous if param is not selected]
            parameters.insert(self.index, selected)

        # https://github.com/python/mypy/issues/5156
        return previous.replace(  # type: ignore
            parameters=parameters,
            __validate_parameters__=False,
        )
Exemplo n.º 2
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Replaces a parameter that matches
        :paramref:`~forge.replace.selector`.

        No validation is performed on the updated :class:`~forge.FSignature`,
        allowing it to be used as an intermediate revision in the context of
        :class:`~forge.compose`.

        :param previous: the :class:`~forge.FSignature` to modify
        :returns: a modified instance of :class:`~forge.FSignature`
        """
        try:
            match = next(findparam(previous, self.selector))
        except StopIteration:
            raise ValueError("No parameter matched selector '{}'".format(
                self.selector))

        # https://github.com/python/mypy/issues/5156
        return previous.replace(  # type: ignore
            parameters=[
                self.parameter if param is match else param
                for param in previous
            ],
            __validate_parameters__=False,
        )
Exemplo n.º 3
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Revises one or more parameters that matches
        :paramref:`~forge.modify.selector`.

        No validation is performed on the updated :class:`~forge.FSignature`,
        allowing it to be used as an intermediate revision in the context of
        :class:`~forge.compose`.

        :param previous: the :class:`~forge.FSignature` to modify
        :returns: a modified instance of :class:`~forge.FSignature`
        """
        matched = list(findparam(previous, self.selector))
        if not matched:
            if self.raising:
                raise ValueError("No parameter matched selector '{}'".format(
                    self.selector))
            return previous

        if not self.multiple:
            del matched[1:]

        # https://github.com/python/mypy/issues/5156
        return previous.replace(  # type: ignore
            parameters=[
                param.replace(**self.updates) if param in matched else param
                for param in previous
            ],
            __validate_parameters__=False,
        )
Exemplo n.º 4
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Inserts the :paramref:`~forge.insert.insertion` into a signature.

        No validation is performed on the updated :class:`~forge.FSignature`,
        allowing it to be used as an intermediate revision in the context of
        :class:`~forge.compose`.

        :param previous: the :class:`~forge.FSignature` to modify
        :returns: a modified instance of :class:`~forge.FSignature`
        """
        pparams = list(previous)
        nparams = []
        if self.before:
            try:
                match = next(findparam(pparams, self.before))
            except StopIteration:
                raise ValueError("No parameter matched selector '{}'".format(
                    self.before))

            for param in pparams:
                if param is match:
                    nparams.extend(self.insertion)
                nparams.append(param)
        elif self.after:
            try:
                match = next(findparam(pparams, self.after))
            except StopIteration:
                raise ValueError("No parameter matched selector '{}'".format(
                    self.after))

            for param in previous:
                nparams.append(param)
                if param is match:
                    nparams.extend(self.insertion)
        else:
            nparams = pparams[:self.index] + \
                self.insertion + \
                pparams[self.index:]

        # https://github.com/python/mypy/issues/5156
        return previous.replace(  # type: ignore
            parameters=nparams,
            __validate_parameters__=False,
        )
Exemplo n.º 5
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Copies the signature of :paramref:`~forge.copy.callable`.
        If provided, only a subset of parameters are copied, as determiend by
        :paramref:`~forge.copy.include` and :paramref:`~forge.copy.exclude`.

        Unlike most subclasses of :class:`~forge.Revision`, validation is
        performed on the updated :class:`~forge.FSignature`.
        This is because :class:`~forge.copy` takes a :term:`callable` which
        is required by Python to have a valid signature, so it's impossible
        to return an invalid signature.

        :param previous: the :class:`~forge.FSignature` to modify
        :returns: a modified instance of :class:`~forge.FSignature`
        """
        if self.include:
            return self.signature.replace(
                parameters=list(findparam(self.signature, self.include)))
        elif self.exclude:
            excluded = list(findparam(self.signature, self.exclude))
            return self.signature.replace(parameters=[
                param for param in self.signature if param not in excluded
            ])
        return self.signature