mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)
This commit is contained in:
		
							parent
							
								
									33986465bd
								
							
						
					
					
						commit
						d01628e411
					
				
					 5 changed files with 24 additions and 14 deletions
				
			
		|  | @ -7,6 +7,7 @@ | |||
| import builtins | ||||
| import functools | ||||
| import _thread | ||||
| from types import GenericAlias | ||||
| 
 | ||||
| 
 | ||||
| __all__ = ['dataclass', | ||||
|  | @ -284,6 +285,8 @@ def __set_name__(self, owner, name): | |||
|             # it. | ||||
|             func(self.default, owner, name) | ||||
| 
 | ||||
|     __class_getitem__ = classmethod(GenericAlias) | ||||
| 
 | ||||
| 
 | ||||
| class _DataclassParams: | ||||
|     __slots__ = ('init', | ||||
|  |  | |||
|  | @ -18,6 +18,7 @@ | |||
| import email.generator | ||||
| import io | ||||
| import contextlib | ||||
| from types import GenericAlias | ||||
| try: | ||||
|     import fcntl | ||||
| except ImportError: | ||||
|  | @ -260,6 +261,8 @@ def _dump_message(self, message, target, mangle_from_=False): | |||
|         else: | ||||
|             raise TypeError('Invalid message type: %s' % type(message)) | ||||
| 
 | ||||
|     __class_getitem__ = classmethod(GenericAlias) | ||||
| 
 | ||||
| 
 | ||||
| class Maildir(Mailbox): | ||||
|     """A qmail-style Maildir mailbox.""" | ||||
|  | @ -2015,6 +2018,8 @@ def closed(self): | |||
|             return False | ||||
|         return self._file.closed | ||||
| 
 | ||||
|     __class_getitem__ = classmethod(GenericAlias) | ||||
| 
 | ||||
| 
 | ||||
| class _PartialFile(_ProxyFile): | ||||
|     """A read-only wrapper of part of a file.""" | ||||
|  |  | |||
|  | @ -358,10 +358,6 @@ def sub(num): | |||
|             tp.shutdown() | ||||
|         self.assertEqual(results, list(range(10))) | ||||
| 
 | ||||
|     def test_contextvar_getitem(self): | ||||
|         clss = contextvars.ContextVar | ||||
|         self.assertEqual(clss[str], clss) | ||||
| 
 | ||||
| 
 | ||||
| # HAMT Tests | ||||
| 
 | ||||
|  |  | |||
|  | @ -9,7 +9,10 @@ | |||
| from concurrent.futures import Future | ||||
| from concurrent.futures.thread import _WorkItem | ||||
| from contextlib import AbstractContextManager, AbstractAsyncContextManager | ||||
| from functools import partial, partialmethod, _lru_cache_wrapper, cached_property | ||||
| from contextvars import ContextVar, Token | ||||
| from dataclasses import Field | ||||
| from functools import partial, partialmethod, cached_property | ||||
| from mailbox import Mailbox, _PartialFile | ||||
| from ctypes import Array, LibraryLoader | ||||
| from difflib import SequenceMatcher | ||||
| from filecmp import dircmp | ||||
|  | @ -60,6 +63,9 @@ def test_subscriptable(self): | |||
|                   Reversible, | ||||
|                   Container, Collection, | ||||
|                   Callable, | ||||
|                   Mailbox, _PartialFile, | ||||
|                   ContextVar, Token, | ||||
|                   Field, | ||||
|                   Set, MutableSet, | ||||
|                   Mapping, MutableMapping, MappingView, | ||||
|                   KeysView, ItemsView, ValuesView, | ||||
|  |  | |||
|  | @ -1024,13 +1024,6 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token) | |||
| } | ||||
| 
 | ||||
| 
 | ||||
| static PyObject * | ||||
| contextvar_cls_getitem(PyObject *self, PyObject *arg) | ||||
| { | ||||
|     Py_INCREF(self); | ||||
|     return self; | ||||
| } | ||||
| 
 | ||||
| static PyMemberDef PyContextVar_members[] = { | ||||
|     {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY}, | ||||
|     {NULL} | ||||
|  | @ -1040,8 +1033,8 @@ static PyMethodDef PyContextVar_methods[] = { | |||
|     _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF | ||||
|     _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF | ||||
|     _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF | ||||
|     {"__class_getitem__", contextvar_cls_getitem, | ||||
|         METH_O | METH_CLASS, NULL}, | ||||
|     {"__class_getitem__", (PyCFunction)Py_GenericAlias, | ||||
|     METH_O|METH_CLASS,       PyDoc_STR("See PEP 585")}, | ||||
|     {NULL, NULL} | ||||
| }; | ||||
| 
 | ||||
|  | @ -1180,10 +1173,17 @@ static PyGetSetDef PyContextTokenType_getsetlist[] = { | |||
|     {NULL} | ||||
| }; | ||||
| 
 | ||||
| static PyMethodDef PyContextTokenType_methods[] = { | ||||
|     {"__class_getitem__",    (PyCFunction)Py_GenericAlias, | ||||
|     METH_O|METH_CLASS,       PyDoc_STR("See PEP 585")}, | ||||
|     {NULL} | ||||
| }; | ||||
| 
 | ||||
| PyTypeObject PyContextToken_Type = { | ||||
|     PyVarObject_HEAD_INIT(&PyType_Type, 0) | ||||
|     "Token", | ||||
|     sizeof(PyContextToken), | ||||
|     .tp_methods = PyContextTokenType_methods, | ||||
|     .tp_getset = PyContextTokenType_getsetlist, | ||||
|     .tp_dealloc = (destructor)token_tp_dealloc, | ||||
|     .tp_getattro = PyObject_GenericGetAttr, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Ethan Smith
						Ethan Smith