Skip to content

Commit 7204091

Browse files
Tianhan Lufacebook-github-bot
Tianhan Lu
authored andcommitted
Add test cases for context manager class decorators for higher order call graphs
Reviewed By: arthaud Differential Revision: D71836151 fbshipit-source-id: df00b0bb893d4c0c8aa317b938d399e9d88e0da1
1 parent 6f5797f commit 7204091

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

source/interprocedural/test/callGraphFixpointTest.ml

+229
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,235 @@ let test_higher_order_call_graph_fixpoint =
27692769
@subclass
27702770
def stub_contextmanager(x):
27712771
print(x)
2772+
|}
2773+
~expected:
2774+
[
2775+
{
2776+
Expected.callable =
2777+
Target.Regular.Function { name = "test.stub_contextmanager"; kind = Decorated }
2778+
|> Target.from_regular;
2779+
call_graph =
2780+
[
2781+
( "9:1-9:9",
2782+
LocationCallees.Singleton
2783+
(ExpressionCallees.from_call
2784+
(CallCallees.create
2785+
~call_targets:
2786+
[
2787+
CallTarget.create_regular
2788+
~implicit_receiver:true
2789+
~implicit_dunder_call:true
2790+
~receiver_class:"test.Subclass"
2791+
(Target.Regular.Method
2792+
{
2793+
class_name = "test.Base";
2794+
method_name = "__call__";
2795+
kind = Normal;
2796+
});
2797+
]
2798+
~higher_order_parameters:
2799+
(HigherOrderParameterMap.from_list
2800+
[
2801+
{
2802+
index = 0;
2803+
call_targets =
2804+
[
2805+
CallTarget.create_regular
2806+
(Target.Regular.Function
2807+
{
2808+
name = "test.stub_contextmanager";
2809+
kind = Normal;
2810+
});
2811+
];
2812+
unresolved = CallGraph.Unresolved.False;
2813+
};
2814+
])
2815+
())) );
2816+
( "10:0-11:10",
2817+
LocationCallees.Singleton
2818+
(ExpressionCallees.from_attribute_access
2819+
(AttributeAccessCallees.create
2820+
~callable_targets:
2821+
[
2822+
CallTarget.create_regular
2823+
(Target.Regular.Function
2824+
{ name = "test.stub_contextmanager"; kind = Normal });
2825+
]
2826+
())) );
2827+
];
2828+
returned_callables = [];
2829+
};
2830+
]
2831+
();
2832+
labeled_test_case __FUNCTION__ __LINE__
2833+
@@ assert_higher_order_call_graph_fixpoint
2834+
~source:
2835+
{|
2836+
class Base:
2837+
def __enter__(self):
2838+
return self
2839+
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> bool:
2840+
return False
2841+
def __call__(self, func):
2842+
def inner(*args, **kwds):
2843+
func(*args, **kwds)
2844+
return inner
2845+
base = Base()
2846+
@base # Test custom context manager
2847+
def contextmanager(x):
2848+
print(x)
2849+
class Subclass(Base):
2850+
def __init__(self, name):
2851+
self.name = name
2852+
subclass = Subclass("123")
2853+
@subclass # Test subclass and decorator factory
2854+
def contextmanager_subclass(x):
2855+
print(x)
2856+
|}
2857+
~expected:
2858+
[
2859+
{
2860+
Expected.callable =
2861+
Target.Regular.Function { name = "test.contextmanager"; kind = Decorated }
2862+
|> Target.from_regular;
2863+
call_graph =
2864+
[
2865+
( "12:1-12:5",
2866+
LocationCallees.Singleton
2867+
(ExpressionCallees.from_call
2868+
(CallCallees.create
2869+
~call_targets:
2870+
[
2871+
CallTarget.create
2872+
~implicit_receiver:true
2873+
~implicit_dunder_call:true
2874+
~receiver_class:"test.Base"
2875+
(create_parameterized_target
2876+
~regular:
2877+
(Target.Regular.Method
2878+
{
2879+
class_name = "test.Base";
2880+
method_name = "__call__";
2881+
kind = Normal;
2882+
})
2883+
~parameters:
2884+
[
2885+
( create_positional_parameter 1 "func",
2886+
Target.Regular.Function
2887+
{ name = "test.contextmanager"; kind = Normal }
2888+
|> Target.from_regular );
2889+
]);
2890+
]
2891+
())) );
2892+
( "13:0-14:10",
2893+
LocationCallees.Singleton
2894+
(ExpressionCallees.from_attribute_access
2895+
(AttributeAccessCallees.create
2896+
~callable_targets:
2897+
[
2898+
CallTarget.create_regular
2899+
(Target.Regular.Function
2900+
{ name = "test.contextmanager"; kind = Normal });
2901+
]
2902+
())) );
2903+
];
2904+
returned_callables =
2905+
[
2906+
CallTarget.create
2907+
(create_parameterized_target
2908+
~regular:
2909+
(Target.Regular.Function
2910+
{ name = "test.Base.__call__.inner"; kind = Normal })
2911+
~parameters:
2912+
[
2913+
( AccessPath.Root.Variable "$parameter$func",
2914+
Target.Regular.Function
2915+
{ name = "test.contextmanager"; kind = Normal }
2916+
|> Target.from_regular );
2917+
]);
2918+
];
2919+
};
2920+
{
2921+
Expected.callable =
2922+
Target.Regular.Function
2923+
{ name = "test.contextmanager_subclass"; kind = Decorated }
2924+
|> Target.from_regular;
2925+
call_graph =
2926+
[
2927+
( "19:1-19:9",
2928+
LocationCallees.Singleton
2929+
(ExpressionCallees.from_call
2930+
(CallCallees.create
2931+
~call_targets:
2932+
[
2933+
CallTarget.create
2934+
~implicit_receiver:true
2935+
~implicit_dunder_call:true
2936+
~receiver_class:"test.Subclass"
2937+
(create_parameterized_target
2938+
~regular:
2939+
(Target.Regular.Method
2940+
{
2941+
class_name = "test.Base";
2942+
method_name = "__call__";
2943+
kind = Normal;
2944+
})
2945+
~parameters:
2946+
[
2947+
( create_positional_parameter 1 "func",
2948+
Target.Regular.Function
2949+
{
2950+
name = "test.contextmanager_subclass";
2951+
kind = Normal;
2952+
}
2953+
|> Target.from_regular );
2954+
]);
2955+
]
2956+
())) );
2957+
( "20:0-21:10",
2958+
LocationCallees.Singleton
2959+
(ExpressionCallees.from_attribute_access
2960+
(AttributeAccessCallees.create
2961+
~callable_targets:
2962+
[
2963+
CallTarget.create_regular
2964+
(Target.Regular.Function
2965+
{ name = "test.contextmanager_subclass"; kind = Normal });
2966+
]
2967+
())) );
2968+
];
2969+
returned_callables =
2970+
[
2971+
CallTarget.create
2972+
(create_parameterized_target
2973+
~regular:
2974+
(Target.Regular.Function
2975+
{ name = "test.Base.__call__.inner"; kind = Normal })
2976+
~parameters:
2977+
[
2978+
( AccessPath.Root.Variable "$parameter$func",
2979+
Target.Regular.Function
2980+
{ name = "test.contextmanager_subclass"; kind = Normal }
2981+
|> Target.from_regular );
2982+
]);
2983+
];
2984+
};
2985+
]
2986+
();
2987+
labeled_test_case __FUNCTION__ __LINE__
2988+
@@ assert_higher_order_call_graph_fixpoint
2989+
~source:
2990+
{|
2991+
class Base:
2992+
def __call__(self, func):
2993+
... # Test when `__call__` is a stub
2994+
class Subclass(Base):
2995+
def __init__(self, name):
2996+
self.name = name
2997+
subclass = Subclass("123")
2998+
@subclass
2999+
def stub_contextmanager(x):
3000+
print(x)
27723001
|}
27733002
~expected:
27743003
[

0 commit comments

Comments
 (0)