Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/tank/util/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def load_plugin(plugin_file, valid_base_class, alternate_base_classes=None):
try:
plugin_spec = importlib.util.spec_from_file_location(module_uid, plugin_file)
module = importlib.util.module_from_spec(plugin_spec)
plugin_spec.loader.exec_module(module)
sys.modules[module.__name__] = module
plugin_spec.loader.exec_module(module)
except Exception:
# log the full callstack to make sure that whatever the
# calling code is doing, this error is logged to help
Expand Down
49 changes: 49 additions & 0 deletions tests/fixtures/config/hooks/dataclass_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2026 Autodesk.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the ShotGrid Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the ShotGrid Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Autodesk.
"""Case where we check defining a Python dataclass won't fail during hook loading.

The dataclasses module check the `.sys.modules` for the ``Data`` class's ``__module__``
during type checking, so it should exist in there and not fatally error when loading
this hook.

This is a regression test for https://github.com/shotgunsoftware/tk-core/pull/1047
(https://github.com/shotgunsoftware/tk-core/commit/eaaadceafa4b449f819cf9c1b05cba229daaeeab)
"""

from __future__ import annotations

from dataclasses import dataclass

import sgtk


@dataclass(frozen=True)
class Data:
name: str | None
number: int | float
error: (
sgtk.authentication.AuthenticationError
| sgtk.authentication.IncompleteCredentials
| sgtk.authentication.AuthenticationCancelled
| sgtk.authentication.ConsoleLoginNotSupportedError
| None
) = None


class TestHook(sgtk.get_hook_baseclass()):
def execute(self):
return Data("foo", 123)

def second_method(self):
return Data(
None,
-3.1419,
error=sgtk.authentication.AuthenticationCancelled(),
)
14 changes: 14 additions & 0 deletions tests/platform_tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ def test_get_setting(self):
)


class TestDataclassHook(TestApplication):
"""
Test loading, executing and calling ``dataclass_hook``.
"""

def test_execute(self):
app = self.engine.apps["test_app"]
app.execute_hook_by_name("dataclass_hook")

def test_legacy_format(self):
app = self.engine.apps["test_app"]
app.execute_hook_expression("dataclass_hook", "second_method")


class TestExecuteHookByName(TestApplication):
"""
Tests execute_hook_by_name
Expand Down
Loading