Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structural pattern matching to handle linkcheck result statuses #13049

Conversation

jayaddison
Copy link
Contributor

@jayaddison jayaddison commented Oct 20, 2024

Feature or Bugfix

  • Refactoring (and a legacy code cleanup)

Purpose

  • The repeated [el]if...result.status == <status> conditions in part of the linkcheck code expose an intended behaviour: each status code implies that the code should follow one particular code path to handle the result. This seems like a good fit for a match clause, available from Python3.11+.

Detail

  • Handle linkcheck status codes using a match statement.
  • Also includes a tangential refactor of some logging code, to fit the resulting code within formatting guidelines while remaining aesthetically reasonable.
  • For review purposes, it could be helpful to ignore whitespace differences introduced by this changeset (w=1 query-string parameter, if using the GitHub web comparison interface).

Relates

This anticipates an additional level of indendation to be added for
the relevant code during the introduction of Structural Pattern
Matching.
Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we integrate the UNCHECKED and WORKING (old) checks above into the match? Ideally, as we use an enum, we shouldn't need to use case _ -- we just check every valid state.

A

Comment on lines -116 to -117
if result.status == _Status.WORKING and result.message == 'old':
return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting for future historians: the old message tag here was a way that the linkcheck component would communicate to itself that a hyperlink had already been checked and confirmed as working ('good').

That code was removed in 4351721, and I believe that the URI deduplication that was removed there is no longer necessary, because linkcheck nowadays collects hyperlinks in a Python dict, CheckExternalLinksBuilder.hyperlinks, keyed by the URI; so duplicates should not occur anyway.

@jayaddison
Copy link
Contributor Author

@AA-Turner changelog entry required, do you think?

@AA-Turner
Copy link
Member

There's no visible behaviour change to users so I shouldn't think one is required.

@AA-Turner AA-Turner changed the title linkcheck: use Structural Pattern Matching (match statement) to handle result statuses Use structural pattern matching to handle linkcheck result statuses Oct 20, 2024
@AA-Turner AA-Turner merged commit d3dd8e4 into sphinx-doc:master Oct 20, 2024
23 checks passed
@AA-Turner
Copy link
Member

Thank you @jayaddison!

A

@jayaddison
Copy link
Contributor Author

Brilliant! Thank you for the code reviews!

@jayaddison jayaddison deleted the refactor/3-of-3-linkcheck-linkstatus-pattern-matching branch October 21, 2024 00:11
@AA-Turner
Copy link
Member

It seems 'local' is currently unused, by the way.

@jayaddison
Copy link
Contributor Author

Mmm. As is the linkstat['text'] assignment.

For local -- perhaps that should become part of a separate link property that indicates whether each link is local or remote. I'll think about that and file a change suggestion / feature description at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants