Skip to content

Commit 02bec00

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 7204091 commit 02bec00

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
@@ -2998,6 +2998,235 @@ let test_higher_order_call_graph_fixpoint =
29982998
@subclass
29992999
def stub_contextmanager(x):
30003000
print(x)
3001+
|}
3002+
~expected:
3003+
[
3004+
{
3005+
Expected.callable =
3006+
Target.Regular.Function { name = "test.stub_contextmanager"; kind = Decorated }
3007+
|> Target.from_regular;
3008+
call_graph =
3009+
[
3010+
( "9:1-9:9",
3011+
LocationCallees.Singleton
3012+
(ExpressionCallees.from_call
3013+
(CallCallees.create
3014+
~call_targets:
3015+
[
3016+
CallTarget.create_regular
3017+
~implicit_receiver:true
3018+
~implicit_dunder_call:true
3019+
~receiver_class:"test.Subclass"
3020+
(Target.Regular.Method
3021+
{
3022+
class_name = "test.Base";
3023+
method_name = "__call__";
3024+
kind = Normal;
3025+
});
3026+
]
3027+
~higher_order_parameters:
3028+
(HigherOrderParameterMap.from_list
3029+
[
3030+
{
3031+
index = 0;
3032+
call_targets =
3033+
[
3034+
CallTarget.create_regular
3035+
(Target.Regular.Function
3036+
{
3037+
name = "test.stub_contextmanager";
3038+
kind = Normal;
3039+
});
3040+
];
3041+
unresolved = CallGraph.Unresolved.False;
3042+
};
3043+
])
3044+
())) );
3045+
( "10:0-11:10",
3046+
LocationCallees.Singleton
3047+
(ExpressionCallees.from_attribute_access
3048+
(AttributeAccessCallees.create
3049+
~callable_targets:
3050+
[
3051+
CallTarget.create_regular
3052+
(Target.Regular.Function
3053+
{ name = "test.stub_contextmanager"; kind = Normal });
3054+
]
3055+
())) );
3056+
];
3057+
returned_callables = [];
3058+
};
3059+
]
3060+
();
3061+
labeled_test_case __FUNCTION__ __LINE__
3062+
@@ assert_higher_order_call_graph_fixpoint
3063+
~source:
3064+
{|
3065+
class Base:
3066+
def __enter__(self):
3067+
return self
3068+
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> bool:
3069+
return False
3070+
def __call__(self, func):
3071+
def inner(*args, **kwds):
3072+
func(*args, **kwds)
3073+
return inner
3074+
base = Base()
3075+
@base # Test custom context manager
3076+
def contextmanager(x):
3077+
print(x)
3078+
class Subclass(Base):
3079+
def __init__(self, name):
3080+
self.name = name
3081+
subclass = Subclass("123")
3082+
@subclass # Test subclass and decorator factory
3083+
def contextmanager_subclass(x):
3084+
print(x)
3085+
|}
3086+
~expected:
3087+
[
3088+
{
3089+
Expected.callable =
3090+
Target.Regular.Function { name = "test.contextmanager"; kind = Decorated }
3091+
|> Target.from_regular;
3092+
call_graph =
3093+
[
3094+
( "12:1-12:5",
3095+
LocationCallees.Singleton
3096+
(ExpressionCallees.from_call
3097+
(CallCallees.create
3098+
~call_targets:
3099+
[
3100+
CallTarget.create
3101+
~implicit_receiver:true
3102+
~implicit_dunder_call:true
3103+
~receiver_class:"test.Base"
3104+
(create_parameterized_target
3105+
~regular:
3106+
(Target.Regular.Method
3107+
{
3108+
class_name = "test.Base";
3109+
method_name = "__call__";
3110+
kind = Normal;
3111+
})
3112+
~parameters:
3113+
[
3114+
( create_positional_parameter 1 "func",
3115+
Target.Regular.Function
3116+
{ name = "test.contextmanager"; kind = Normal }
3117+
|> Target.from_regular );
3118+
]);
3119+
]
3120+
())) );
3121+
( "13:0-14:10",
3122+
LocationCallees.Singleton
3123+
(ExpressionCallees.from_attribute_access
3124+
(AttributeAccessCallees.create
3125+
~callable_targets:
3126+
[
3127+
CallTarget.create_regular
3128+
(Target.Regular.Function
3129+
{ name = "test.contextmanager"; kind = Normal });
3130+
]
3131+
())) );
3132+
];
3133+
returned_callables =
3134+
[
3135+
CallTarget.create
3136+
(create_parameterized_target
3137+
~regular:
3138+
(Target.Regular.Function
3139+
{ name = "test.Base.__call__.inner"; kind = Normal })
3140+
~parameters:
3141+
[
3142+
( AccessPath.Root.Variable "$parameter$func",
3143+
Target.Regular.Function
3144+
{ name = "test.contextmanager"; kind = Normal }
3145+
|> Target.from_regular );
3146+
]);
3147+
];
3148+
};
3149+
{
3150+
Expected.callable =
3151+
Target.Regular.Function
3152+
{ name = "test.contextmanager_subclass"; kind = Decorated }
3153+
|> Target.from_regular;
3154+
call_graph =
3155+
[
3156+
( "19:1-19:9",
3157+
LocationCallees.Singleton
3158+
(ExpressionCallees.from_call
3159+
(CallCallees.create
3160+
~call_targets:
3161+
[
3162+
CallTarget.create
3163+
~implicit_receiver:true
3164+
~implicit_dunder_call:true
3165+
~receiver_class:"test.Subclass"
3166+
(create_parameterized_target
3167+
~regular:
3168+
(Target.Regular.Method
3169+
{
3170+
class_name = "test.Base";
3171+
method_name = "__call__";
3172+
kind = Normal;
3173+
})
3174+
~parameters:
3175+
[
3176+
( create_positional_parameter 1 "func",
3177+
Target.Regular.Function
3178+
{
3179+
name = "test.contextmanager_subclass";
3180+
kind = Normal;
3181+
}
3182+
|> Target.from_regular );
3183+
]);
3184+
]
3185+
())) );
3186+
( "20:0-21:10",
3187+
LocationCallees.Singleton
3188+
(ExpressionCallees.from_attribute_access
3189+
(AttributeAccessCallees.create
3190+
~callable_targets:
3191+
[
3192+
CallTarget.create_regular
3193+
(Target.Regular.Function
3194+
{ name = "test.contextmanager_subclass"; kind = Normal });
3195+
]
3196+
())) );
3197+
];
3198+
returned_callables =
3199+
[
3200+
CallTarget.create
3201+
(create_parameterized_target
3202+
~regular:
3203+
(Target.Regular.Function
3204+
{ name = "test.Base.__call__.inner"; kind = Normal })
3205+
~parameters:
3206+
[
3207+
( AccessPath.Root.Variable "$parameter$func",
3208+
Target.Regular.Function
3209+
{ name = "test.contextmanager_subclass"; kind = Normal }
3210+
|> Target.from_regular );
3211+
]);
3212+
];
3213+
};
3214+
]
3215+
();
3216+
labeled_test_case __FUNCTION__ __LINE__
3217+
@@ assert_higher_order_call_graph_fixpoint
3218+
~source:
3219+
{|
3220+
class Base:
3221+
def __call__(self, func):
3222+
... # Test when `__call__` is a stub
3223+
class Subclass(Base):
3224+
def __init__(self, name):
3225+
self.name = name
3226+
subclass = Subclass("123")
3227+
@subclass
3228+
def stub_contextmanager(x):
3229+
print(x)
30013230
|}
30023231
~expected:
30033232
[

0 commit comments

Comments
 (0)