1414
1515import python
1616import Shadowing
17+ import semmle.python.types.Builtins
1718
1819predicate shadows ( Name d , GlobalVariable g , Scope scope , int line ) {
1920 exists ( LocalVariable l | d .defines ( l ) and l .getId ( ) = g .getId ( ) and
2021 scope instanceof Function and g .getScope ( ) = scope .getScope ( ) and
2122 not exists ( Import il , Import ig , Name gd | il .contains ( d ) and gd .defines ( g ) and ig .contains ( gd ) ) and
2223 not exists ( Assign a | a .getATarget ( ) = d and a .getValue ( ) = g .getAnAccess ( ) )
2324 ) and
24- not exists ( Object :: builtin ( g .getId ( ) ) ) and
25+ not exists ( Builtin :: builtin ( g .getId ( ) ) ) and
2526 d .getScope ( ) = scope and
2627 d .getLocation ( ) .getStartLine ( ) = line and
2728 exists ( Name defn | defn .defines ( g ) |
28- not exists ( If i | i .isNameEqMain ( ) |
29+ not exists ( If i | i .isNameEqMain ( ) |
2930 i .contains ( defn )
3031 )
3132 ) and
@@ -34,24 +35,24 @@ predicate shadows(Name d, GlobalVariable g, Scope scope, int line) {
3435
3536/* pytest dynamically populates its namespace so, we cannot look directly for the pytest.fixture function */
3637AttrNode pytest_fixture_attr ( ) {
37- exists ( ModuleObject pytest |
38- result .getObject ( "fixture" ) .refersTo ( pytest )
38+ exists ( ModuleValue pytest |
39+ result .getObject ( "fixture" ) .pointsTo ( pytest )
3940 )
4041}
4142
42- Object pytest_fixture ( ) {
43+ Value pytest_fixture ( ) {
4344 exists ( CallNode call |
4445 call .getFunction ( ) = pytest_fixture_attr ( )
4546 or
4647 call .getFunction ( ) .( CallNode ) .getFunction ( ) = pytest_fixture_attr ( )
4748 |
48- call .refersTo ( result )
49+ call .pointsTo ( result )
4950 )
5051}
5152
5253/* pytest fixtures require that the parameter name is also a global */
5354predicate assigned_pytest_fixture ( GlobalVariable v ) {
54- exists ( NameNode def | def .defines ( v ) and def .( DefinitionNode ) .getValue ( ) .refersTo ( pytest_fixture ( ) ) )
55+ exists ( NameNode def | def .defines ( v ) and def .( DefinitionNode ) .getValue ( ) .pointsTo ( pytest_fixture ( ) ) )
5556}
5657
5758predicate first_shadowing_definition ( Name d , GlobalVariable g ) {
@@ -61,6 +62,6 @@ predicate first_shadowing_definition(Name d, GlobalVariable g) {
6162}
6263
6364from Name d , GlobalVariable g , Name def
64- where first_shadowing_definition ( d , g ) and not exists ( Name n | n .deletes ( g ) ) and
65+ where first_shadowing_definition ( d , g ) and not exists ( Name n | n .deletes ( g ) ) and
6566 def .defines ( g ) and not assigned_pytest_fixture ( g ) and not g .getId ( ) = "_"
6667select d , "Local variable '" + g .getId ( ) + "' shadows a global variable defined $@." , def , "here"
0 commit comments