Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,12 @@ without the dedicated syntax, as documented below.
>>> Alias.__module__
'__main__'

This attribute is writable.

.. versionchanged:: 3.15

The attribute is now writable.

.. attribute:: __type_params__

The type parameters of the type alias, or an empty tuple if the alias is
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ def test_module(self):
mod_generics_cache.__name__)
self.assertEqual(mod_generics_cache.OldStyle.__module__,
mod_generics_cache.__name__)
Alias.__module__ = "ham.spam.eggs"
self.assertEqual(Alias.__module__, "ham.spam.eggs")

def test_unpack(self):
type Alias = tuple[int, int]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow assignment to the ``__module__`` attribute of
:class:`typing.TypeAliasType` instances.
26 changes: 22 additions & 4 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1983,8 +1983,12 @@
typealias_module(PyObject *self, void *Py_UNUSED(closure))
{
typealiasobject *ta = typealiasobject_CAST(self);
if (ta->module != NULL) {
return Py_NewRef(ta->module);
PyObject *module;
Py_BEGIN_CRITICAL_SECTION(self);
module = Py_XNewRef(ta->module);
Py_END_CRITICAL_SECTION();
if (module != NULL) {
return module;
}
if (ta->compute_value != NULL) {
PyObject* mod = PyFunction_GetModule(ta->compute_value);
Expand All @@ -1998,12 +2002,24 @@
Py_RETURN_NONE;
}

static int
typealias_set_module(PyObject *self, PyObject *value, void *Py_UNUSED(closure))
{
typealiasobject *ta = typealiasobject_CAST(self);
Py_BEGIN_CRITICAL_SECTION(self);
old = ta->module;

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'=': 'int' differs in levels of indirection from 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'=': 'int' differs in levels of indirection from 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'=': 'int' differs in levels of indirection from 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'=': 'int' differs in levels of indirection from 'PyObject *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'=': 'int' differs in levels of indirection from 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'old': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'=': 'int' differs in levels of indirection from 'PyObject *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2010 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'old': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
ta->module = Py_XNewRef(value);
Py_END_CRITICAL_SECTION();
Py_XDECREF(old);

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

'old': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

'old': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 2013 in Objects/typevarobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

'old': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
return 0;
}

static PyGetSetDef typealias_getset[] = {
{"__parameters__", typealias_parameters, NULL, NULL, NULL},
{"__type_params__", typealias_type_params, NULL, NULL, NULL},
{"__value__", typealias_value, NULL, NULL, NULL},
{"evaluate_value", typealias_evaluate_value, NULL, NULL, NULL},
{"__module__", typealias_module, NULL, NULL, NULL},
{"__module__", typealias_module, typealias_set_module, NULL, NULL},
{0}
};

Expand Down Expand Up @@ -2203,7 +2219,9 @@
At runtime, Alias is an instance of TypeAliasType. The __name__\n\
attribute holds the name of the type alias. The value of the type alias\n\
is stored in the __value__ attribute. It is evaluated lazily, so the\n\
value is computed only if the attribute is accessed.\n\
value is computed only if the attribute is accessed. The __module__\n\
attribute holds the name of the module in which the type alias was\n\
defined; it can be assigned to.\n\
\n\
Type aliases can also be generic::\n\
\n\
Expand Down
Loading