GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib by JelleZijlstra · Pull Request #124415 · python/cpython Conversation
|
| The exact values of these strings may change in future versions of Python. | ||
|
|
||
| .. attribute:: VALUE_WITH_FAKE_GLOBALS | ||
| :value: 4 |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
| {"a": fwd}, | ||
| ) | ||
| self.assertEqual(annotationlib.get_annotations(f2, format=2), {"a": fwd}) | ||
| self.assertEqual(annotationlib.get_annotations(f2, format=3), {"a": fwd}) |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
|
|
||
| """ | ||
| if format == Format.VALUE_WITH_FAKE_GLOBALS: | ||
| raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only") |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
|
|
||
| self.assertEqual(Format.FORWARDREF.value, 2) | ||
| self.assertEqual(Format.FORWARDREF, 2) | ||
| self.assertEqual(Format.VALUE_WITH_FAKE_GLOBALS.value, 2) |
There was a problem 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.
There was an error while loading. Please reload this page.
| } | ||
| PyObject *value = ((constevaluatorobject *)self)->value; | ||
| if (format == 3) { // STRING | ||
| if (format == 4) { // STRING |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
|
|
||
| The exact values of these strings may change in future versions of Python. | ||
|
|
||
| .. attribute:: VALUE_WITH_FAKE_GLOBALS |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
| key, loc.lineno, NULL, &umd)); | ||
|
|
||
| // if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError | ||
| PyObject *two = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.