Esempio n. 1
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,
        )
Esempio n. 2
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,
        )
Esempio n. 3
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,
        )
Esempio n. 4
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Produces a signature with the parameters provided at initialization.

        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`
        """
        return previous.replace(  # type: ignore
            parameters=self.parameters,
            __validate_parameters__=False,
        )
Esempio n. 5
0
    def revise(self, previous: FSignature) -> FSignature:
        """
        Applies the sorting :paramref:`~forge.returns.return_annotation`, to
        the input 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`
        """
        return previous.replace(  # type: ignore
            parameters=sorted(previous, key=self.sortkey),
            __validate_parameters__=False,
        )
Esempio n. 6
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,
        )