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

* 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 GitHub
parent ea15b184c0
commit 1d9a154d5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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