@@ -66,24 +66,23 @@ def __init__(self, name: str, spec: ModuleSpec):
66
66
vars (self ).update (locals ())
67
67
del self .self
68
68
69
+ def _find_assignments (self ) -> Iterator [Tuple [ast .AST , ast .AST ]]:
70
+ for statement in self .module .body :
71
+ if isinstance (statement , ast .Assign ):
72
+ yield from ((target , statement .value ) for target in statement .targets )
73
+ elif isinstance (statement , ast .AnnAssign ) and statement .value :
74
+ yield (statement .target , statement .value )
75
+
69
76
def __getattr__ (self , attr ):
70
77
"""Attempt to load an attribute "statically", via :func:`ast.literal_eval`."""
71
78
try :
72
- for statement in self .module .body :
73
- if isinstance (statement , ast .Assign ):
74
- targets = statement .targets
75
- value = statement .value
76
- elif isinstance (statement , ast .AnnAssign ):
77
- targets = [statement .target ]
78
- value = statement .value
79
- else :
80
- continue
81
- for target in targets :
82
- if isinstance (target , ast .Name ) and target .id == attr :
83
- return ast .literal_eval (value )
79
+ return next (
80
+ ast .literal_eval (value )
81
+ for target , value in self ._find_assignments ()
82
+ if isinstance (target , ast .Name ) and target .id == attr
83
+ )
84
84
except Exception as e :
85
85
raise AttributeError (f"{ self .name } has no attribute { attr } " ) from e
86
- raise AttributeError (f"{ self .name } has no attribute { attr } " )
87
86
88
87
89
88
def glob_relative (
0 commit comments