8000 gh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib by JelleZijlstra · Pull Request #124415 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119180: Add VALUE_WITH_FAKE_GLOBALS format to annotationlib #124415

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 12 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into pep649-fakevalue
  • Loading branch information
JelleZijlstra authored Sep 28, 2024
commit 80818c62da6aa1548e37744d6e5c71aac5f2818a
4 changes: 2 additions & 2 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Format(enum.IntEnum):
VALUE = 1
VALUE_WITH_FAKE_GLOBALS = 2
FORWARDREF = 3
SOURCE = 4
STRING = 4


_Union = None
Expand Down Expand Up @@ -710,7 +710,7 @@ def get_annotations(
return ann
# But if we didn't get it, we use __annotations__ instead.
ann = _get_dunder_annotations(obj)
return annotations_to_source(ann)
return annotations_to_string(ann)
case Format.VALUE_WITH_FAKE_GLOBALS:
raise ValueError("The VALUE_WITH_FAKE_GLOBALS format is for internal use only")
case _:
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def test_enum(self):
self.assertEqual(Format.VALUE.value, 1)
self.assertEqual(Format.VALUE, 1)

self.assertEqual(annotationlib.Format.VALUE_WITH_FAKE_GLOBALS.value, 2)
self.assertEqual(annotationlib.Format.VALUE_WITH_FAKE_GLOBALS, 2)
self.assertEqual(Format.VALUE_WITH_FAKE_GLOBALS.value, 2)
Copy link
Contributor

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.

self.assertEqual(Format.VALUE_WITH_FAKE_GLOBALS, 2)

self.assertEqual(annotationlib.Format.FORWARDREF.value, 3)
self.assertEqual(annotationlib.Format.FORWARDREF, 3)
self.assertEqual(Format.FORWARDREF.value, 3)
self.assertEqual(Format.FORWARDREF, 3)

self.assertEqual(annotationlib.Format.SOURCE.value, 4)
self.assertEqual(annotationlib.Format.SOURCE, 4)
self.assertEqual(Format.STRING.value, 4)
self.assertEqual(Format.STRING, 4)


class TestForwardRefFormat(unittest.TestCase):
Expand Down Expand Up @@ -470,7 +470,7 @@ def foo(a: int, b: str):
)

foo.__annotations__ = {"a": "foo", "b": "str"}
for format in annotationlib.Format:
for format in Format:
if format is Format.VALUE_WITH_FAKE_GLOBALS:
continue
with self.subTest(format=format):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class X:
with self.assertRaises(NotImplementedError):
annotate(annotationlib.Format.FORWARDREF)
with self.assertRaises(NotImplementedError):
annotate(annotationlib.Format.SOURCE)
annotate(annotationlib.Format.STRING)
with self.assertRaises(TypeError):
annotate(None)
self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int})
Expand Down
8 changes: 4 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2937,8 +2937,8 @@ def annotate(format):
match format:
case annotationlib.Format.VALUE | annotationlib.Format.FORWARDREF:
return checked_types
case annotationlib.Format.SOURCE:
return annotationlib.annotations_to_source(types)
case annotationlib.Format.STRING:
return annotationlib.annotations_to_string(types)
case _:
raise NotImplementedError(format)
return annotate
Expand Down Expand Up @@ -3228,8 +3228,8 @@ def __annotate__(format):
n: _type_check(tp, msg, module=tp_dict.__module__)
for n, tp in own.items()
}
elif format == annotationlib.Format.SOURCE:
own = annotationlib.annotations_to_source(own_annotations)
elif format == annotationlib.Format.STRING:
own = annotationlib.annotations_to_string(own_annotations)
elif format in (annotationlib.Format.FORWARDREF, annotationlib.Format.VALUE):
own = own_checked_annotations
else:
Expand Down
2 changes: 1 addition & 1 deletion Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
return NULL;
}
PyObject *value = ((constevaluatorobject *)self)->value;
if (format == 4) { // SOURCE
if (format == 4) { // STRING
Copy link
Contributor

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.

Copy link
Member Author

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.

PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5
if (writer == NULL) {
return NULL;
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0