Skip to content
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

gh-119180: Disallow instantiation of ConstEvaluator objects #124561

Merged
merged 3 commits into from
Sep 25, 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
11 changes: 11 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,3 +1452,14 @@ def f[T: (int, str)](): pass
self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.VALUE), (int, str))
self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.FORWARDREF), (int, str))
self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.SOURCE), '(int, str)')

def test_const_evaluator(self):
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
T = TypeVar("T", bound=int)
self.assertEqual(repr(T.evaluate_bound), "<constevaluator <class 'int'>>")

ConstEvaluator = type(T.evaluate_bound)

with self.assertRaisesRegex(TypeError, r"cannot create '_typing\._ConstEvaluator' instances"):
ConstEvaluator()
with self.assertRaisesRegex(TypeError, r"cannot set 'attribute' attribute of immutable type '_typing\._ConstEvaluator'"):
ConstEvaluator.attribute = 1
3 changes: 2 additions & 1 deletion Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ static PyType_Slot constevaluator_slots[] = {
PyType_Spec constevaluator_spec = {
.name = "_typing._ConstEvaluator",
.basicsize = sizeof(constevaluatorobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_DISALLOW_INSTANTIATION,
.slots = constevaluator_slots,
};

Expand Down
Loading