guess_type: if the type is optional, treat it like it's "not None" (#3839)

* guess_type: if the type is optional, treat it like it's "not None"

When guessing the type for an Optional annotation, the Optional was already
being stripped off, but this value was being ignored, except for error
messages. So actually use the Optional-stripped value.

* Strip Optional when casting Var .to
This commit is contained in:
Masen Furer 2024-08-26 16:26:17 -07:00 committed by Khaleel Al-Adhami
parent c926c8e4eb
commit 3772fb2f2d

View File

@ -362,7 +362,11 @@ class ImmutableVar(Var, Generic[VAR_TYPE]):
from .object import ObjectVar, ToObjectOperation
from .sequence import ArrayVar, StringVar, ToArrayOperation, ToStringOperation
fixed_type = get_origin(var_type) or var_type
base_type = var_type
if types.is_optional(base_type):
base_type = types.get_args(base_type)[0]
fixed_type = get_origin(base_type) or base_type
fixed_output_type = get_origin(output) or output