- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.3k
gh-74185: repr() of ImportError now contains attributes name and path #136770
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            31 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      cb764d9
              
                bpo-29999: repr() of ImportError now contains attributes name and path.
              
              
                serhiy-storchaka db41c7d
              
                Use GitHub Actions version of Bedevere
              
              
                arhadthedev 1caeb91
              
                Add the news entry back
              
              
                arhadthedev 7f29270
              
                Remove an accidentally included untracked file
              
              
                arhadthedev c80e056
              
                first commit
              
              
                ynir3 126be20
              
                add test_ModuleNotFoundError_repr_with_failed_import
              
              
                ynir3 4a97f5c
              
                add test_ModuleNotFoundError_repr_with_failed_import
              
              
                ynir3 852da8c
              
                add blurb
              
              
                ynir3 6ab7362
              
                add noqa:F401
              
              
                ynir3 12c2293
              
                remove whitespace
              
              
                ynir3 a53da37
              
                fix trailing-whitespace
              
              
                ynir3 5b6d626
              
                use ComplexExtendsException
              
              
                ynir3 951e0a1
              
                return to non-macro style
              
              
                ynir3 8fbfd18
              
                use modern field initializers
              
              
                ynir3 458bcd0
              
                remove un-necessary casts
              
              
                ynir3 fa2f884
              
                use PyUnicodeWriter
              
              
                ynir3 35a6a29
              
                fix trailing whitespace
              
              
                ynir3 6e416c6
              
                reduce nesting
              
              
                ynir3 aa75f6a
              
                use PyUnicdodeWriter_Format and if statement braces
              
              
                ynir3 8b5e4ed
              
                indentation
              
              
                ynir3 28545e5
              
                move decref
              
              
                ynir3 4f63193
              
                check baseException_repr failure, add 3.15rst
              
              
                ynir3 62a479b
              
                Merge branch 'main' into importerror-repr
              
              
                Yoav11 25f3d42
              
                fix chr c input
              
              
                ynir3 a45ed75
              
                move tests, edit rst wording
              
              
                ynir3 69e7305
              
                Merge branch 'main' into importerror-repr
              
              
                Yoav11 9c09cd0
              
                edit blurb
              
              
                ynir3 ca0e2a4
              
                edit blurb
              
              
                ynir3 b0d7f4a
              
                Merge branch 'main' into importerror-repr
              
              
                Yoav11 3e57fba
              
                Merge branch 'main' into importerror-repr
              
              
                ynir3 3a30ef3
              
                Py_DECREF and early exit logic
              
              
                ynir3 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            4 changes: 4 additions & 0 deletions
          
          4 
        
  Misc/NEWS.d/next/Core_and_Builtins/2025-07-19-10-35-31.gh-issue-74185.7hPCA5.rst
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError` | ||
| now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given | ||
| as keyword arguments at construction time. | ||
| Patch by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
Are there other exceptions that will benefit from having keywords in their reprs? AFAICT, the keywords are only rendered if they were passed to the constructor as keyword arguments. In particular, we could make a generic
reprhelper for that which takes into account them, something like:Its usage would be
Uh oh!
There was an error while loading. Please reload this page.
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.
For now, let's hold off this idea as I don't know if it can be useful. But this can be worth investigating instead of storing the given keywords explicitly.
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.
yes - AFAIK only ImportError and ModuleNotFoundError (which hinnerits this functionality as per the tests) have optional arguments ? but makes sense to me that if other exception types in the future have optionals then we could extract this out.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, but let's do it in a follow-up PR so that we can revert the helper commit more easily. Also it depends on whether we would store the keywords passed to the constructor (this is #11580)