Skip to content

Commit

Permalink
pythongh-104549: Set __module__ on TypeAliasType
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed May 16, 2023
1 parent 24d8b88 commit 98cc5fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Lib/test/test_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,22 @@ def test_basic(self):
self.assertEqual(TA.__name__, "TA")
self.assertIs(TA.__value__, int)
self.assertEqual(TA.__type_params__, ())
self.assertEqual(TA.__module__, __name__)

def test_generic(self):
T = TypeVar("T")
TA = TypeAliasType("TA", list[T], type_params=(T,))
self.assertEqual(TA.__name__, "TA")
self.assertEqual(TA.__value__, list[T])
self.assertEqual(TA.__type_params__, (T,))
self.assertEqual(TA.__module__, __name__)

def test_keywords(self):
TA = TypeAliasType(name="TA", value=int)
self.assertEqual(TA.__name__, "TA")
self.assertIs(TA.__value__, int)
self.assertEqual(TA.__type_params__, ())
self.assertEqual(TA.__module__, __name__)

def test_errors(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -202,3 +205,7 @@ def test_union(self):
union3 = list[range] | Alias1
self.assertIsInstance(union3, types.UnionType)
self.assertEqual(get_args(union3), (list[range], Alias1))

def test_module(self):
type Alias = int
self.assertEqual(Alias.__module__, __name__)
33 changes: 30 additions & 3 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct {
PyObject *type_params;
PyObject *compute_value;
PyObject *value;
PyObject *module;
} typealiasobject;

#include "clinic/typevarobject.c.h"
Expand Down Expand Up @@ -1252,6 +1253,7 @@ typealias_dealloc(PyObject *self)
Py_XDECREF(ta->type_params);
Py_XDECREF(ta->compute_value);
Py_XDECREF(ta->value);
Py_XDECREF(ta->module);
Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1309,16 +1311,30 @@ typealias_type_params(PyObject *self, void *unused)
return Py_NewRef(ta->type_params);
}

static PyObject *
typealias_module(PyObject *self, void *unused)
{
typealiasobject *ta = (typealiasobject *)self;
if (ta->module != NULL) {
return Py_NewRef(ta->module);
}
if (ta->compute_value != NULL) {
return PyFunction_GetModule(ta->compute_value);
}
Py_RETURN_NONE;
}

static PyGetSetDef typealias_getset[] = {
{"__parameters__", typealias_parameters, (setter)NULL, NULL, NULL},
{"__type_params__", typealias_type_params, (setter)NULL, NULL, NULL},
{"__value__", typealias_value, (setter)NULL, NULL, NULL},
{"__module__", typealias_module, (setter)NULL, NULL, NULL},
{0}
};

static typealiasobject *
typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
PyObject *value)
PyObject *value, PyObject *module)
{
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typealias_type;
typealiasobject *ta = PyObject_GC_New(typealiasobject, tp);
Expand All @@ -1329,6 +1345,7 @@ typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
ta->type_params = Py_IsNone(type_params) ? NULL : Py_XNewRef(type_params);
ta->compute_value = Py_XNewRef(compute_value);
ta->value = Py_XNewRef(value);
ta->module = Py_XNewRef(module);
_PyObject_GC_TRACK(ta);
return ta;
}
Expand All @@ -1339,6 +1356,7 @@ typealias_traverse(typealiasobject *self, visitproc visit, void *arg)
Py_VISIT(self->type_params);
Py_VISIT(self->compute_value);
Py_VISIT(self->value);
Py_VISIT(self->module);
return 0;
}

Expand All @@ -1348,6 +1366,7 @@ typealias_clear(typealiasobject *self)
Py_CLEAR(self->type_params);
Py_CLEAR(self->compute_value);
Py_CLEAR(self->value);
Py_CLEAR(self->module);
return 0;
}

Expand Down Expand Up @@ -1401,7 +1420,14 @@ typealias_new_impl(PyTypeObject *type, PyObject *name, PyObject *value,
PyErr_SetString(PyExc_TypeError, "type_params must be a tuple");
return NULL;
}
return (PyObject *)typealias_alloc(name, type_params, NULL, value);
PyObject *module = caller();
if (module == NULL) {
return NULL;
}
PyObject *ta = (PyObject *)typealias_alloc(name, type_params, NULL, value,
module);
Py_DECREF(module);
return ta;
}

PyDoc_STRVAR(typealias_doc,
Expand Down Expand Up @@ -1445,7 +1471,8 @@ _Py_make_typealias(PyThreadState* unused, PyObject *args)
assert(PyUnicode_Check(name));
PyObject *type_params = PyTuple_GET_ITEM(args, 1);
PyObject *compute_value = PyTuple_GET_ITEM(args, 2);
return (PyObject *)typealias_alloc(name, type_params, compute_value, NULL);
assert(PyFunction_Check(compute_value));
return (PyObject *)typealias_alloc(name, type_params, compute_value, NULL, NULL);
}

PyDoc_STRVAR(generic_doc,
Expand Down

0 comments on commit 98cc5fa

Please sign in to comment.