Fix Python 3.14 compatibility for typing.Union annotations

In Python 3.14, typing.Union changed its repr from 'typing.Union[X, Y]'
to 'X | Y' (PEP 604), breaking annotation inference.

Changes:
- Use getattr() instead of safe_getattr() for __module__ retrieval
  (getattr_static fails on Union types in Python 3.14)
- Add fallback to typing.get_origin() when regex fails to match
- Normalize Union display back to 'Union[X, Y]' format for consistency
- Update test expectations for invalid annotation edge case in 3.14

Fixes: https://github.com/davidhalter/jedi/issues/2064
This commit is contained in:
Lumir Balhar
2026-03-09 15:38:12 +01:00
parent 76c1e03f07
commit 9b24443787
2 changed files with 34 additions and 7 deletions
+1 -1
View File
@@ -664,7 +664,7 @@ def bar():
({'return': 'typing.Union["str", int]'},
['int', 'str'] if sys.version_info >= (3, 9) else ['int'], ''),
({'return': 'typing.Union["str", 1]'},
['str'] if sys.version_info >= (3, 11) else [], ''),
[] if sys.version_info >= (3, 14) else (['str'] if sys.version_info >= (3, 11) else []), ''),
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
({'return': 'typing.Optional[str, int]'}, [], ''), # Takes only one arg
({'return': 'typing.Any'},