Skip to content

Commit

Permalink
Change the values
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 25, 2024
1 parent 5bb1d1e commit ab05089
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

class Format(enum.IntEnum):
VALUE = 1
FORWARDREF = 2
SOURCE = 3
VALUE_WITH_FAKE_GLOBALS = 4
VALUE_WITH_FAKE_GLOBALS = 2
FORWARDREF = 3
SOURCE = 4


_Union = None
Expand Down
22 changes: 11 additions & 11 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ def test_enum(self):
self.assertEqual(annotationlib.Format.VALUE.value, 1)
self.assertEqual(annotationlib.Format.VALUE, 1)

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

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

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


class TestForwardRefFormat(unittest.TestCase):
Expand Down Expand Up @@ -370,16 +373,13 @@ def f2(a: undefined):
annotationlib.get_annotations(f2, format=annotationlib.Format.FORWARDREF),
{"a": fwd},
)
self.assertEqual(annotationlib.get_annotations(f2, format=2), {"a": fwd})
self.assertEqual(annotationlib.get_annotations(f2, format=3), {"a": fwd})

self.assertEqual(
annotationlib.get_annotations(f1, format=annotationlib.Format.SOURCE),
{"a": "int"},
)
self.assertEqual(annotationlib.get_annotations(f1, format=3), {"a": "int"})

with self.assertRaises(ValueError):
annotationlib.get_annotations(f1, format=0)
self.assertEqual(annotationlib.get_annotations(f1, format=4), {"a": "int"})

with self.assertRaises(ValueError):
annotationlib.get_annotations(f1, format=42)
Expand All @@ -394,7 +394,7 @@ def f2(a: undefined):
ValueError,
r"The VALUE_WITH_FAKE_GLOBALS format is for internal use only",
):
annotationlib.get_annotations(f1, format=4)
annotationlib.get_annotations(f1, format=2)

def test_custom_object_with_annotations(self):
class C:
Expand Down Expand Up @@ -852,7 +852,7 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
class TestCallEvaluateFunction(unittest.TestCase):
def test_evaluation(self):
def evaluate(format, exc=NotImplementedError):
if format != 1 and format != 4:
if format > 2:
raise exc
return undefined

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def test_module(self):
ns = run_code("x: undefined = 1")
anno = ns["__annotate__"]
with self.assertRaises(NotImplementedError):
anno(2)
anno(3)

with self.assertRaises(NameError):
anno(1)
Expand Down Expand Up @@ -376,7 +376,7 @@ class X:
annotate(annotationlib.Format.FORWARDREF)
with self.assertRaises(NotImplementedError):
annotate(annotationlib.Format.SOURCE)
with self.assertRaises(NotImplementedError):
with self.assertRaises(TypeError):
annotate(None)
self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int})

Expand Down
14 changes: 4 additions & 10 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,21 +655,15 @@ codegen_setup_annotations_scope(compiler *c, location loc,
codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS,
key, loc.lineno, NULL, &umd));

// if .format != 1 and .format != 4: raise NotImplementedError
// if .format > 2: raise NotImplementedError
_Py_DECLARE_STR(format, ".format");
PyObject *two = PyLong_FromLong(2);
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST(c, loc, _PyLong_GetOne());
ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]);
ADDOP_LOAD_CONST(c, loc, two);
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
NEW_JUMP_TARGET_LABEL(c, body);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);

ADDOP_I(c, loc, LOAD_FAST, 0);
PyObject *four = PyLong_FromLong(4);
assert(four != NULL);
ADDOP_LOAD_CONST(c, loc, four);
ADDOP_I(c, loc, COMPARE_OP, (Py_NE << 5) | compare_masks[Py_NE]);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);

ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);
ADDOP_I(c, loc, RAISE_VARARGS, 1);
USE_LABEL(c, body);
Expand Down

0 comments on commit ab05089

Please sign in to comment.