Rely on infogami-style setup to avoid duplicate setup calls#12046
Rely on infogami-style setup to avoid duplicate setup calls#12046RayBB merged 1 commit intointernetarchive:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Updates Open Library’s Infogami app initialization to avoid duplicate setup() execution by relying on module-import side effects (endpoint registration) instead of explicitly importing and calling plugin setup() functions.
Changes:
- Removes explicit
setupimports for several plugins fromopenlibrary/code.py. - Registers plugin endpoints by importing each plugin’s
codemodule insidesetup().
| import openlibrary.plugins.openlibrary.code # noqa: I001 registers endpoints | ||
| import openlibrary.plugins.worksearch.code | ||
| import openlibrary.plugins.inside.code | ||
| import openlibrary.plugins.books.code | ||
| import openlibrary.plugins.admin.code | ||
| import openlibrary.plugins.upstream.code |
There was a problem hiding this comment.
These imports are only for side effects, but as written they will trigger Ruff F401 (unused import) for each line (only the last one is suppressed). Consider switching to importlib.import_module(...) calls (keeps explicit ordering without isort interference and avoids F401), or add # noqa: F401 to each import (and optionally combine with the existing I001 suppression if you’re intentionally keeping a non-isort order).
| import openlibrary.plugins.openlibrary.code # noqa: I001 registers endpoints | |
| import openlibrary.plugins.worksearch.code | |
| import openlibrary.plugins.inside.code | |
| import openlibrary.plugins.books.code | |
| import openlibrary.plugins.admin.code | |
| import openlibrary.plugins.upstream.code | |
| import openlibrary.plugins.openlibrary.code # noqa: I001, F401 registers endpoints | |
| import openlibrary.plugins.worksearch.code # noqa: F401 registers endpoints | |
| import openlibrary.plugins.inside.code # noqa: F401 registers endpoints | |
| import openlibrary.plugins.books.code # noqa: F401 registers endpoints | |
| import openlibrary.plugins.admin.code # noqa: F401 registers endpoints | |
| import openlibrary.plugins.upstream.code # noqa: F401 registers endpoints |
There was a problem hiding this comment.
Unfortunately it won't let me do that. Not sure why. It gets reverted by (I think) ruff.
RayBB
left a comment
There was a problem hiding this comment.
Makes sense.
It's not ideal but needed for short term fixes.
Long term we'll naturally move away from these as we move towards fastapi.
Closes #12034
This isn't ideal, but this
setuppattern is used profusely and recursively throughout the codebase; including in infogami. To close the P0 and remove the risk of double-running sensitive internals, this seems like the quickest, simplest approach.Technical
In infogami we often use a pattern like this to define an endpoint:
Sub-classing
delegate.pageis enough to register the endpoint. But if the file is never imported, that sub-classing never happens! So the pattern that infogami/webpy use is to have a setup method in the bottom of every file that contains endpoints, like this:This is a problematic pattern, because it makes it incredibly easy to:
A better approach is to make it explicit, as #11762 does:
This decouples the setup from the import, but again still has the magic endpoint definition. And the new issue introduced is that we can now call these setup methods twice, resulting in undefined behaviour.
This is possibly also complicated by the
@publicdecorator, which also requires being called at the correct point in the infogami import life-cycle.Testing
No duplicate headers on testing or prod.
Screenshot
Stakeholders