Skip to content

Custom classes for extlinks handling #13544

Closed
@zerothi

Description

@zerothi

Is your feature request related to a problem? Please describe.
When using custom classes for the extlinks extension it would be nice to not rebuild on every build.

Describe the solution you'd like
Forcing no rebuild when using a class with proper hash functionality and pickling state.

Describe alternatives you've considered
There isn't really an alternative when you want custom handling of links.

So here is a somewhat minimal class:

class GHFormat:

    def __init__(self, type: Literal["issues", "discussions", "pull"]):
        self._type = type

    def is_valid(self, number: str) -> bool:
        try:
            return int(number) > 0
        except ValueError:
            return False

    def format(self, number: str) -> str:
        if not self.is_valid(number):
            # link to the type index
            return f"https://github.com/<user>/<project>/{self._type}"
        return f"https://github.com/<user>/<project>/{self._type}/{number}"

    def __mod__(self, arg) -> str:
        return self.format(arg)

    def __hash__(self) -> int: # trying to force the same env... didn't work
        return hash(self._type)

    def __setstate__(self, d): # for pickling
        self.__init__(d["type"])

    def __getstate__(self): # for pickling
        return {"type": self._type}

The class should of course be in a separate folder for import.

Then the conf file would look something like this:

extlinks = {
    "issue": (GHFormat("issues"), "GH %s"),
    ...
}

Even when I do multible builds the build keeps saying:

updating environment: [config changed ('extlinks')] ...

or something similar. But I have fixed the hash, and made it perfectly pickable, so I don't see why it doesn't accept the state?

Now, the typing of extlinks doesn't suggest this way of manipulating things, but I can see some nice utils for things like this. It could do many things, simply have a proper __mod__ operator.

I have tried to search for details on how sphinx tracks changes in the environment when building the docs, but I didn't find any details, guidance could perhaps guide me towards a solution that I could produce?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions