fix: fix edge case on forward unresolveable type#1673
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1673 +/- ##
==========================================
- Coverage 99.50% 99.40% -0.11%
==========================================
Files 23 23
Lines 5689 5692 +3
==========================================
- Hits 5661 5658 -3
- Misses 28 34 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
The one test failure is due to release of Python 3.15.0b2 and is fixed in PR #1674 |
|
I will rebase once that is landed. I found another edge case related to the function return type that also needs to be covered |
| hints = get_type_hints(func, include_extras=True) | ||
| hints = get_type_hints( | ||
| types.SimpleNamespace(__annotations__=relevant_annotations), | ||
| globalns=getattr(func, "__globals__", {}), |
There was a problem hiding this comment.
If func is wrapped by another decorator using @functools.wraps, its __globals__ attribute points to the decorator's module, not the original function's module. Because globalns is explicitly provided here, get_type_hints() won't traverse the __wrapped__ chain to find the correct globals. This will cause forward references (e.g., string annotations) to fail with NameError or resolve to the wrong type.
Suggested change:
ignored = {next(iter(sig.parameters), None), *skip_params}
ignored.discard(None)
relevant_annotations = {name: ann for name, ann in getattr(func, "__annotations__", {}).items() if name not in ignored}
unwrapped = inspect.unwrap(func)
try:
hints = get_type_hints(
types.SimpleNamespace(__annotations__=relevant_annotations),
globalns=getattr(unwrapped, "__globals__", {}),
include_extras=True,
)The key change is created unrwapped = inspect.unwrap(func) before the try and then using unwrapped instead of func in the call to getattr.
|
@KelvinChung2000 I merged the fix for Python 3.15.0b2. You are free to rebase when you see fit. |
Fix an edge case when doing forward declaration on types:
which happens when trying to declare the command in another file, while still wanting to keep the type hinting for
self.