Skip to content

Conversation

DavidCEllis
Copy link
Contributor

@DavidCEllis DavidCEllis commented Sep 11, 2025

This fixes an issue where user defined __annotate__ functions that do not support a specific format would be called with fake globals in a way that they couldn't correctly raise NotImplementedError.

One downside is that some annotate functions that may have appeared to 'work' before now correctly fail. It's possible we may want to add some kind of fallback to VALUE annotations in these cases, but calling with fake globals as it does now would only accidentally succeed depending on exactly how the function performs the comparisons.

from annotationlib import Format, call_annotate_function
def annotate(format, /):
    if format == Format.VALUE:
        return {'x': str}
    else:
        raise NotImplementedError(format)

print(call_annotate_function(annotate, Format.STRING))

Current:

{'x': 'str'}

With patch:

NotImplementedError: 2

But change the comparison to check if the format is in a set:

from annotationlib import Format, call_annotate_function

def annotate(format, /):
    if format in {Format.VALUE}:
        return {'x': str}
    else:
        raise NotImplementedError(format)

print(call_annotate_function(annotate, Format.STRING))

Current:

TypeError: exceptions must derive from BaseException

With patch:

NotImplementedError: 2

annotate.assert_called_once_with(Format.FORWARDREF)

# Test the call on the function with fake globals
new_annotate.assert_called_once_with(Format.VALUE_WITH_FAKE_GLOBALS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of these tests as they're asserting details of the implementation, not the observable behavior. Is there a way to write a test that's closer to your use case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this could instead test multiple annotate functions that provide only those formats?

Let me know if you prefer this or #138803 and I'll modify the tests for the relevant PR.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants