-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Treat module-level assignments where the RHS is a type as equivalent to class declarations. Type aliases stopped working when we decided not to store inferred types for top-level variables, due to corner cases with `global` later mutating those variables (see facebookincubator/cinder#116 for discussion). However, in this specific case, it should be safe to treat the aliases as declared rather than inferred, since their values will be used by type annotations before we run any functions that could modify them via `global`. Reviewed By: alexmalyshev Differential Revision: D69254419 fbshipit-source-id: d29a268ed15a035dbc1a3f4d1a2eb32280637e45
- Loading branch information
1 parent
fe9adb5
commit 0019b52
Showing
8 changed files
with
258 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
PythonLib/test_cinderx/test_compiler/test_static/type_alias.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
import sys | ||
import unittest | ||
from unittest import skipIf | ||
|
||
from re import escape | ||
|
||
from cinderx.compiler.static.types import TypedSyntaxError | ||
|
||
from .common import StaticTestBase | ||
|
||
|
||
@skipIf(sys.version_info < (3, 12), "New in 3.12") | ||
class TypeAliasTests(StaticTestBase): | ||
|
||
def test_assign(self): | ||
codestr = """ | ||
class B: pass | ||
A = B | ||
def f(x: A): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(mod.B()) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_alias(self): | ||
codestr = """ | ||
type A = int | ||
def f(x: A): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_optional_assign(self): | ||
codestr = """ | ||
A = int | None | ||
def f(x: A): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_optional_alias(self): | ||
codestr = """ | ||
type A = int | None | ||
def f(x: A): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_transitive_alias(self): | ||
codestr = """ | ||
type A = int | None | ||
type B = A | ||
def f(x: B): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_transitive_assign(self): | ||
codestr = """ | ||
A = int | None | ||
B = A | ||
def f(x: B): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_transitive_alias_and_assign(self): | ||
codestr = """ | ||
A = int | None | ||
type B = A | ||
def f(x: B): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_transitive_alias_in_optional(self): | ||
codestr = """ | ||
A = int | ||
type B = A | None | ||
def f(x: B): | ||
pass | ||
""" | ||
with self.in_module(codestr) as mod: | ||
mod.f(42) | ||
mod.f(None) | ||
with self.assertRaises(TypeError): | ||
mod.f("hello") | ||
|
||
def test_alias_check_in_module(self): | ||
codestr = """ | ||
class B: pass | ||
A = B | ||
def f(x: A): | ||
pass | ||
f("hello") | ||
""" | ||
with self.assertRaises(TypedSyntaxError): | ||
with self.in_module(codestr): | ||
pass | ||
|
||
def test_overload(self): | ||
codestr = """ | ||
A = int | ||
class B: | ||
def f(self, x: A): | ||
pass | ||
class D(B): | ||
def f(self, x: int): | ||
super().f(x) | ||
D().f(10) | ||
""" | ||
with self.in_module(codestr): | ||
pass | ||
|
||
def test_type_alias_error(self): | ||
codestr = """ | ||
type A = 42 | ||
""" | ||
with self.assertRaisesRegex(TypedSyntaxError, "A is not a type"): | ||
with self.in_module(codestr): | ||
pass | ||
|
||
def test_assign_to_constructor(self): | ||
# Regression test for a crash when calling resolve_type on the rhs of B | ||
codestr = """ | ||
B = str("<unknown>") | ||
""" | ||
with self.in_module(codestr): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters