Skip to content

Commit dbcbc66

Browse files
committed
Make it a static type
1 parent 90a846c commit dbcbc66

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

Include/internal/pycore_global_objects.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ struct _Py_interp_cached_objects {
7575
PyTypeObject *paramspec_type;
7676
PyTypeObject *paramspecargs_type;
7777
PyTypeObject *paramspeckwargs_type;
78-
PyTypeObject *typealias_type;
7978
};
8079

8180
#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \

Include/internal/pycore_typevarobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern PyObject *_Py_subscript_generic(PyThreadState *, PyObject *);
1616
extern int _Py_initialize_generic(PyInterpreterState *);
1717
extern void _Py_clear_generic_types(PyInterpreterState *);
1818

19+
extern PyTypeObject _PyTypeAlias_Type;
20+
1921
#ifdef __cplusplus
2022
}
2123
#endif

Lib/test/test_type_aliases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ def test_union(self):
209209
self.assertEqual(get_args(union3), (list[range], Alias1))
210210

211211
def test_module(self):
212+
self.assertEqual(TypeAliasType.__module__, "typing")
212213
type Alias = int
213214
self.assertEqual(Alias.__module__, __name__)
214215
self.assertEqual(mod_generics_cache.Alias.__module__,

Modules/_typingmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Python.h"
88
#include "internal/pycore_interp.h"
9+
#include "internal/pycore_typevarobject.h"
910
#include "clinic/_typingmodule.c.h"
1011

1112
/*[clinic input]
@@ -56,9 +57,11 @@ _typing_exec(PyObject *m)
5657
EXPORT_TYPE("ParamSpec", paramspec_type);
5758
EXPORT_TYPE("ParamSpecArgs", paramspecargs_type);
5859
EXPORT_TYPE("ParamSpecKwargs", paramspeckwargs_type);
59-
EXPORT_TYPE("TypeAliasType", typealias_type);
6060
EXPORT_TYPE("Generic", generic_type);
6161
#undef EXPORT_TYPE
62+
if (PyModule_AddObjectRef(m, "TypeAliasType", &_PyTypeAlias_Type) < 0) {
63+
return -1;
64+
}
6265
return 0;
6366
}
6467

Objects/object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
1515
#include "pycore_pystate.h" // _PyThreadState_GET()
1616
#include "pycore_symtable.h" // PySTEntry_Type
17-
#include "pycore_typevarobject.h" // _PyTypeVar_Type etc., _Py_initialize_generic
17+
#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
1818
#include "pycore_typeobject.h" // _PyBufferWrapper_Type
1919
#include "pycore_unionobject.h" // _PyUnion_Type
2020
#include "pycore_interpreteridobject.h" // _PyInterpreterID_Type
@@ -2112,6 +2112,7 @@ static PyTypeObject* static_types[] = {
21122112
&_PyWeakref_CallableProxyType,
21132113
&_PyWeakref_ProxyType,
21142114
&_PyWeakref_RefType,
2115+
&_PyTypeAlias_Type,
21152116

21162117
// subclasses: _PyTypes_FiniTypes() deallocates them before their base
21172118
// class

Objects/typevarobject.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,8 +1337,7 @@ static typealiasobject *
13371337
typealias_alloc(PyObject *name, PyObject *type_params, PyObject *compute_value,
13381338
PyObject *value, PyObject *module)
13391339
{
1340-
PyTypeObject *tp = PyInterpreterState_Get()->cached_objects.typealias_type;
1341-
typealiasobject *ta = PyObject_GC_New(typealiasobject, tp);
1340+
typealiasobject *ta = PyObject_GC_New(typealiasobject, &_PyTypeAlias_Type);
13421341
if (ta == NULL) {
13431342
return NULL;
13441343
}
@@ -1439,28 +1438,32 @@ Type aliases are created through the type statement:\n\
14391438
type Alias = int\n\
14401439
");
14411440

1442-
static PyType_Slot typealias_slots[] = {
1443-
{Py_tp_doc, (void *)typealias_doc},
1444-
{Py_tp_members, typealias_members},
1445-
{Py_tp_methods, typealias_methods},
1446-
{Py_tp_getset, typealias_getset},
1447-
{Py_mp_subscript, typealias_subscript},
1448-
{Py_tp_dealloc, typealias_dealloc},
1449-
{Py_tp_alloc, PyType_GenericAlloc},
1450-
{Py_tp_new, typealias_new},
1451-
{Py_tp_free, PyObject_GC_Del},
1452-
{Py_tp_traverse, (traverseproc)typealias_traverse},
1453-
{Py_tp_clear, (inquiry)typealias_clear},
1454-
{Py_tp_repr, typealias_repr},
1455-
{Py_nb_or, _Py_union_type_or},
1456-
{0, 0},
1441+
static PyNumberMethods typealias_as_number = {
1442+
.nb_or = _Py_union_type_or,
1443+
};
1444+
1445+
static PyMappingMethods typealias_as_mapping = {
1446+
.mp_subscript = typealias_subscript,
14571447
};
14581448

1459-
PyType_Spec typealias_spec = {
1460-
.name = "typing.TypeAliasType",
1461-
.basicsize = sizeof(typealiasobject),
1462-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
1463-
.slots = typealias_slots,
1449+
PyTypeObject _PyTypeAlias_Type = {
1450+
PyVarObject_HEAD_INIT(&PyType_Type, 0)
1451+
.tp_name = "typing.TypeAliasType",
1452+
.tp_basicsize = sizeof(typealiasobject),
1453+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
1454+
.tp_doc = typealias_doc,
1455+
.tp_members = typealias_members,
1456+
.tp_methods = typealias_methods,
1457+
.tp_getset = typealias_getset,
1458+
.tp_alloc = PyType_GenericAlloc,
1459+
.tp_dealloc = typealias_dealloc,
1460+
.tp_new = typealias_new,
1461+
.tp_free = PyObject_GC_Del,
1462+
.tp_traverse = (traverseproc)typealias_traverse,
1463+
.tp_clear = (inquiry)typealias_clear,
1464+
.tp_repr = typealias_repr,
1465+
.tp_as_number = &typealias_as_number,
1466+
.tp_as_mapping = &typealias_as_mapping,
14641467
};
14651468

14661469
PyObject *
@@ -1631,7 +1634,6 @@ int _Py_initialize_generic(PyInterpreterState *interp)
16311634
MAKE_TYPE(paramspec);
16321635
MAKE_TYPE(paramspecargs);
16331636
MAKE_TYPE(paramspeckwargs);
1634-
MAKE_TYPE(typealias);
16351637
#undef MAKE_TYPE
16361638
return 0;
16371639
}
@@ -1644,5 +1646,4 @@ void _Py_clear_generic_types(PyInterpreterState *interp)
16441646
Py_CLEAR(interp->cached_objects.paramspec_type);
16451647
Py_CLEAR(interp->cached_objects.paramspecargs_type);
16461648
Py_CLEAR(interp->cached_objects.paramspeckwargs_type);
1647-
Py_CLEAR(interp->cached_objects.typealias_type);
16481649
}

Objects/unionobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// types.UnionType -- used to represent e.g. Union[int, str], int | str
22
#include "Python.h"
33
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
4+
#include "pycore_typevarobject.h" // _PyTypeAlias_Type
45
#include "pycore_unionobject.h"
56
#include "structmember.h"
67

@@ -150,11 +151,11 @@ is_unionable(PyObject *obj)
150151
if (obj == Py_None ||
151152
PyType_Check(obj) ||
152153
_PyGenericAlias_Check(obj) ||
153-
_PyUnion_Check(obj)) {
154+
_PyUnion_Check(obj) ||
155+
Py_IS_TYPE(obj, &_PyTypeAlias_Type)) {
154156
return 1;
155157
}
156-
PyInterpreterState *interp = PyInterpreterState_Get();
157-
return Py_IS_TYPE(obj, interp->cached_objects.typealias_type);
158+
return 0;
158159
}
159160

160161
PyObject *

0 commit comments

Comments
 (0)