20
20
Callable ,
21
21
Collection ,
22
22
Dict ,
23
+ Generator ,
23
24
Generic ,
24
25
Iterable ,
25
26
Iterator ,
@@ -2910,7 +2911,23 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
2910
2911
2911
2912
def get_future_imports (node : Node ) -> Set [str ]:
2912
2913
"""Return a set of __future__ imports in the file."""
2913
- imports = set ()
2914
+ imports : Set [str ] = set ()
2915
+
2916
+ def get_imports_from_children (children : List [LN ]) -> Generator [str , None , None ]:
2917
+ for child in children :
2918
+ if isinstance (child , Leaf ):
2919
+ if child .type == token .NAME :
2920
+ yield child .value
2921
+ elif child .type == syms .import_as_name :
2922
+ orig_name = child .children [0 ]
2923
+ assert isinstance (orig_name , Leaf ), "Invalid syntax parsing imports"
2924
+ assert orig_name .type == token .NAME , "Invalid syntax parsing imports"
2925
+ yield orig_name .value
2926
+ elif child .type == syms .import_as_names :
2927
+ yield from get_imports_from_children (child .children )
2928
+ else :
2929
+ assert False , "Invalid syntax parsing imports"
2930
+
2914
2931
for child in node .children :
2915
2932
if child .type != syms .simple_stmt :
2916
2933
break
@@ -2929,15 +2946,7 @@ def get_future_imports(node: Node) -> Set[str]:
2929
2946
module_name = first_child .children [1 ]
2930
2947
if not isinstance (module_name , Leaf ) or module_name .value != "__future__" :
2931
2948
break
2932
- for import_from_child in first_child .children [3 :]:
2933
- if isinstance (import_from_child , Leaf ):
2934
- if import_from_child .type == token .NAME :
2935
- imports .add (import_from_child .value )
2936
- else :
2937
- assert import_from_child .type == syms .import_as_names
2938
- for leaf in import_from_child .children :
2939
- if isinstance (leaf , Leaf ) and leaf .type == token .NAME :
2940
- imports .add (leaf .value )
2949
+ imports |= set (get_imports_from_children (first_child .children [3 :]))
2941
2950
else :
2942
2951
break
2943
2952
return imports
0 commit comments