Skip to content

fix(python): support @typechecked decorator#8495

Open
pyranota wants to merge 3 commits intomainfrom
py-typechecked-decorator
Open

fix(python): support @typechecked decorator#8495
pyranota wants to merge 3 commits intomainfrom
py-typechecked-decorator

Conversation

@pyranota
Copy link
Copy Markdown
Contributor

WindmillFinder's ModuleSpec lacked origin, so file was never set on loaded modules. inspect.getfile() then raised "is a built-in module", breaking typeguard's @TypeChecked and anything else that introspects module source. Use spec_from_file_location() which sets origin correctly.

WindmillFinder's ModuleSpec lacked origin, so __file__ was never set on
loaded modules. inspect.getfile() then raised "is a built-in module",
breaking typeguard's @TypeChecked and anything else that introspects
module source. Use spec_from_file_location() which sets origin correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Mar 24, 2026

Deploying windmill with  Cloudflare Pages  Cloudflare Pages

Latest commit: 42eba98
Status: ✅  Deploy successful!
Preview URL: https://ca463de6.windmill.pages.dev
Branch Preview URL: https://py-typechecked-decorator.windmill.pages.dev

View logs

@pyranota pyranota marked this pull request as ready for review March 28, 2026 14:07
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Mar 28, 2026

Claude finished @pyranota's task in 1m 8s —— View job


PR Review: fix @TypeChecked decorator in Python relative imports

  • Read changed files and gather context
  • Review code quality, bugs, security, performance
  • Post review feedback

Summary

Clean, well-scoped fix. The root cause is correctly identified: ModuleSpec(name, SourceFileLoader(name, fullpath)) doesn't set spec.origin, so module.__file__ is never populated. spec_from_file_location(name, fullpath) sets origin (and has_location=True), which means inspect.getfile() works correctly — fixing @typechecked and any other decorator/tool that introspects module sources.

Code Review

loader.py changes — looks good. Both call sites (line 37: local cache hit, line 66: freshly downloaded) are updated consistently. spec_from_file_location internally creates a SourceFileLoader under the hood, so behavior is otherwise identical.

Minor nit: SourceFileLoader is still imported on line 4 but is no longer used directly. Consider removing it to keep the imports clean:

from importlib.machinery import ModuleSpec

Fix this →

Test (python_jobs.rs) — well structured. The test creates a helper script via SQL fixture that uses inspect.getfile() (the same mechanism typeguard uses), then imports it from another script. The assertion starts_with("Hello, World! from ") correctly validates that __file__ was set without being brittle about the exact path. Good approach.

Fixture (typechecked_python.sql) — clean and minimal. Hash 12349 doesn't collide with existing fixtures.

No Issues Found

The change is minimal, correct, and well-tested. No security, performance, or correctness concerns.

Testing Instructions

To verify this fix, create two Python scripts in a Windmill workspace. First, create a helper script at path f/system/typechecked_helper with content that uses inspect.getfile(sys.modules[__name__]) and returns the result (or uses @typechecked from typeguard). Then create a second script that does from f.system.typechecked_helper import greet and calls the imported function. Run the second script — it should succeed without raising "<module> is a built-in module" errors. Previously, inspect.getfile() would fail because __file__ was not set on dynamically loaded Windmill modules.

@pyranota pyranota changed the title fix: support @typechecked decorator in Python relative imports fix(python): support @typechecked decorator Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant