Skip to content
Merged
Prev Previous commit
Next Next commit
Make it a static type
  • Loading branch information
JelleZijlstra committed May 18, 2023
commit dbcbc66398619e5b107caf2a12a16593c3421b76
1 change: 0 additions & 1 deletion Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ struct _Py_interp_cached_objects {
PyTypeObject *paramspec_type;
PyTypeObject *paramspecargs_type;
PyTypeObject *paramspeckwargs_type;
PyTypeObject *typealias_type;
};

#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_typevarobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern PyObject *_Py_subscript_generic(PyThreadState *, PyObject *);
extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);

extern PyTypeObject _PyTypeAlias_Type;

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def test_union(self):
self.assertEqual(get_args(union3), (list[range], Alias1))

def test_module(self):
self.assertEqual(TypeAliasType.__module__, "typing")
type Alias = int
self.assertEqual(Alias.__module__, __name__)
self.assertEqual(mod_generics_cache.Alias.__module__,
Expand Down
5 changes: 4 additions & 1 deletion Modules/_typingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Python.h"
#include "internal/pycore_interp.h"
#include "internal/pycore_typevarobject.h"
#include "clinic/_typingmodule.c.h"

/*[clinic input]
Expand Down Expand Up @@ -56,9 +57,11 @@ _typing_exec(PyObject *m)
EXPORT_TYPE("ParamSpec", paramspec_type);
EXPORT_TYPE("ParamSpecArgs", paramspecargs_type);
EXPORT_TYPE("ParamSpecKwargs", paramspeckwargs_type);
EXPORT_TYPE("TypeAliasType", typealias_type);
EXPORT_TYPE("Generic", generic_type);
#undef EXPORT_TYPE
if (PyModule_AddObjectRef(m, "TypeAliasType", &_PyTypeAlias_Type) < 0) {
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
return -1;
}
return 0;
}

Expand Down
3 changes: 2 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_symtable.h" // PySTEntry_Type
#include "pycore_typevarobject.h" // _PyTypeVar_Type etc., _Py_initialize_generic
#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
#include "pycore_typeobject.h" // _PyBufferWrapper_Type
#include "pycore_unionobject.h" // _PyUnion_Type
#include "pycore_interpreteridobject.h" // _PyInterpreterID_Type
Expand Down Expand Up @@ -2112,6 +2112,7 @@ static PyTypeObject* static_types[] = {
&_PyWeakref_CallableProxyType,
&_PyWeakref_ProxyType,
&_PyWeakref_RefType,
&_PyTypeAlias_Type,

// subclasses: _PyTypes_FiniTypes() deallocates them before their base
// class
Expand Down
49 changes: 25 additions & 24 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,8 +1337,7 @@ static typealiasobject *
typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
PyObject *value, PyObject *module)
{
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typealias_type;
typealiasobject *ta = PyObject_GC_New(typealiasobject, tp);
typealiasobject *ta = PyObject_GC_New(typealiasobject, &_PyTypeAlias_Type);
if (ta == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1439,28 +1438,32 @@ Type aliases are created through the type statement:\n\
type Alias = int\n\
");

static PyType_Slot typealias_slots[] = {
{Py_tp_doc, (void *)typealias_doc},
{Py_tp_members, typealias_members},
{Py_tp_methods, typealias_methods},
{Py_tp_getset, typealias_getset},
{Py_mp_subscript, typealias_subscript},
{Py_tp_dealloc, typealias_dealloc},
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, typealias_new},
{Py_tp_free, PyObject_GC_Del},
{Py_tp_traverse, (traverseproc)typealias_traverse},
{Py_tp_clear, (inquiry)typealias_clear},
{Py_tp_repr, typealias_repr},
{Py_nb_or, _Py_union_type_or},
{0, 0},
static PyNumberMethods typealias_as_number = {
.nb_or = _Py_union_type_or,
};

static PyMappingMethods typealias_as_mapping = {
.mp_subscript = typealias_subscript,
};

PyType_Spec typealias_spec = {
.name = "typing.TypeAliasType",
.basicsize = sizeof(typealiasobject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
.slots = typealias_slots,
PyTypeObject _PyTypeAlias_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "typing.TypeAliasType",
.tp_basicsize = sizeof(typealiasobject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
.tp_doc = typealias_doc,
.tp_members = typealias_members,
.tp_methods = typealias_methods,
.tp_getset = typealias_getset,
.tp_alloc = PyType_GenericAlloc,
.tp_dealloc = typealias_dealloc,
.tp_new = typealias_new,
.tp_free = PyObject_GC_Del,
.tp_traverse = (traverseproc)typealias_traverse,
.tp_clear = (inquiry)typealias_clear,
.tp_repr = typealias_repr,
.tp_as_number = &typealias_as_number,
.tp_as_mapping = &typealias_as_mapping,
};

PyObject *
Expand Down Expand Up @@ -1631,7 +1634,6 @@ int _Py_initialize_generic(PyInterpreterState *interp)
MAKE_TYPE(paramspec);
MAKE_TYPE(paramspecargs);
MAKE_TYPE(paramspeckwargs);
MAKE_TYPE(typealias);
#undef MAKE_TYPE
return 0;
}
Expand All @@ -1644,5 +1646,4 @@ void _Py_clear_generic_types(PyInterpreterState *interp)
Py_CLEAR(interp->cached_objects.paramspec_type);
Py_CLEAR(interp->cached_objects.paramspecargs_type);
Py_CLEAR(interp->cached_objects.paramspeckwargs_type);
Py_CLEAR(interp->cached_objects.typealias_type);
}
7 changes: 4 additions & 3 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// types.UnionType -- used to represent e.g. Union[int, str], int | str
#include "Python.h"
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
#include "pycore_typevarobject.h" // _PyTypeAlias_Type
#include "pycore_unionobject.h"
#include "structmember.h"

Expand Down Expand Up @@ -150,11 +151,11 @@ is_unionable(PyObject *obj)
if (obj == Py_None ||
PyType_Check(obj) ||
_PyGenericAlias_Check(obj) ||
_PyUnion_Check(obj)) {
_PyUnion_Check(obj) ||
Py_IS_TYPE(obj, &_PyTypeAlias_Type)) {
return 1;
}
PyInterpreterState *interp = PyInterpreterState_Get();
return Py_IS_TYPE(obj, interp->cached_objects.typealias_type);
return 0;
}

PyObject *
Expand Down