Implement new-style unions with TypeVars

This commit is contained in:
Dave Halter
2026-05-01 21:01:27 +02:00
parent 8ba5b67622
commit 55e5f0cb92
2 changed files with 10 additions and 1 deletions
+2 -1
View File
@@ -31,6 +31,7 @@ from jedi.inference.context import CompForContext
from jedi.inference.value.decorator import Decoratee
from jedi.plugins import plugin_manager
from jedi.inference.gradual.typing import ProxyTypingValue, IGNORE_ANNOTATION_PARTS
from jedi.inference.gradual.type_var import TypeVar
operator_to_magic_method = {
'+': '__add__',
@@ -530,7 +531,7 @@ def _infer_comparison(context, left_values, operator, right_values):
result = (left_values or NO_VALUES) | (right_values or NO_VALUES)
return _literals_to_types(state, result)
elif operator_str == "|" and all(
value.is_class() or value.is_compiled()
value.is_class() or value.is_compiled() or isinstance(value, TypeVar)
for value in itertools.chain(left_values, right_values)
):
# ^^^ A naive hack for PEP 604
@@ -387,3 +387,11 @@ first(custom_partial2_unbound_instance)
#? str()
values(custom_partial2_unbound_instance)[0]
def generic_func1(arg: T) -> int | str | T: pass
def generic_func2(arg: T) -> Union[int, str, T]: pass
#? int() str() bytes()
generic_func1(b"hello")
#? int() str() bytes()
generic_func2(b"hello")