Skip to content

Commit

Permalink
Verify decorator constraint solving
Browse files Browse the repository at this point in the history
Summary:
To get started I used a trivial decorator that spits out a primitive type, but
let's add tests verifying constraint solving (which most real decorators require)
for signature-preserving and signature-modifying decorators.

These tests work, although I encountered a bug in `assert_type` which causes
the assert to fail even though the type appears to be right; we will need to
investigate.

Reviewed By: rchen152

Differential Revision: D68218913

fbshipit-source-id: e4aabb9938a65877ab2f6b1098e332051243c223
  • Loading branch information
stroxler authored and facebook-github-bot committed Jan 15, 2025
1 parent 51d6e45 commit 2e27fe3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pyre2/pyre2/bin/test/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use crate::testcase;
use crate::testcase_with_bug;

testcase!(
test_simple_function_decorator,
Expand All @@ -21,3 +22,34 @@ def decorated(x: int) -> int:
assert_type(decorated, int)
"#,
);

testcase_with_bug!(
"The logic tested here passes, but assert_type is running into a problem we need to debug.",
test_identity_function_decorator,
r#"
from typing import assert_type, Callable, Any
def decorator[T](f: T) -> T: ...
@decorator
def decorated(x: int) -> str:
return f"{x}"
assert_type(decorated, Callable[[int], str]) # E: assert_type(Callable[[int], str], Callable[[int], str])
"#,
);

testcase!(
test_signature_modifying_function_decorator,
r#"
from typing import assert_type, Callable, Any
def decorator[T, R](f: Callable[[T], R]) -> Callable[[T, T], R]: ...
@decorator
def decorated(x: int) -> str:
return f"{x}"
assert_type(decorated, Callable[[int, int], str])
"#,
);

0 comments on commit 2e27fe3

Please sign in to comment.