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-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib by JelleZijlstra · Pull Request #124415 · python/cpython

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Sep 24, 2024 •
edited by github-actions Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Refer to #3991.

  • Issue: Implement PEP 649 and PEP 749 #119180

📚 Documentation preview 📚: https://cpython-previews--124415.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

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

All reactions
@JelleZijlstra
pythongh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib
ddacdb7
@JelleZijlstra JelleZijlstra added the skip news label Sep 24, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Sep 24, 2024
Implement PEP 649 and PEP 749 #119180
Closed
21 of 30 tasks
JelleZijlstra added 6 commits September 25, 2024 12:10
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-fakevalue
5bb1d1e
@JelleZijlstra
Change the values
ab05089
@JelleZijlstra
fix constevaluator
3ba132a
@JelleZijlstra
Merge branch 'main' into pep649-fakevalue
26f1c7c
@JelleZijlstra
fix tests
2012bb4
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-fakevalue
f77be37
@JelleZijlstra
JelleZijlstra marked this pull request as ready for review September 26, 2024 04:18
@JelleZijlstra
JelleZijlstra requested review from AlexWaygood, iritkatriel and markshannon as code owners September 26, 2024 04:18
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 26, 2024
@JelleZijlstra
JelleZijlstra requested a review from larryhastings September 26, 2024 04:18
@JelleZijlstra
Merge branch 'main' into pep649-fakevalue
80818c6
larryhastings
larryhastings reviewed Sep 29, 2024
View reviewed changes

@larryhastings larryhastings left a comment

Copy link
Copy Markdown
Contributor

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.

Mostly this is about "the actual runtime values of the format constants shouldn't be defined as part of the API". I invite you to convince me otherwise.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Doc/library/annotationlib.rst
The exact values of these strings may change in future versions of Python.

.. attribute:: VALUE_WITH_FAKE_GLOBALS
:value: 4

@larryhastings larryhastings Sep 29, 2024

Copy link
Copy Markdown
Contributor

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.

VALUE_WITH_FAKE_GLOBALS is now 2. But why publish their values in the first place?

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Sep 29, 2024

Copy link
Copy Markdown
Member Author

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.

A wise man wrote these values in PEP 649 (https://peps.python.org/pep-0649/#overview). They are also relevant if you write an annotate function in C. But yes, we can probably do without them in Python code.

Sorry, something went wrong.

Uh oh!

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

AlexWaygood reacted with laugh emoji
All reactions
  • 😄 1 reaction
Comment thread Lib/test/test_annotationlib.py
{"a": fwd},
)
self.assertEqual(annotationlib.get_annotations(f2, format=2), {"a": fwd})
self.assertEqual(annotationlib.get_annotations(f2, format=3), {"a": fwd})

@larryhastings larryhastings Sep 29, 2024

Copy link
Copy Markdown
Contributor

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.

Why is format hard-coded here? I'd prefer you think of it like an enum, an arbitrary value whose actual value doesn't matter. I don't think it's necessary to externally test that the format constants are specific values; they happen to be 1, 2, 3, 4, but I don't want external users to rely on that or ever hard-code them.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Sep 29, 2024

Copy link
Copy Markdown
Member Author

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.

The intent was to test that the function accepts both the integer value and the enum. If you think it's OK to accept only the enum, I can switch to that model.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/annotationlib.py

"""
if format == Format.VALUE_WITH_FAKE_GLOBALS:
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")

@larryhastings larryhastings Sep 29, 2024

Copy link
Copy Markdown
Contributor

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.

I was surprised to see this exception. I haven't given the matter a lot of thought, and I don't have a strongly held opinion yet. Can you tell me why we should raise this exception here? (My knee-jerk instinct: "special cases aren't special enough to break the rules.")

p.s. if we do keep these assertions, please drop "The ". The text should read "VALUES_WITH_FAKE_GLOBALS format is for internal use only".

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Sep 29, 2024

Copy link
Copy Markdown
Member Author

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.

There is no meaningful value to return for VALUE_WITH_FAKE_GLOBALS; it's only useful within an annotate function.

Concretely, raising an exception here means that if a user-provided annotate function delegates to an API in annotationlib without further thought, it will raise an exception for VALUE_WITH_FAKE_GLOBALS, which is what we'd want.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_annotationlib.py

self.assertEqual(Format.FORWARDREF.value, 2)
self.assertEqual(Format.FORWARDREF, 2)
self.assertEqual(Format.VALUE_WITH_FAKE_GLOBALS.value, 2)

@larryhastings larryhastings Sep 29, 2024

Copy link
Copy Markdown
Contributor

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.

See related comments, I don't think the fact that STRING format is the int object 2 is or should be part of the interface. I don't want people hard-coding it.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Objects/typevarobject.c Outdated
}
PyObject *value = ((constevaluatorobject *)self)->value;
if (format == 3) { // STRING
if (format == 4) { // STRING

@larryhastings larryhastings Sep 29, 2024

Copy link
Copy Markdown
Contributor

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.

The formats should be defined in C too. Macros are fine. I'm expecting FORMAT_STRING here, optionally with some variant of Py_ in front. Worst case, Py_ANNOTATE_FORMAT_STRING.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Nov 20, 2024

Copy link
Copy Markdown
Member Author

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.

I added a C enum for these.

Sorry, something went wrong.

Uh oh!

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

All reactions
@JelleZijlstra
Add C enum for format
4081f8f
@JelleZijlstra JelleZijlstra mentioned this pull request Oct 15, 2024
Incorrect format passed to __annotate__ function #125507
Closed
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-fakevalue
3d382ba
iritkatriel
iritkatriel reviewed Nov 20, 2024
View reviewed changes
Comment thread Doc/library/annotationlib.rst

The exact values of these strings may change in future versions of Python.

.. attribute:: VALUE_WITH_FAKE_GLOBALS

@iritkatriel iritkatriel Sep 26, 2024

Copy link
Copy Markdown
Member

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.

Since this is internal only, maybe it should start with an _?

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Nov 26, 2024

Copy link
Copy Markdown
Member Author

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.

It's not quite internal-only, because external users who write their own __annotate__ functions need to care about this value.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Python/codegen.c Outdated
key, loc.lineno, NULL, &umd));

// if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError
PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);

@iritkatriel iritkatriel Nov 20, 2024

Copy link
Copy Markdown
Member

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.

Suggested change
PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);
PyObject *fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);

Or something, but not two.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JelleZijlstra JelleZijlstra Nov 26, 2024

Copy link
Copy Markdown
Member Author

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.

Good point, changing to value_with_fake_globals.

Sorry, something went wrong.

Uh oh!

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

All reactions
larryhastings
larryhastings approved these changes Nov 26, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Nov 26, 2024
JelleZijlstra added 2 commits November 26, 2024 07:12
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into pep649-fakevalue
10df40d
@JelleZijlstra
Rename poorly named variable
718205f
@JelleZijlstra
JelleZijlstra enabled auto-merge (squash) November 26, 2024 15:30
@JelleZijlstra
JelleZijlstra merged commit dcf6292 into python:main Nov 26, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 26, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
@JelleZijlstra @ebonnal
pythongh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib (p…
d24437b
…ython#124415)
@Daraan Daraan mentioned this pull request Apr 1, 2025
Unit tests fails under Python 3.14 python/typing_extensions#564
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel left review comments
@larryhastings larryhastings larryhastings approved these changes
@AlexWaygood AlexWaygood Awaiting requested review from AlexWaygood AlexWaygood is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

@JelleZijlstra @iritkatriel @larryhastings
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.