Skip to content

Commit 1e01bc2

Browse files
martindemellometa-codesync[bot]
authored andcommitted
Resolve pyrefly types for literals.
Reviewed By: DinoV Differential Revision: D96212432 fbshipit-source-id: 7ec265cd970eb16924019da97cd7a9ad32ab63dc
1 parent 5b2dc7a commit 1e01bc2

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TypeInfo(TypedDict):
3434
class TypeTableEntry(TypedDict):
3535
kind: str
3636
qname: object
37+
promoted_type: TypeKind
3738

3839

3940
@dataclass(slots=True, eq=True)
@@ -128,7 +129,17 @@ def lookup(self, node: AST) -> str:
128129
return ""
129130
return self._type_to_str(type_index)
130131

131-
def lookup_class_qname(self, node: AST) -> str:
132+
def _get_type_qname(self, type_index: TypeKind) -> str:
133+
entry = self._type_table[type_index]
134+
if entry["kind"] == "class":
135+
# Ignore the generic args
136+
return str(entry["qname"])
137+
elif entry["kind"] == "literal":
138+
if "promoted_type" in entry:
139+
return self._get_type_qname(entry["promoted_type"])
140+
return ""
141+
142+
def lookup_typename(self, node: AST) -> str:
132143
"""Look up the qname for an AST node if its type is a simple class.
133144
134145
We treat generic classes as their unparametrised "base" version,
@@ -137,11 +148,7 @@ def lookup_class_qname(self, node: AST) -> str:
137148
type_index = self._lookup(node)
138149
if type_index is None:
139150
return ""
140-
entry = self._type_table[type_index]
141-
if entry["kind"] == "class":
142-
# Ignore the generic args
143-
return str(entry["qname"])
144-
return ""
151+
return self._get_type_qname(type_index)
145152

146153
@classmethod
147154
def load_json(cls, json_path: str) -> PyreflyTypeInfo:

cinderx/PythonLib/cinderx/compiler/static/pyrefly_type_binder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def __init__(
8585
def visit(self, node: AST, *args: object) -> NarrowingEffect | None:
8686
ret = super().visit(node, *args)
8787
if isinstance(node, ast.expr) and self._type_info is not None:
88-
# For now, we only try to get type information for classes,
89-
# disregarding their type parameters, and doing nothing if we see a
90-
# some other type_info kind like a callable.
91-
classname = self._type_info.lookup_class_qname(node)
88+
# For now, we only try to get type information for class instances
89+
# and literals, disregarding any type parameters, and doing nothing
90+
# if we see a some other type_info kind like a callable.
91+
classname = self._type_info.lookup_typename(node)
9292
if classname:
9393
resolved = _resolve_classname(classname, self.modules)
9494
if resolved is not None:

0 commit comments

Comments
 (0)