1414import python
1515
1616/** Whether name is declared in the __all__ list of this module */
17- predicate declaredInAll ( Module m , StrConst name )
18- {
19- exists ( Assign a , GlobalVariable all |
20- a .defines ( all ) and a .getScope ( ) = m and
21- all .getId ( ) = "__all__" and ( ( List ) a .getValue ( ) ) .getAnElt ( ) = name
17+ predicate declaredInAll ( Module m , StrConst name ) {
18+ exists ( Assign a , GlobalVariable all |
19+ a .defines ( all ) and
20+ a .getScope ( ) = m and
21+ all .getId ( ) = "__all__" and
22+ a .getValue ( ) .( List ) .getAnElt ( ) = name
2223 )
2324}
2425
25- predicate mutates_globals ( PythonModuleObject m ) {
26+ predicate mutates_globals ( ModuleValue m ) {
2627 exists ( CallNode globals |
27- globals = Object:: builtin ( "globals" ) .( FunctionObject ) .getACall ( ) and
28- globals .getScope ( ) = m .getModule ( ) |
28+ globals = Value:: named ( "globals" ) .( FunctionValue ) .getACall ( ) and
29+ globals .getScope ( ) = m .getScope ( )
30+ |
2931 exists ( AttrNode attr | attr .getObject ( ) = globals )
3032 or
3133 exists ( SubscriptNode sub | sub .getValue ( ) = globals and sub .isStore ( ) )
3234 )
3335 or
34- exists ( Object enum_convert |
35- enum_convert .hasLongName ( "enum.Enum._convert" ) and
36- exists ( CallNode call |
37- call .getScope ( ) = m .getModule ( )
38- |
39- enum_convert .( FunctionObject ) .getACall ( ) = call or
40- call .getFunction ( ) .refersTo ( enum_convert )
36+ exists ( Value enum_convert , ClassValue enum_class |
37+ enum_class .getASuperType ( ) = Value:: named ( "enum.Enum" ) and
38+ enum_convert = enum_class .attr ( "_convert" ) and
39+ exists ( CallNode call | call .getScope ( ) = m .getScope ( ) |
40+ enum_convert .getACall ( ) = call or
41+ call .getFunction ( ) .pointsTo ( enum_convert )
4142 )
4243 )
4344}
4445
45- from PythonModuleObject m , StrConst name , string exported_name
46- where declaredInAll ( m .getModule ( ) , name ) and
47- exported_name = name .strValue ( ) and
48- not m .hasAttribute ( exported_name ) and
49- not ( m .getShortName ( ) = "__init__" and exists ( m .getPackage ( ) .getModule ( ) .getSubModule ( exported_name ) ) ) and
50- not exists ( ImportStarNode imp | imp .getEnclosingModule ( ) = m .getModule ( ) | not imp .getModule ( ) .refersTo ( _) ) and
51- not mutates_globals ( m )
52- select name , "The name '" + exported_name + "' is exported by __all__ but is not defined."
46+ predicate is_exported_submodule_name ( ModuleValue m , string exported_name ) {
47+ m .getScope ( ) .getShortName ( ) = "__init__" and
48+ exists ( m .getScope ( ) .getPackage ( ) .getSubModule ( exported_name ) )
49+ }
50+
51+ predicate contains_unknown_import_star ( ModuleValue m ) {
52+ exists ( ImportStarNode imp | imp .getEnclosingModule ( ) = m .getScope ( ) |
53+ imp .getModule ( ) .pointsTo ( ) .isAbsent ( )
54+ or
55+ not exists ( imp .getModule ( ) .pointsTo ( ) )
56+ )
57+ }
58+
59+ from ModuleValue m , StrConst name , string exported_name
60+ where
61+ declaredInAll ( m .getScope ( ) , name ) and
62+ exported_name = name .strValue ( ) and
63+ not m .hasAttribute ( exported_name ) and
64+ not is_exported_submodule_name ( m , exported_name ) and
65+ not contains_unknown_import_star ( m ) and
66+ not mutates_globals ( m )
67+ select name , "The name '" + exported_name + "' is exported by __all__ but is not defined."
0 commit comments