GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-125618: Make FORWARDREF format succeed more often by JelleZijlstra · Pull Request #132818 · python/cpython

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Apr 22, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Fixes #125618.

cc @DavidCEllis

  • Issue: [3.14] annotationlib - get_annotations returns an empty annotations dict if an AttributeError is raised when __annotations__ is accessed #125618

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JelleZijlstra
pythongh-125618: Make FORWARDREF format succeed more often
15ea954
Fixes python#125618.
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 22, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Apr 22, 2025
[3.14] annotationlib - get_annotations returns an empty annotations dict if an AttributeError is raised when __annotations__ is accessed #125618
Closed
@JelleZijlstra
one line
8b776d0
@DavidCEllis

DavidCEllis commented Apr 23, 2025

Copy link
Copy Markdown
Contributor

Looks like this works in the case where the object is defined globally, but this still manages to fail if the object used in the annotation is defined (or even potentially redefined) within the function.

Repro:

from annotationlib import get_annotations, Format

def boom():
    obj = object()
    class RaisesAttributeError:
        attriberr: obj.missing
    get_annotations(RaisesAttributeError, format=Format.FORWARDREF)

boom()

Output:

Traceback (most recent call last):
  File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 9, in <module>
    boom()
    ~~~~^^
  File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 7, in boom
    get_annotations(RaisesAttributeError, format=Format.FORWARDREF)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 778, in get_annotations
    ann = _get_and_call_annotate(obj, format)
  File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 902, in _get_and_call_annotate
    ann = call_annotate_function(annotate, format, owner=obj)
  File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 629, in call_annotate_function
    result = func(Format.VALUE_WITH_FAKE_GLOBALS)
  File "C:\Users\ducks\Source\cpython\.cache\annotation_examine.py", line 6, in __annotate__
    attriberr: obj.missing
               ^^^^^^^^^^^
  File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 380, in __getattr__
    return self.__make_new(ast.Attribute(self.__get_ast(), attr))
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ducks\Source\cpython\Lib\annotationlib.py", line 358, in __make_new
    self.__stringifier_dict__.stringifiers.append(stringifier)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'builtin_function_or_method' object has no attribute 'stringifiers'

This came up because this is how I'd written my test case for this (writing the test case was actually as far as I'd gone, because the first test case raised the other issue).

    def test_attributeerror_handled(self):
        class MissingAttrib:
            pass
        
        class RaisesAttributeError:
            attriberr: MissingAttrib.missing

        with self.assertRaises(AttributeError):
            get_annotations(RaisesAttributeError, format=Format.VALUE)

        self.assertEqual(
            get_annotations(RaisesAttributeError, format=Format.FORWARDREF),
            {
                "attriberr": support.EqualToForwardRef(
                    "MissingAttrib.missing", 
                    owner=RaisesAttributeError,
                    is_class=True,
                )
            }
        )

        self.assertEqual(
            get_annotations(RaisesAttributeError, format=Format.STRING),
            {"attriberr": "MissingAttrib.missing"}
        )
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra added 2 commits April 23, 2025 06:03
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into fwdref-format
fb5354d
@JelleZijlstra
fix
d095e2c
@JelleZijlstra

JelleZijlstra commented Apr 23, 2025

Copy link
Copy Markdown
Member Author

Thanks! This was a silly bug that also broke test_pydoc, I will add your case as a test case so we catch this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra added 7 commits April 23, 2025 07:57
@JelleZijlstra
add test
d27ffd8
@JelleZijlstra
fix test
bf260fb
@JelleZijlstra
formatting
28be691
@JelleZijlstra
:Merge remote-tracking branch 'upstream/main' into fwdref-format
675f55c
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into fwdref-format
f1d8888
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into fwdref-format
ec613f8
@JelleZijlstra
fix merge
9684e40
@JelleZijlstra
JelleZijlstra merged commit af5799f into python:main May 4, 2025
@bedevere-app bedevere-app Bot removed the awaiting core review label May 4, 2025
@JelleZijlstra
JelleZijlstra deleted the fwdref-format branch May 4, 2025 22:22
diegorusso added a commit to diegorusso/cpython that referenced this pull request May 4, 2025
@diegorusso
Merge remote-tracking branch 'origin/main' into call-len
251007d
* origin/main:
  pythongh-125618: Make FORWARDREF format succeed more often (python#132818)
@JelleZijlstra JelleZijlstra mentioned this pull request May 4, 2025
gh-119180: Make the FORWARDREF format work with more kinds of runtime errors #133407
Merged
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@JelleZijlstra @Pranjal095
pythongh-125618: Make FORWARDREF format succeed more often (python#13…
03ce360
…2818)

Fixes python#125618.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[3.14] annotationlib - get_annotations returns an empty annotations dict if an AttributeError is raised when __annotations__ is accessed

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@JelleZijlstra @DavidCEllis
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.