Repository navigation
scripts: fix crash when loading addon scripts that define a dataclass - #8293
Open
gaurav0107 wants to merge 2 commits into
Open
gaurav0107 wants to merge 2 commits into
gaurav0107 wants to merge 2 commits into
Conversation
gaurav0107
marked this pull request as ready for review
June 27, 2026 17:54
Register the script module in sys.modules before executing it, as recommended by the importlib docs. dataclasses (and other standard library machinery such as pickling and typing.get_type_hints) look the defining module up in sys.modules by its __module__ name while the module body runs; without the module being registered, a script that defines a @DataClass fails to load. Remove the module again if execution fails so a broken load leaves nothing registered. Closes mitmproxy#8279
gaurav0107
force-pushed
the
fix/8279-can-t-use-dataclass-in-the-script
branch
from
July 23, 2026 19:42
24df0e0 to
305aad3
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Loading an addon script that defines a
@dataclasscurrently crashes:The root cause is in
mitmproxy.addons.script.load_script: it builds themodule with
importlib.util.module_from_spec(spec)and then callsloader.exec_module(m)without first registering the module insys.modules. Python's import documentation recommends inserting the moduleinto
sys.modulesbefore executing it, because parts of the standard library(
dataclasses, pickling,typing.get_type_hints, ...) look the definingmodule up in
sys.modulesby its__module__name while the class body runs.When the entry is missing the lookup returns
Noneand the decorator raises.This registers the module in
sys.modulesbefore execution and removes itagain if execution fails, so a failed load still leaves nothing behind. The
existing
sys.modules.pop(fullname, None)at the top ofload_scriptkeepsguaranteeing uniqueness across reloads.
Closes #8279
Checklist
Summary
sys.modulesbeforeexec_module, andunregister on failure.
test_load_script_with_dataclass) and adataclass.pyaddon fixture that fails to load without the fix.