- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.3k
 
          Clean sys.modules if TYPE_CHECKING=True import fails
          #11645
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
  
    Clean sys.modules if TYPE_CHECKING=True import fails
  
  #11645
              Conversation
cba1453    to
    b1977c5      
    Compare
  
    | 
           The 1 failing test run seems unrelated to my changes, AFAICS  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an explicit test checking the state of sys.modules please?
        
          
                sphinx/ext/autodoc/importer.py
              
                Outdated
          
        
      | objpath = list(objpath) | ||
| while module is None: | ||
| try: | ||
| orig_modules = set(sys.modules) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a frozenset instead. Same in the except when you are running over the modules to remove them. At least it shows that the variable is meant to be readonly.
          
 Can you elaborate on what you'd like this test to look like? I'm not sure I follow. At what point do you want  Oh, maybe I see: are you just suggesting that tests/roots/test-ext-autodoc/circular_import/c.py should have something like   | 
    
d5bdf5e    to
    2077f50      
    Compare
  
    | 
           Actually the test root is only some fixture directory so it should be tested in the test body. For instance, you check what sys.modules is before and after building (I think?).  | 
    
| 
           Tentatively targeting 7.2.5 for this, as it is a regression from 7.1. a  | 
    
autodoc now attempts to import with `typing.TYPE_CHECKING = True` first, and then falls back to `typing.TYPE_CHECKING = False` if that fails. Unfortunately, the first import can leave behind some partially-imported modules in `sys.modules` such that the retry fails. Attempt to work around this by detecting what modules were added to `sys.modules` by the first attempt and removing them before retrying. Signed-off-by: Matt Wozniski <[email protected]>
2077f50    to
    8528c31      
    Compare
  
    
          
 OK, I've added a test that explicitly looks at   | 
    
| 
           Thank you. I was thinking about a more explicit check where you check that some module does not exist in   | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation looks good, please ping when the test suggested by @picnixz has been added for merging. Please also add an entry to CHANGES describing the bug fixed as I intend this to be included in 7.2.5.
A
          
 I don't understand... The regression that this PR is fixing is that autodoc fails to import some modules. The buggy behavior that the new test would exhibit without my fix is that autodoc fails to import   | 
    
| 
           I'd understood it to mean checking that a half-imported module isn't left over, even if the entire import fails, but that might be difficult to set up a test-case for. A  | 
    
          
 This PR won't do that, though. It prevents a failed import done with   | 
    
| 
           Ahh! Then let's leave things as they are, just the CHANGES entry needed please if possible! A  | 
    
| 
           Ah, my bad then, I understood wrongly.  | 
    
| 
           @AA-Turner I've added that CHANGES entry  | 
    
| 
           Thanks -- I've credited you as Matt, just checking on Matt vs Matthew?  | 
    
| 
           I prefer "Matt" - thanks for checking!  | 
    
| 
           Thanks @godlygeek! A  | 
    
autodoc now attempts to import with
typing.TYPE_CHECKING = Truefirst, and then falls back totyping.TYPE_CHECKING = Falseif that fails. Unfortunately, the first import can leave behind some partially-imported modules insys.modulessuch that the retry fails.Attempt to work around this by detecting what modules were added to
sys.modulesby the first attempt and removing them before retrying.Feature or Bugfix
Purpose
autodoc now attempts to importing with
typing.TYPE_CHECKING = Truefirst, and then falls back totyping.TYPE_CHECKING = Falseif that fails. Unfortunately, the first import can leave behind some partially-imported modules insys.modulessuch that the retry fails.Attempt to work around this by detecting what modules were added to
sys.modulesby the first attempt and removing them before retrying.Detail / Relates
See #11642