@@ -1708,18 +1708,19 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
1708
1708
def visit_import (self , i : Import ) -> None :
1709
1709
self .statement = i
1710
1710
for id , as_id in i .ids :
1711
+ # Modules imported in a stub file without using 'import X as X' won't get exported
1712
+ # When implicit re-exporting is disabled, we have the same behavior as stubs.
1713
+ use_implicit_reexport = not self .is_stub_file and self .options .implicit_reexport
1711
1714
if as_id is not None :
1712
- self .add_module_symbol (id , as_id , module_public = True , context = i )
1715
+ base_id = id
1716
+ imported_id = as_id
1717
+ module_public = use_implicit_reexport or id .split ("." )[- 1 ] == as_id
1713
1718
else :
1714
- # Modules imported in a stub file without using 'as x' won't get exported
1715
- # When implicit re-exporting is disabled, we have the same behavior as stubs.
1716
- module_public = (
1717
- not self .is_stub_file
1718
- and self .options .implicit_reexport
1719
- )
1720
- base = id .split ('.' )[0 ]
1721
- self .add_module_symbol (base , base , module_public = module_public ,
1722
- context = i , module_hidden = not module_public )
1719
+ base_id = id .split ('.' )[0 ]
1720
+ imported_id = base_id
1721
+ module_public = use_implicit_reexport
1722
+ self .add_module_symbol (base_id , imported_id , context = i , module_public = module_public ,
1723
+ module_hidden = not module_public )
1723
1724
1724
1725
def visit_import_from (self , imp : ImportFrom ) -> None :
1725
1726
self .statement = imp
@@ -1762,17 +1763,21 @@ def visit_import_from(self, imp: ImportFrom) -> None:
1762
1763
if gvar :
1763
1764
self .add_symbol (imported_id , gvar , imp )
1764
1765
continue
1766
+
1767
+ # Modules imported in a stub file without using 'from Y import X as X' will
1768
+ # not get exported.
1769
+ # When implicit re-exporting is disabled, we have the same behavior as stubs.
1770
+ use_implicit_reexport = not self .is_stub_file and self .options .implicit_reexport
1771
+ module_public = use_implicit_reexport or (as_id is not None and id == as_id )
1772
+
1765
1773
if node and not node .module_hidden :
1766
- self .process_imported_symbol (node , module_id , id , as_id , fullname , imp )
1774
+ self .process_imported_symbol (
1775
+ node , module_id , id , imported_id , fullname , module_public , context = imp
1776
+ )
1767
1777
elif module and not missing_submodule :
1768
1778
# Target module exists but the imported name is missing or hidden.
1769
1779
self .report_missing_module_attribute (module_id , id , imported_id , imp )
1770
1780
else :
1771
- module_public = (
1772
- not self .is_stub_file
1773
- and self .options .implicit_reexport
1774
- or as_id is not None
1775
- )
1776
1781
# Import of a missing (sub)module.
1777
1782
self .add_unknown_imported_symbol (
1778
1783
imported_id , imp , target_name = fullname , module_public = module_public
@@ -1782,17 +1787,10 @@ def process_imported_symbol(self,
1782
1787
node : SymbolTableNode ,
1783
1788
module_id : str ,
1784
1789
id : str ,
1785
- as_id : Optional [ str ] ,
1790
+ imported_id : str ,
1786
1791
fullname : str ,
1792
+ module_public : bool ,
1787
1793
context : ImportBase ) -> None :
1788
- imported_id = as_id or id
1789
- # 'from m import x as x' exports x in a stub file or when implicit
1790
- # re-exports are disabled.
1791
- module_public = (
1792
- not self .is_stub_file
1793
- and self .options .implicit_reexport
1794
- or as_id is not None
1795
- )
1796
1794
module_hidden = not module_public and fullname not in self .modules
1797
1795
1798
1796
if isinstance (node .node , PlaceholderNode ):
@@ -4419,12 +4417,19 @@ def add_redefinition(self,
4419
4417
return
4420
4418
i += 1
4421
4419
4420
+ def add_local (self , node : Union [Var , FuncDef , OverloadedFuncDef ], context : Context ) -> None :
4421
+ """Add local variable or function."""
4422
+ assert self .is_func_scope ()
4423
+ name = node .name
4424
+ node ._fullname = name
4425
+ self .add_symbol (name , node , context )
4426
+
4422
4427
def add_module_symbol (self ,
4423
4428
id : str ,
4424
4429
as_id : str ,
4425
- module_public : bool ,
4426
4430
context : Context ,
4427
- module_hidden : bool = False ) -> None :
4431
+ module_public : bool ,
4432
+ module_hidden : bool ) -> None :
4428
4433
"""Add symbol that is a reference to a module object."""
4429
4434
if id in self .modules :
4430
4435
node = self .modules [id ]
@@ -4436,19 +4441,12 @@ def add_module_symbol(self,
4436
4441
as_id , context , target_name = id , module_public = module_public
4437
4442
)
4438
4443
4439
- def add_local (self , node : Union [Var , FuncDef , OverloadedFuncDef ], context : Context ) -> None :
4440
- """Add local variable or function."""
4441
- assert self .is_func_scope ()
4442
- name = node .name
4443
- node ._fullname = name
4444
- self .add_symbol (name , node , context )
4445
-
4446
4444
def add_imported_symbol (self ,
4447
4445
name : str ,
4448
4446
node : SymbolTableNode ,
4449
4447
context : Context ,
4450
- module_public : bool = True ,
4451
- module_hidden : bool = False ) -> None :
4448
+ module_public : bool ,
4449
+ module_hidden : bool ) -> None :
4452
4450
"""Add an alias to an existing symbol through import."""
4453
4451
assert not module_hidden or not module_public
4454
4452
symbol = SymbolTableNode (node .kind , node .node ,
0 commit comments