Skip to content

Commit 9894ec1

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Support nullable types better
Summary: Without this, Static Python can't promote LOAD_ATTR to LOAD_FIELD for `T | None` types. Reviewed By: stroxler Differential Revision: D97328150 fbshipit-source-id: 20808cdfb1c5770e35233180bc0628483bf6b1e2
1 parent e9968ea commit 9894ec1

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

cinderx/PythonLib/cinderx/compiler/static/pyrefly_info.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ def lookup_type(
198198
# itself; use exact_type since a bare name like
199199
# `C` refers to the exact class, not a subclass.
200200
return resolved.exact_type()
201+
elif entry["qname"] in ("typing.Optional", "typing.Union"):
202+
# pyre-ignore[27]: tagged union data layout
203+
args = entry.get("args", [])
204+
resolved_args: list[Class] = []
205+
for arg_index in args:
206+
arg_val = self.lookup_type(arg_index, modules, type_env)
207+
if arg_val is None:
208+
resolved_args = []
209+
break
210+
resolved_args.append(arg_val.klass)
211+
if entry["qname"] == "typing.Optional" and resolved_args:
212+
none = self.resolve_classname("builtins.None", modules, type_env)
213+
if none is not None:
214+
resolved_args.append(none)
215+
if resolved_args:
216+
return type_env.get_union(tuple(resolved_args)).instance
201217

202218
return None
203219

0 commit comments

Comments
 (0)