You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Promote implicit literals for module-level variables
Summary:
Module-level assignments like `timeout = 100` infer `Literal[100]`, but
protocols expect `timeout: int`. This caused the `protocols_modules.py`
conformance test to fail.
We fix this by promoting implicit literals at two use sites where
module-level variables are read from outside their defining scope:
1. Cross-barrier function reads: when a function reads a module-level
variable (across a flow barrier), we use `Binding::PromoteForward`
instead of `Binding::Forward`. This widens implicit literals to their
base types, so `def foo(): reveal_type(x)` gives `int` for `x = 42`.
The `is_module_scope` flag on `NameReadInfo::Anywhere` ensures we only
promote reads from module scope, not from enclosing functions.
2. Module exports: unannotated exports promote implicit literals so that
cross-module access like `import mod; mod.x` gives `int`.
Flow-sensitive reads at the same level are unchanged — `x = 42;
reveal_type(x)` still gives `Literal[42]`. ALL_CAPS names are excluded
from promotion as conventional constants. Annotated exports (including
`Final`) are unchanged since the annotation determines the type.
Reviewed By: rchen152
Differential Revision: D98803805
fbshipit-source-id: e7784ad9be3b7a8a32bfd280b31f6e1e2f273069
Copy file name to clipboardExpand all lines: conformance/third_party/conformance.exp
+1-12Lines changed: 1 addition & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -9655,22 +9655,11 @@
9655
9655
}
9656
9656
],
9657
9657
"protocols_modules.py": [
9658
-
{
9659
-
"code": -2,
9660
-
"column": 17,
9661
-
"concise_description": "`Module[_protocols_modules1]` is not assignable to `Options1`",
9662
-
"description": "`Module[_protocols_modules1]` is not assignable to `Options1`\n `Module[_protocols_modules1].timeout` has type `Literal[100]`, which is not consistent with `int` in `Options1.timeout` (the type of read-write attributes cannot be changed)",
9663
-
"line": 25,
9664
-
"name": "bad-assignment",
9665
-
"severity": "error",
9666
-
"stop_column": 36,
9667
-
"stop_line": 25
9668
-
},
9669
9658
{
9670
9659
"code": -2,
9671
9660
"column": 17,
9672
9661
"concise_description": "`Module[_protocols_modules1]` is not assignable to `Options2`",
9673
-
"description": "`Module[_protocols_modules1]` is not assignable to `Options2`\n `Module[_protocols_modules1].timeout` has type `Literal[100]`, which is not consistent with `str` in `Options2.timeout` (the type of read-write attributes cannot be changed)",
9662
+
"description": "`Module[_protocols_modules1]` is not assignable to `Options2`\n `Module[_protocols_modules1].timeout` has type `int`, which is not consistent with `str` in `Options2.timeout` (the type of read-write attributes cannot be changed)",
Copy file name to clipboardExpand all lines: conformance/third_party/conformance.result
+1-3Lines changed: 1 addition & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -147,9 +147,7 @@
147
147
"protocols_explicit.py": [],
148
148
"protocols_generic.py": [],
149
149
"protocols_merging.py": [],
150
-
"protocols_modules.py": [
151
-
"Line 25: Unexpected errors ['`Module[_protocols_modules1]` is not assignable to `Options1`\\n `Module[_protocols_modules1].timeout` has type `Literal[100]`, which is not consistent with `int` in `Options1.timeout` (the type of read-write attributes cannot be changed)']"
0 commit comments