mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Add CPython dict tests (#150791)
Tests:
* test_dict.py
* test_ordered_dict.py
* test_userdict.py
Minor changes were made to each test to run them inside Dynamo
One can reproduce the changes by downloading the tests from CPython and applying the diff:
```bash
for f in "test_dict" "test_ordered_dict" "test_userdict"; do
wget -O "test/dynamo/cpython/3_13/${f}.py" "https://raw.githubusercontent.com/python/cpython/refs/heads/3.13/Lib/test/${f}.py"
git apply "test/dynamo/cpython/3_13/${f}.diff"
done
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/150791
Approved by: https://github.com/zou3519
This commit is contained in:
parent
2120eeb8de
commit
bbdf469f0e
67
test/dynamo/cpython/3_13/mapping_tests.diff
Normal file
67
test/dynamo/cpython/3_13/mapping_tests.diff
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
diff --git a/test/dynamo/cpython/3_13/mapping_tests.py b/test/dynamo/cpython/3_13/mapping_tests.py
|
||||||
|
index ed89a81a6ea..eed59a68e94 100644
|
||||||
|
--- a/test/dynamo/cpython/3_13/mapping_tests.py
|
||||||
|
+++ b/test/dynamo/cpython/3_13/mapping_tests.py
|
||||||
|
@@ -1,10 +1,61 @@
|
||||||
|
+# ======= BEGIN Dynamo patch =======
|
||||||
|
+# Owner(s): ["module: dynamo"]
|
||||||
|
+
|
||||||
|
+# ruff: noqa
|
||||||
|
+# flake8: noqa
|
||||||
|
+
|
||||||
|
+import sys
|
||||||
|
+import torch
|
||||||
|
+import torch._dynamo.test_case
|
||||||
|
+import unittest
|
||||||
|
+from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
+from torch.testing._internal.common_utils import run_tests
|
||||||
|
+
|
||||||
|
+__TestCase = CPythonTestCase
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# redirect import statements
|
||||||
|
+import sys
|
||||||
|
+import importlib.abc
|
||||||
|
+
|
||||||
|
+redirect_imports = (
|
||||||
|
+ "test.mapping_tests",
|
||||||
|
+ "test.typinganndata",
|
||||||
|
+ "test.test_grammar",
|
||||||
|
+ "test.test_math",
|
||||||
|
+ "test.test_iter",
|
||||||
|
+ "test.typinganndata.ann_module",
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
+ def find_spec(self, fullname, path, target=None):
|
||||||
|
+ # Check if the import is the problematic one
|
||||||
|
+ if fullname in redirect_imports:
|
||||||
|
+ try:
|
||||||
|
+ # Attempt to import the standalone module
|
||||||
|
+ name = fullname.removeprefix("test.")
|
||||||
|
+ r = importlib.import_module(name)
|
||||||
|
+ # Redirect the module in sys.modules
|
||||||
|
+ sys.modules[fullname] = r
|
||||||
|
+ # Return a module spec from the found module
|
||||||
|
+ return importlib.util.find_spec(name)
|
||||||
|
+ except ImportError:
|
||||||
|
+ return None
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+# Add the custom finder to sys.meta_path
|
||||||
|
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# ======= END DYNAMO PATCH =======
|
||||||
|
+
|
||||||
|
# tests common to dict and UserDict
|
||||||
|
import unittest
|
||||||
|
import collections
|
||||||
|
from test.support import get_c_recursion_limit
|
||||||
|
|
||||||
|
|
||||||
|
-class BasicTestMappingProtocol(unittest.TestCase):
|
||||||
|
+class BasicTestMappingProtocol(__TestCase):
|
||||||
|
# This base class can be used to check that an object conforms to the
|
||||||
|
# mapping protocol
|
||||||
|
|
||||||
719
test/dynamo/cpython/3_13/mapping_tests.py
Normal file
719
test/dynamo/cpython/3_13/mapping_tests.py
Normal file
|
|
@ -0,0 +1,719 @@
|
||||||
|
# ======= BEGIN Dynamo patch =======
|
||||||
|
# Owner(s): ["module: dynamo"]
|
||||||
|
|
||||||
|
# ruff: noqa
|
||||||
|
# flake8: noqa
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import torch
|
||||||
|
import torch._dynamo.test_case
|
||||||
|
import unittest
|
||||||
|
from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
from torch.testing._internal.common_utils import run_tests
|
||||||
|
|
||||||
|
__TestCase = CPythonTestCase
|
||||||
|
|
||||||
|
|
||||||
|
# redirect import statements
|
||||||
|
import sys
|
||||||
|
import importlib.abc
|
||||||
|
|
||||||
|
redirect_imports = (
|
||||||
|
"test.mapping_tests",
|
||||||
|
"test.typinganndata",
|
||||||
|
"test.test_grammar",
|
||||||
|
"test.test_math",
|
||||||
|
"test.test_iter",
|
||||||
|
"test.typinganndata.ann_module",
|
||||||
|
)
|
||||||
|
|
||||||
|
class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
def find_spec(self, fullname, path, target=None):
|
||||||
|
# Check if the import is the problematic one
|
||||||
|
if fullname in redirect_imports:
|
||||||
|
try:
|
||||||
|
# Attempt to import the standalone module
|
||||||
|
name = fullname.removeprefix("test.")
|
||||||
|
r = importlib.import_module(name)
|
||||||
|
# Redirect the module in sys.modules
|
||||||
|
sys.modules[fullname] = r
|
||||||
|
# Return a module spec from the found module
|
||||||
|
return importlib.util.find_spec(name)
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Add the custom finder to sys.meta_path
|
||||||
|
sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
|
||||||
|
|
||||||
|
# ======= END DYNAMO PATCH =======
|
||||||
|
|
||||||
|
# tests common to dict and UserDict
|
||||||
|
import unittest
|
||||||
|
import collections
|
||||||
|
from test.support import get_c_recursion_limit
|
||||||
|
|
||||||
|
|
||||||
|
class BasicTestMappingProtocol(__TestCase):
|
||||||
|
# This base class can be used to check that an object conforms to the
|
||||||
|
# mapping protocol
|
||||||
|
|
||||||
|
# Functions that can be useful to override to adapt to dictionary
|
||||||
|
# semantics
|
||||||
|
type2test = None # which class is being tested (overwrite in subclasses)
|
||||||
|
|
||||||
|
def _reference(self):
|
||||||
|
"""Return a dictionary of values which are invariant by storage
|
||||||
|
in the object under test."""
|
||||||
|
return {"1": "2", "key1":"value1", "key2":(1,2,3)}
|
||||||
|
def _empty_mapping(self):
|
||||||
|
"""Return an empty mapping object"""
|
||||||
|
return self.type2test()
|
||||||
|
def _full_mapping(self, data):
|
||||||
|
"""Return a mapping object with the value contained in data
|
||||||
|
dictionary"""
|
||||||
|
x = self._empty_mapping()
|
||||||
|
for key, value in data.items():
|
||||||
|
x[key] = value
|
||||||
|
return x
|
||||||
|
|
||||||
|
def __init__(self, *args, **kw):
|
||||||
|
unittest.TestCase.__init__(self, *args, **kw)
|
||||||
|
self.reference = self._reference().copy()
|
||||||
|
|
||||||
|
# A (key, value) pair not in the mapping
|
||||||
|
key, value = self.reference.popitem()
|
||||||
|
self.other = {key:value}
|
||||||
|
|
||||||
|
# A (key, value) pair in the mapping
|
||||||
|
key, value = self.reference.popitem()
|
||||||
|
self.inmapping = {key:value}
|
||||||
|
self.reference[key] = value
|
||||||
|
|
||||||
|
def test_read(self):
|
||||||
|
# Test for read only operations on mapping
|
||||||
|
p = self._empty_mapping()
|
||||||
|
p1 = dict(p) #workaround for singleton objects
|
||||||
|
d = self._full_mapping(self.reference)
|
||||||
|
if d is p:
|
||||||
|
p = p1
|
||||||
|
#Indexing
|
||||||
|
for key, value in self.reference.items():
|
||||||
|
self.assertEqual(d[key], value)
|
||||||
|
knownkey = list(self.other.keys())[0]
|
||||||
|
self.assertRaises(KeyError, lambda:d[knownkey])
|
||||||
|
#len
|
||||||
|
self.assertEqual(len(p), 0)
|
||||||
|
self.assertEqual(len(d), len(self.reference))
|
||||||
|
#__contains__
|
||||||
|
for k in self.reference:
|
||||||
|
self.assertIn(k, d)
|
||||||
|
for k in self.other:
|
||||||
|
self.assertNotIn(k, d)
|
||||||
|
#cmp
|
||||||
|
self.assertEqual(p, p)
|
||||||
|
self.assertEqual(d, d)
|
||||||
|
self.assertNotEqual(p, d)
|
||||||
|
self.assertNotEqual(d, p)
|
||||||
|
#bool
|
||||||
|
if p: self.fail("Empty mapping must compare to False")
|
||||||
|
if not d: self.fail("Full mapping must compare to True")
|
||||||
|
# keys(), items(), iterkeys() ...
|
||||||
|
def check_iterandlist(iter, lst, ref):
|
||||||
|
self.assertTrue(hasattr(iter, '__next__'))
|
||||||
|
self.assertTrue(hasattr(iter, '__iter__'))
|
||||||
|
x = list(iter)
|
||||||
|
self.assertTrue(set(x)==set(lst)==set(ref))
|
||||||
|
check_iterandlist(iter(d.keys()), list(d.keys()),
|
||||||
|
self.reference.keys())
|
||||||
|
check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
|
||||||
|
check_iterandlist(iter(d.values()), list(d.values()),
|
||||||
|
self.reference.values())
|
||||||
|
check_iterandlist(iter(d.items()), list(d.items()),
|
||||||
|
self.reference.items())
|
||||||
|
#get
|
||||||
|
key, value = next(iter(d.items()))
|
||||||
|
knownkey, knownvalue = next(iter(self.other.items()))
|
||||||
|
self.assertEqual(d.get(key, knownvalue), value)
|
||||||
|
self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
|
||||||
|
self.assertNotIn(knownkey, d)
|
||||||
|
|
||||||
|
def test_write(self):
|
||||||
|
# Test for write operations on mapping
|
||||||
|
p = self._empty_mapping()
|
||||||
|
#Indexing
|
||||||
|
for key, value in self.reference.items():
|
||||||
|
p[key] = value
|
||||||
|
self.assertEqual(p[key], value)
|
||||||
|
for key in self.reference.keys():
|
||||||
|
del p[key]
|
||||||
|
self.assertRaises(KeyError, lambda:p[key])
|
||||||
|
p = self._empty_mapping()
|
||||||
|
#update
|
||||||
|
p.update(self.reference)
|
||||||
|
self.assertEqual(dict(p), self.reference)
|
||||||
|
items = list(p.items())
|
||||||
|
p = self._empty_mapping()
|
||||||
|
p.update(items)
|
||||||
|
self.assertEqual(dict(p), self.reference)
|
||||||
|
d = self._full_mapping(self.reference)
|
||||||
|
#setdefault
|
||||||
|
key, value = next(iter(d.items()))
|
||||||
|
knownkey, knownvalue = next(iter(self.other.items()))
|
||||||
|
self.assertEqual(d.setdefault(key, knownvalue), value)
|
||||||
|
self.assertEqual(d[key], value)
|
||||||
|
self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
|
||||||
|
self.assertEqual(d[knownkey], knownvalue)
|
||||||
|
#pop
|
||||||
|
self.assertEqual(d.pop(knownkey), knownvalue)
|
||||||
|
self.assertNotIn(knownkey, d)
|
||||||
|
self.assertRaises(KeyError, d.pop, knownkey)
|
||||||
|
default = 909
|
||||||
|
d[knownkey] = knownvalue
|
||||||
|
self.assertEqual(d.pop(knownkey, default), knownvalue)
|
||||||
|
self.assertNotIn(knownkey, d)
|
||||||
|
self.assertEqual(d.pop(knownkey, default), default)
|
||||||
|
#popitem
|
||||||
|
key, value = d.popitem()
|
||||||
|
self.assertNotIn(key, d)
|
||||||
|
self.assertEqual(value, self.reference[key])
|
||||||
|
p=self._empty_mapping()
|
||||||
|
self.assertRaises(KeyError, p.popitem)
|
||||||
|
|
||||||
|
def test_constructor(self):
|
||||||
|
self.assertEqual(self._empty_mapping(), self._empty_mapping())
|
||||||
|
|
||||||
|
def test_bool(self):
|
||||||
|
self.assertTrue(not self._empty_mapping())
|
||||||
|
self.assertTrue(self.reference)
|
||||||
|
self.assertTrue(bool(self._empty_mapping()) is False)
|
||||||
|
self.assertTrue(bool(self.reference) is True)
|
||||||
|
|
||||||
|
def test_keys(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(list(d.keys()), [])
|
||||||
|
d = self.reference
|
||||||
|
self.assertIn(list(self.inmapping.keys())[0], d.keys())
|
||||||
|
self.assertNotIn(list(self.other.keys())[0], d.keys())
|
||||||
|
self.assertRaises(TypeError, d.keys, None)
|
||||||
|
|
||||||
|
def test_values(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(list(d.values()), [])
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.values, None)
|
||||||
|
|
||||||
|
def test_items(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(list(d.items()), [])
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.items, None)
|
||||||
|
|
||||||
|
def test_len(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(len(d), 0)
|
||||||
|
|
||||||
|
def test_getitem(self):
|
||||||
|
d = self.reference
|
||||||
|
self.assertEqual(d[list(self.inmapping.keys())[0]],
|
||||||
|
list(self.inmapping.values())[0])
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.__getitem__)
|
||||||
|
|
||||||
|
def test_update(self):
|
||||||
|
# mapping argument
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update(self.other)
|
||||||
|
self.assertEqual(list(d.items()), list(self.other.items()))
|
||||||
|
|
||||||
|
# No argument
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update()
|
||||||
|
self.assertEqual(d, self._empty_mapping())
|
||||||
|
|
||||||
|
# item sequence
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update(self.other.items())
|
||||||
|
self.assertEqual(list(d.items()), list(self.other.items()))
|
||||||
|
|
||||||
|
# Iterator
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update(self.other.items())
|
||||||
|
self.assertEqual(list(d.items()), list(self.other.items()))
|
||||||
|
|
||||||
|
# FIXME: Doesn't work with UserDict
|
||||||
|
# self.assertRaises((TypeError, AttributeError), d.update, None)
|
||||||
|
self.assertRaises((TypeError, AttributeError), d.update, 42)
|
||||||
|
|
||||||
|
outerself = self
|
||||||
|
class SimpleUserDict:
|
||||||
|
def __init__(self):
|
||||||
|
self.d = outerself.reference
|
||||||
|
def keys(self):
|
||||||
|
return self.d.keys()
|
||||||
|
def __getitem__(self, i):
|
||||||
|
return self.d[i]
|
||||||
|
d.clear()
|
||||||
|
d.update(SimpleUserDict())
|
||||||
|
i1 = sorted(d.items())
|
||||||
|
i2 = sorted(self.reference.items())
|
||||||
|
self.assertEqual(i1, i2)
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
class FailingUserDict:
|
||||||
|
def keys(self):
|
||||||
|
raise Exc
|
||||||
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
||||||
|
|
||||||
|
d.clear()
|
||||||
|
|
||||||
|
class FailingUserDict:
|
||||||
|
def keys(self):
|
||||||
|
class BogonIter:
|
||||||
|
def __init__(self):
|
||||||
|
self.i = 1
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
if self.i:
|
||||||
|
self.i = 0
|
||||||
|
return 'a'
|
||||||
|
raise Exc
|
||||||
|
return BogonIter()
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return key
|
||||||
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
||||||
|
|
||||||
|
class FailingUserDict:
|
||||||
|
def keys(self):
|
||||||
|
class BogonIter:
|
||||||
|
def __init__(self):
|
||||||
|
self.i = ord('a')
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
if self.i <= ord('z'):
|
||||||
|
rtn = chr(self.i)
|
||||||
|
self.i += 1
|
||||||
|
return rtn
|
||||||
|
raise StopIteration
|
||||||
|
return BogonIter()
|
||||||
|
def __getitem__(self, key):
|
||||||
|
raise Exc
|
||||||
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
class badseq(object):
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
raise Exc()
|
||||||
|
|
||||||
|
self.assertRaises(Exc, d.update, badseq())
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, d.update, [(1, 2, 3)])
|
||||||
|
|
||||||
|
# no test_fromkeys or test_copy as both os.environ and selves don't support it
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
|
||||||
|
self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
|
||||||
|
d = self.reference
|
||||||
|
self.assertTrue(d.get(list(self.other.keys())[0]) is None)
|
||||||
|
self.assertEqual(d.get(list(self.other.keys())[0], 3), 3)
|
||||||
|
self.assertEqual(d.get(list(self.inmapping.keys())[0]),
|
||||||
|
list(self.inmapping.values())[0])
|
||||||
|
self.assertEqual(d.get(list(self.inmapping.keys())[0], 3),
|
||||||
|
list(self.inmapping.values())[0])
|
||||||
|
self.assertRaises(TypeError, d.get)
|
||||||
|
self.assertRaises(TypeError, d.get, None, None, None)
|
||||||
|
|
||||||
|
def test_setdefault(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertRaises(TypeError, d.setdefault)
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertRaises(KeyError, d.popitem)
|
||||||
|
self.assertRaises(TypeError, d.popitem, 42)
|
||||||
|
|
||||||
|
def test_pop(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
k, v = list(self.inmapping.items())[0]
|
||||||
|
d[k] = v
|
||||||
|
self.assertRaises(KeyError, d.pop, list(self.other.keys())[0])
|
||||||
|
|
||||||
|
self.assertEqual(d.pop(k), v)
|
||||||
|
self.assertEqual(len(d), 0)
|
||||||
|
|
||||||
|
self.assertRaises(KeyError, d.pop, k)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMappingProtocol(BasicTestMappingProtocol):
|
||||||
|
def test_constructor(self):
|
||||||
|
BasicTestMappingProtocol.test_constructor(self)
|
||||||
|
self.assertTrue(self._empty_mapping() is not self._empty_mapping())
|
||||||
|
self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2})
|
||||||
|
|
||||||
|
def test_bool(self):
|
||||||
|
BasicTestMappingProtocol.test_bool(self)
|
||||||
|
self.assertTrue(not self._empty_mapping())
|
||||||
|
self.assertTrue(self._full_mapping({"x": "y"}))
|
||||||
|
self.assertTrue(bool(self._empty_mapping()) is False)
|
||||||
|
self.assertTrue(bool(self._full_mapping({"x": "y"})) is True)
|
||||||
|
|
||||||
|
def test_keys(self):
|
||||||
|
BasicTestMappingProtocol.test_keys(self)
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(list(d.keys()), [])
|
||||||
|
d = self._full_mapping({'a': 1, 'b': 2})
|
||||||
|
k = d.keys()
|
||||||
|
self.assertIn('a', k)
|
||||||
|
self.assertIn('b', k)
|
||||||
|
self.assertNotIn('c', k)
|
||||||
|
|
||||||
|
def test_values(self):
|
||||||
|
BasicTestMappingProtocol.test_values(self)
|
||||||
|
d = self._full_mapping({1:2})
|
||||||
|
self.assertEqual(list(d.values()), [2])
|
||||||
|
|
||||||
|
def test_items(self):
|
||||||
|
BasicTestMappingProtocol.test_items(self)
|
||||||
|
|
||||||
|
d = self._full_mapping({1:2})
|
||||||
|
self.assertEqual(list(d.items()), [(1, 2)])
|
||||||
|
|
||||||
|
def test_contains(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertNotIn('a', d)
|
||||||
|
self.assertTrue(not ('a' in d))
|
||||||
|
self.assertTrue('a' not in d)
|
||||||
|
d = self._full_mapping({'a': 1, 'b': 2})
|
||||||
|
self.assertIn('a', d)
|
||||||
|
self.assertIn('b', d)
|
||||||
|
self.assertNotIn('c', d)
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.__contains__)
|
||||||
|
|
||||||
|
def test_len(self):
|
||||||
|
BasicTestMappingProtocol.test_len(self)
|
||||||
|
d = self._full_mapping({'a': 1, 'b': 2})
|
||||||
|
self.assertEqual(len(d), 2)
|
||||||
|
|
||||||
|
def test_getitem(self):
|
||||||
|
BasicTestMappingProtocol.test_getitem(self)
|
||||||
|
d = self._full_mapping({'a': 1, 'b': 2})
|
||||||
|
self.assertEqual(d['a'], 1)
|
||||||
|
self.assertEqual(d['b'], 2)
|
||||||
|
d['c'] = 3
|
||||||
|
d['a'] = 4
|
||||||
|
self.assertEqual(d['c'], 3)
|
||||||
|
self.assertEqual(d['a'], 4)
|
||||||
|
del d['b']
|
||||||
|
self.assertEqual(d, {'a': 4, 'c': 3})
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.__getitem__)
|
||||||
|
|
||||||
|
def test_clear(self):
|
||||||
|
d = self._full_mapping({1:1, 2:2, 3:3})
|
||||||
|
d.clear()
|
||||||
|
self.assertEqual(d, {})
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, d.clear, None)
|
||||||
|
|
||||||
|
def test_update(self):
|
||||||
|
BasicTestMappingProtocol.test_update(self)
|
||||||
|
# mapping argument
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update({1:100})
|
||||||
|
d.update({2:20})
|
||||||
|
d.update({1:1, 2:2, 3:3})
|
||||||
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
||||||
|
|
||||||
|
# no argument
|
||||||
|
d.update()
|
||||||
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
||||||
|
|
||||||
|
# keyword arguments
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update(x=100)
|
||||||
|
d.update(y=20)
|
||||||
|
d.update(x=1, y=2, z=3)
|
||||||
|
self.assertEqual(d, {"x":1, "y":2, "z":3})
|
||||||
|
|
||||||
|
# item sequence
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update([("x", 100), ("y", 20)])
|
||||||
|
self.assertEqual(d, {"x":100, "y":20})
|
||||||
|
|
||||||
|
# Both item sequence and keyword arguments
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d.update([("x", 100), ("y", 20)], x=1, y=2)
|
||||||
|
self.assertEqual(d, {"x":1, "y":2})
|
||||||
|
|
||||||
|
# iterator
|
||||||
|
d = self._full_mapping({1:3, 2:4})
|
||||||
|
d.update(self._full_mapping({1:2, 3:4, 5:6}).items())
|
||||||
|
self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
|
||||||
|
|
||||||
|
class SimpleUserDict:
|
||||||
|
def __init__(self):
|
||||||
|
self.d = {1:1, 2:2, 3:3}
|
||||||
|
def keys(self):
|
||||||
|
return self.d.keys()
|
||||||
|
def __getitem__(self, i):
|
||||||
|
return self.d[i]
|
||||||
|
d.clear()
|
||||||
|
d.update(SimpleUserDict())
|
||||||
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
||||||
|
|
||||||
|
def test_fromkeys(self):
|
||||||
|
self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertTrue(not(d.fromkeys('abc') is d))
|
||||||
|
self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
|
||||||
|
self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
|
||||||
|
self.assertEqual(d.fromkeys([]), {})
|
||||||
|
def g():
|
||||||
|
yield 1
|
||||||
|
self.assertEqual(d.fromkeys(g()), {1:None})
|
||||||
|
self.assertRaises(TypeError, {}.fromkeys, 3)
|
||||||
|
class dictlike(self.type2test): pass
|
||||||
|
self.assertEqual(dictlike.fromkeys('a'), {'a':None})
|
||||||
|
self.assertEqual(dictlike().fromkeys('a'), {'a':None})
|
||||||
|
self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike)
|
||||||
|
self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike)
|
||||||
|
self.assertTrue(type(dictlike.fromkeys('a')) is dictlike)
|
||||||
|
class mydict(self.type2test):
|
||||||
|
def __new__(cls):
|
||||||
|
return collections.UserDict()
|
||||||
|
ud = mydict.fromkeys('ab')
|
||||||
|
self.assertEqual(ud, {'a':None, 'b':None})
|
||||||
|
self.assertIsInstance(ud, collections.UserDict)
|
||||||
|
self.assertRaises(TypeError, dict.fromkeys)
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class baddict1(self.type2test):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
raise Exc()
|
||||||
|
|
||||||
|
self.assertRaises(Exc, baddict1.fromkeys, [1])
|
||||||
|
|
||||||
|
class BadSeq(object):
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
def __next__(self):
|
||||||
|
raise Exc()
|
||||||
|
|
||||||
|
self.assertRaises(Exc, self.type2test.fromkeys, BadSeq())
|
||||||
|
|
||||||
|
class baddict2(self.type2test):
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
raise Exc()
|
||||||
|
|
||||||
|
self.assertRaises(Exc, baddict2.fromkeys, [1])
|
||||||
|
|
||||||
|
def test_copy(self):
|
||||||
|
d = self._full_mapping({1:1, 2:2, 3:3})
|
||||||
|
self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(d.copy(), d)
|
||||||
|
self.assertIsInstance(d.copy(), d.__class__)
|
||||||
|
self.assertRaises(TypeError, d.copy, None)
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
BasicTestMappingProtocol.test_get(self)
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertTrue(d.get('c') is None)
|
||||||
|
self.assertEqual(d.get('c', 3), 3)
|
||||||
|
d = self._full_mapping({'a' : 1, 'b' : 2})
|
||||||
|
self.assertTrue(d.get('c') is None)
|
||||||
|
self.assertEqual(d.get('c', 3), 3)
|
||||||
|
self.assertEqual(d.get('a'), 1)
|
||||||
|
self.assertEqual(d.get('a', 3), 1)
|
||||||
|
|
||||||
|
def test_setdefault(self):
|
||||||
|
BasicTestMappingProtocol.test_setdefault(self)
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertTrue(d.setdefault('key0') is None)
|
||||||
|
d.setdefault('key0', [])
|
||||||
|
self.assertTrue(d.setdefault('key0') is None)
|
||||||
|
d.setdefault('key', []).append(3)
|
||||||
|
self.assertEqual(d['key'][0], 3)
|
||||||
|
d.setdefault('key', []).append(4)
|
||||||
|
self.assertEqual(len(d['key']), 2)
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
BasicTestMappingProtocol.test_popitem(self)
|
||||||
|
for copymode in -1, +1:
|
||||||
|
# -1: b has same structure as a
|
||||||
|
# +1: b is a.copy()
|
||||||
|
for log2size in range(12):
|
||||||
|
size = 2**log2size
|
||||||
|
a = self._empty_mapping()
|
||||||
|
b = self._empty_mapping()
|
||||||
|
for i in range(size):
|
||||||
|
a[repr(i)] = i
|
||||||
|
if copymode < 0:
|
||||||
|
b[repr(i)] = i
|
||||||
|
if copymode > 0:
|
||||||
|
b = a.copy()
|
||||||
|
for i in range(size):
|
||||||
|
ka, va = ta = a.popitem()
|
||||||
|
self.assertEqual(va, int(ka))
|
||||||
|
kb, vb = tb = b.popitem()
|
||||||
|
self.assertEqual(vb, int(kb))
|
||||||
|
self.assertTrue(not(copymode < 0 and ta != tb))
|
||||||
|
self.assertTrue(not a)
|
||||||
|
self.assertTrue(not b)
|
||||||
|
|
||||||
|
def test_pop(self):
|
||||||
|
BasicTestMappingProtocol.test_pop(self)
|
||||||
|
|
||||||
|
# Tests for pop with specified key
|
||||||
|
d = self._empty_mapping()
|
||||||
|
k, v = 'abc', 'def'
|
||||||
|
|
||||||
|
self.assertEqual(d.pop(k, v), v)
|
||||||
|
d[k] = v
|
||||||
|
self.assertEqual(d.pop(k, 1), v)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHashMappingProtocol(TestMappingProtocol):
|
||||||
|
|
||||||
|
def test_getitem(self):
|
||||||
|
TestMappingProtocol.test_getitem(self)
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class BadEq(object):
|
||||||
|
def __eq__(self, other):
|
||||||
|
raise Exc()
|
||||||
|
def __hash__(self):
|
||||||
|
return 24
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d[BadEq()] = 42
|
||||||
|
self.assertRaises(KeyError, d.__getitem__, 23)
|
||||||
|
|
||||||
|
class BadHash(object):
|
||||||
|
fail = False
|
||||||
|
def __hash__(self):
|
||||||
|
if self.fail:
|
||||||
|
raise Exc()
|
||||||
|
else:
|
||||||
|
return 42
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
x = BadHash()
|
||||||
|
d[x] = 42
|
||||||
|
x.fail = True
|
||||||
|
self.assertRaises(Exc, d.__getitem__, x)
|
||||||
|
|
||||||
|
def test_fromkeys(self):
|
||||||
|
TestMappingProtocol.test_fromkeys(self)
|
||||||
|
class mydict(self.type2test):
|
||||||
|
def __new__(cls):
|
||||||
|
return collections.UserDict()
|
||||||
|
ud = mydict.fromkeys('ab')
|
||||||
|
self.assertEqual(ud, {'a':None, 'b':None})
|
||||||
|
self.assertIsInstance(ud, collections.UserDict)
|
||||||
|
|
||||||
|
def test_pop(self):
|
||||||
|
TestMappingProtocol.test_pop(self)
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class BadHash(object):
|
||||||
|
fail = False
|
||||||
|
def __hash__(self):
|
||||||
|
if self.fail:
|
||||||
|
raise Exc()
|
||||||
|
else:
|
||||||
|
return 42
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
x = BadHash()
|
||||||
|
d[x] = 42
|
||||||
|
x.fail = True
|
||||||
|
self.assertRaises(Exc, d.pop, x)
|
||||||
|
|
||||||
|
def test_mutatingiteration(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d[1] = 1
|
||||||
|
try:
|
||||||
|
count = 0
|
||||||
|
for i in d:
|
||||||
|
d[i+1] = 1
|
||||||
|
if count >= 1:
|
||||||
|
self.fail("changing dict size during iteration doesn't raise Error")
|
||||||
|
count += 1
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_repr(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
self.assertEqual(repr(d), '{}')
|
||||||
|
d[1] = 2
|
||||||
|
self.assertEqual(repr(d), '{1: 2}')
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d[1] = d
|
||||||
|
self.assertEqual(repr(d), '{1: {...}}')
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class BadRepr(object):
|
||||||
|
def __repr__(self):
|
||||||
|
raise Exc()
|
||||||
|
|
||||||
|
d = self._full_mapping({1: BadRepr()})
|
||||||
|
self.assertRaises(Exc, repr, d)
|
||||||
|
|
||||||
|
def test_repr_deep(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
for i in range(get_c_recursion_limit() + 1):
|
||||||
|
d0 = d
|
||||||
|
d = self._empty_mapping()
|
||||||
|
d[1] = d0
|
||||||
|
self.assertRaises(RecursionError, repr, d)
|
||||||
|
|
||||||
|
def test_eq(self):
|
||||||
|
self.assertEqual(self._empty_mapping(), self._empty_mapping())
|
||||||
|
self.assertEqual(self._full_mapping({1: 2}),
|
||||||
|
self._full_mapping({1: 2}))
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class BadCmp(object):
|
||||||
|
def __eq__(self, other):
|
||||||
|
raise Exc()
|
||||||
|
def __hash__(self):
|
||||||
|
return 1
|
||||||
|
|
||||||
|
d1 = self._full_mapping({BadCmp(): 1})
|
||||||
|
d2 = self._full_mapping({1: 1})
|
||||||
|
self.assertRaises(Exc, lambda: BadCmp()==1)
|
||||||
|
self.assertRaises(Exc, lambda: d1==d2)
|
||||||
|
|
||||||
|
def test_setdefault(self):
|
||||||
|
TestMappingProtocol.test_setdefault(self)
|
||||||
|
|
||||||
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
class BadHash(object):
|
||||||
|
fail = False
|
||||||
|
def __hash__(self):
|
||||||
|
if self.fail:
|
||||||
|
raise Exc()
|
||||||
|
else:
|
||||||
|
return 42
|
||||||
|
|
||||||
|
d = self._empty_mapping()
|
||||||
|
x = BadHash()
|
||||||
|
d[x] = 42
|
||||||
|
x.fail = True
|
||||||
|
self.assertRaises(Exc, d.setdefault, x, [])
|
||||||
122
test/dynamo/cpython/3_13/test_dict.diff
Normal file
122
test/dynamo/cpython/3_13/test_dict.diff
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
diff --git a/test/dynamo/cpython/3_13/test_dict.py b/test/dynamo/cpython/3_13/test_dict.py
|
||||||
|
index 4729132c5a5..14f829c1715 100644
|
||||||
|
--- a/test/dynamo/cpython/3_13/test_dict.py
|
||||||
|
+++ b/test/dynamo/cpython/3_13/test_dict.py
|
||||||
|
@@ -1,3 +1,57 @@
|
||||||
|
+# ======= BEGIN Dynamo patch =======
|
||||||
|
+# Owner(s): ["module: dynamo"]
|
||||||
|
+
|
||||||
|
+# ruff: noqa
|
||||||
|
+# flake8: noqa
|
||||||
|
+
|
||||||
|
+import sys
|
||||||
|
+import torch
|
||||||
|
+import torch._dynamo.test_case
|
||||||
|
+import unittest
|
||||||
|
+from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
+from torch.testing._internal.common_utils import (
|
||||||
|
+ run_tests,
|
||||||
|
+ xfailIfTorchDynamo,
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+__TestCase = CPythonTestCase
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# redirect import statements
|
||||||
|
+import sys
|
||||||
|
+import importlib.abc
|
||||||
|
+
|
||||||
|
+redirect_imports = (
|
||||||
|
+ "test.mapping_tests",
|
||||||
|
+ "test.typinganndata",
|
||||||
|
+ "test.test_grammar",
|
||||||
|
+ "test.test_math",
|
||||||
|
+ "test.test_iter",
|
||||||
|
+ "test.typinganndata.ann_module",
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
+ def find_spec(self, fullname, path, target=None):
|
||||||
|
+ # Check if the import is the problematic one
|
||||||
|
+ if fullname in redirect_imports:
|
||||||
|
+ try:
|
||||||
|
+ # Attempt to import the standalone module
|
||||||
|
+ name = fullname.removeprefix("test.")
|
||||||
|
+ r = importlib.import_module(name)
|
||||||
|
+ # Redirect the module in sys.modules
|
||||||
|
+ sys.modules[fullname] = r
|
||||||
|
+ # Return a module spec from the found module
|
||||||
|
+ return importlib.util.find_spec(name)
|
||||||
|
+ except ImportError:
|
||||||
|
+ return None
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+# Add the custom finder to sys.meta_path
|
||||||
|
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# ======= END DYNAMO PATCH =======
|
||||||
|
+
|
||||||
|
import collections
|
||||||
|
import collections.abc
|
||||||
|
import gc
|
||||||
|
@@ -11,7 +65,7 @@ from test import support
|
||||||
|
from test.support import import_helper, get_c_recursion_limit
|
||||||
|
|
||||||
|
|
||||||
|
-class DictTest(unittest.TestCase):
|
||||||
|
+class DictTest(__TestCase):
|
||||||
|
|
||||||
|
def test_invalid_keyword_arguments(self):
|
||||||
|
class Custom(dict):
|
||||||
|
@@ -265,6 +319,7 @@ class DictTest(unittest.TestCase):
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
|
||||||
|
|
||||||
|
+ @unittest.skip("test hangs")
|
||||||
|
def test_fromkeys(self):
|
||||||
|
self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
|
||||||
|
d = {}
|
||||||
|
@@ -477,7 +532,7 @@ class DictTest(unittest.TestCase):
|
||||||
|
for copymode in -1, +1:
|
||||||
|
# -1: b has same structure as a
|
||||||
|
# +1: b is a.copy()
|
||||||
|
- for log2size in range(12):
|
||||||
|
+ for log2size in range(4):
|
||||||
|
size = 2**log2size
|
||||||
|
a = {}
|
||||||
|
b = {}
|
||||||
|
@@ -1006,18 +1061,6 @@ class DictTest(unittest.TestCase):
|
||||||
|
pass
|
||||||
|
self._tracked(MyDict())
|
||||||
|
|
||||||
|
- @support.cpython_only
|
||||||
|
- def test_track_lazy_instance_dicts(self):
|
||||||
|
- class C:
|
||||||
|
- pass
|
||||||
|
- o = C()
|
||||||
|
- d = o.__dict__
|
||||||
|
- self._not_tracked(d)
|
||||||
|
- o.untracked = 42
|
||||||
|
- self._not_tracked(d)
|
||||||
|
- o.tracked = []
|
||||||
|
- self._tracked(d)
|
||||||
|
-
|
||||||
|
def make_shared_key_dict(self, n):
|
||||||
|
class C:
|
||||||
|
pass
|
||||||
|
@@ -1622,7 +1665,7 @@ class DictTest(unittest.TestCase):
|
||||||
|
self.assertGreaterEqual(eq_count, 1)
|
||||||
|
|
||||||
|
|
||||||
|
-class CAPITest(unittest.TestCase):
|
||||||
|
+class CAPITest(__TestCase):
|
||||||
|
|
||||||
|
# Test _PyDict_GetItem_KnownHash()
|
||||||
|
@support.cpython_only
|
||||||
|
@@ -1666,4 +1709,4 @@ class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
- unittest.main()
|
||||||
|
+ run_tests()
|
||||||
1712
test/dynamo/cpython/3_13/test_dict.py
Normal file
1712
test/dynamo/cpython/3_13/test_dict.py
Normal file
File diff suppressed because it is too large
Load Diff
173
test/dynamo/cpython/3_13/test_ordered_dict.diff
Normal file
173
test/dynamo/cpython/3_13/test_ordered_dict.diff
Normal file
|
|
@ -0,0 +1,173 @@
|
||||||
|
diff --git a/test/dynamo/cpython/3_13/test_ordered_dict.py b/test/dynamo/cpython/3_13/test_ordered_dict.py
|
||||||
|
index a9b6a84996e..b77eff70414 100644
|
||||||
|
--- a/test/dynamo/cpython/3_13/test_ordered_dict.py
|
||||||
|
+++ b/test/dynamo/cpython/3_13/test_ordered_dict.py
|
||||||
|
@@ -1,3 +1,57 @@
|
||||||
|
+# ======= BEGIN Dynamo patch =======
|
||||||
|
+# Owner(s): ["module: dynamo"]
|
||||||
|
+
|
||||||
|
+# ruff: noqa
|
||||||
|
+# flake8: noqa
|
||||||
|
+
|
||||||
|
+import sys
|
||||||
|
+import torch
|
||||||
|
+import torch._dynamo.test_case
|
||||||
|
+import unittest
|
||||||
|
+from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
+from torch.testing._internal.common_utils import (
|
||||||
|
+ run_tests,
|
||||||
|
+ xfailIfTorchDynamo,
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+__TestCase = CPythonTestCase
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# redirect import statements
|
||||||
|
+import sys
|
||||||
|
+import importlib.abc
|
||||||
|
+
|
||||||
|
+redirect_imports = (
|
||||||
|
+ "test.mapping_tests",
|
||||||
|
+ "test.typinganndata",
|
||||||
|
+ "test.test_grammar",
|
||||||
|
+ "test.test_math",
|
||||||
|
+ "test.test_iter",
|
||||||
|
+ "test.typinganndata.ann_module",
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
+ def find_spec(self, fullname, path, target=None):
|
||||||
|
+ # Check if the import is the problematic one
|
||||||
|
+ if fullname in redirect_imports:
|
||||||
|
+ try:
|
||||||
|
+ # Attempt to import the standalone module
|
||||||
|
+ name = fullname.removeprefix("test.")
|
||||||
|
+ r = importlib.import_module(name)
|
||||||
|
+ # Redirect the module in sys.modules
|
||||||
|
+ sys.modules[fullname] = r
|
||||||
|
+ # Return a module spec from the found module
|
||||||
|
+ return importlib.util.find_spec(name)
|
||||||
|
+ except ImportError:
|
||||||
|
+ return None
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+# Add the custom finder to sys.meta_path
|
||||||
|
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# ======= END DYNAMO PATCH =======
|
||||||
|
+
|
||||||
|
import builtins
|
||||||
|
import contextlib
|
||||||
|
import copy
|
||||||
|
@@ -760,7 +814,7 @@ class _TriggerSideEffectOnEqual:
|
||||||
|
def side_effect(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
-class PurePythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
|
||||||
|
+class PurePythonOrderedDictTests(OrderedDictTests, __TestCase):
|
||||||
|
|
||||||
|
module = py_coll
|
||||||
|
OrderedDict = py_coll.OrderedDict
|
||||||
|
@@ -781,7 +835,7 @@ class PurePythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
|
||||||
|
self.assertDictEqual(dict2, dict.fromkeys((0, Key(), 4.2)))
|
||||||
|
|
||||||
|
|
||||||
|
-class CPythonBuiltinDictTests(unittest.TestCase):
|
||||||
|
+class CPythonBuiltinDictTests(__TestCase):
|
||||||
|
"""Builtin dict preserves insertion order.
|
||||||
|
|
||||||
|
Reuse some of tests in OrderedDict selectively.
|
||||||
|
@@ -800,6 +854,7 @@ for method in (
|
||||||
|
del method
|
||||||
|
|
||||||
|
|
||||||
|
+
|
||||||
|
class CPythonOrderedDictSideEffects:
|
||||||
|
|
||||||
|
def check_runtime_error_issue119004(self, dict1, dict2):
|
||||||
|
@@ -878,7 +933,7 @@ class CPythonOrderedDictSideEffects:
|
||||||
|
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
|
||||||
|
class CPythonOrderedDictTests(OrderedDictTests,
|
||||||
|
CPythonOrderedDictSideEffects,
|
||||||
|
- unittest.TestCase):
|
||||||
|
+ __TestCase):
|
||||||
|
|
||||||
|
module = c_coll
|
||||||
|
OrderedDict = c_coll.OrderedDict
|
||||||
|
@@ -986,7 +1041,7 @@ class CPythonOrderedDictSubclassTests(CPythonOrderedDictTests):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
-class PurePythonOrderedDictWithSlotsCopyingTests(unittest.TestCase):
|
||||||
|
+class PurePythonOrderedDictWithSlotsCopyingTests(__TestCase):
|
||||||
|
|
||||||
|
module = py_coll
|
||||||
|
class OrderedDict(py_coll.OrderedDict):
|
||||||
|
@@ -995,7 +1050,7 @@ class PurePythonOrderedDictWithSlotsCopyingTests(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
|
||||||
|
-class CPythonOrderedDictWithSlotsCopyingTests(unittest.TestCase):
|
||||||
|
+class CPythonOrderedDictWithSlotsCopyingTests(__TestCase):
|
||||||
|
|
||||||
|
module = c_coll
|
||||||
|
class OrderedDict(c_coll.OrderedDict):
|
||||||
|
@@ -1008,6 +1063,7 @@ class PurePythonGeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.type2test = py_coll.OrderedDict
|
||||||
|
+ super().setUpClass()
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
@@ -1020,6 +1076,7 @@ class CPythonGeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.type2test = c_coll.OrderedDict
|
||||||
|
+ super().setUpClass()
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
@@ -1033,6 +1090,7 @@ class PurePythonSubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
|
class MyOrderedDict(py_coll.OrderedDict):
|
||||||
|
pass
|
||||||
|
cls.type2test = MyOrderedDict
|
||||||
|
+ super().setUpClass()
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
@@ -1047,6 +1105,7 @@ class CPythonSubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
|
class MyOrderedDict(c_coll.OrderedDict):
|
||||||
|
pass
|
||||||
|
cls.type2test = MyOrderedDict
|
||||||
|
+ super().setUpClass()
|
||||||
|
|
||||||
|
def test_popitem(self):
|
||||||
|
d = self._empty_mapping()
|
||||||
|
@@ -1120,21 +1179,22 @@ class SimpleLRUCacheTests:
|
||||||
|
self.assertEqual(list(c), [1, 3, 2])
|
||||||
|
|
||||||
|
|
||||||
|
-class PySimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
|
||||||
|
+class PySimpleLRUCacheTests(SimpleLRUCacheTests, __TestCase):
|
||||||
|
|
||||||
|
class type2test(SimpleLRUCache, py_coll.OrderedDict):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(c_coll, 'requires the C version of the collections module')
|
||||||
|
-class CSimpleLRUCacheTests(SimpleLRUCacheTests, unittest.TestCase):
|
||||||
|
+class CSimpleLRUCacheTests(SimpleLRUCacheTests, __TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
class type2test(SimpleLRUCache, c_coll.OrderedDict):
|
||||||
|
pass
|
||||||
|
cls.type2test = type2test
|
||||||
|
+ super().setUpClass()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
- unittest.main()
|
||||||
|
+ run_tests()
|
||||||
1200
test/dynamo/cpython/3_13/test_ordered_dict.py
Normal file
1200
test/dynamo/cpython/3_13/test_ordered_dict.py
Normal file
File diff suppressed because it is too large
Load Diff
74
test/dynamo/cpython/3_13/test_userdict.diff
Normal file
74
test/dynamo/cpython/3_13/test_userdict.diff
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
diff --git a/test/dynamo/cpython/3_13/test_userdict.py b/test/dynamo/cpython/3_13/test_userdict.py
|
||||||
|
index 61e79f553e8..c953390355e 100644
|
||||||
|
--- a/test/dynamo/cpython/3_13/test_userdict.py
|
||||||
|
+++ b/test/dynamo/cpython/3_13/test_userdict.py
|
||||||
|
@@ -1,3 +1,54 @@
|
||||||
|
+# ======= BEGIN Dynamo patch =======
|
||||||
|
+# Owner(s): ["module: dynamo"]
|
||||||
|
+
|
||||||
|
+# ruff: noqa
|
||||||
|
+# flake8: noqa
|
||||||
|
+
|
||||||
|
+import sys
|
||||||
|
+import torch
|
||||||
|
+import torch._dynamo.test_case
|
||||||
|
+import unittest
|
||||||
|
+from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
+from torch.testing._internal.common_utils import run_tests
|
||||||
|
+
|
||||||
|
+__TestCase = CPythonTestCase
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# redirect import statements
|
||||||
|
+import sys
|
||||||
|
+import importlib.abc
|
||||||
|
+
|
||||||
|
+redirect_imports = (
|
||||||
|
+ "test.mapping_tests",
|
||||||
|
+ "test.typinganndata",
|
||||||
|
+ "test.test_grammar",
|
||||||
|
+ "test.test_math",
|
||||||
|
+ "test.test_iter",
|
||||||
|
+ "test.typinganndata.ann_module",
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
+ def find_spec(self, fullname, path, target=None):
|
||||||
|
+ # Check if the import is the problematic one
|
||||||
|
+ if fullname in redirect_imports:
|
||||||
|
+ try:
|
||||||
|
+ # Attempt to import the standalone module
|
||||||
|
+ name = fullname.removeprefix("test.")
|
||||||
|
+ r = importlib.import_module(name)
|
||||||
|
+ # Redirect the module in sys.modules
|
||||||
|
+ sys.modules[fullname] = r
|
||||||
|
+ # Return a module spec from the found module
|
||||||
|
+ return importlib.util.find_spec(name)
|
||||||
|
+ except ImportError:
|
||||||
|
+ return None
|
||||||
|
+ return None
|
||||||
|
+
|
||||||
|
+# Add the custom finder to sys.meta_path
|
||||||
|
+sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
+
|
||||||
|
+
|
||||||
|
+# ======= END DYNAMO PATCH =======
|
||||||
|
+
|
||||||
|
# Check every path through every method of UserDict
|
||||||
|
|
||||||
|
from test import mapping_tests, support
|
||||||
|
@@ -215,10 +266,10 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||||
|
|
||||||
|
# Decorate existing test with recursion limit, because
|
||||||
|
# the test is for C structure, but `UserDict` is a Python structure.
|
||||||
|
- test_repr_deep = support.infinite_recursion(25)(
|
||||||
|
- mapping_tests.TestHashMappingProtocol.test_repr_deep,
|
||||||
|
- )
|
||||||
|
+ # test_repr_deep = support.infinite_recursion(25)(
|
||||||
|
+ # mapping_tests.TestHashMappingProtocol.test_repr_deep,
|
||||||
|
+ # )
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
- unittest.main()
|
||||||
|
+ run_tests()
|
||||||
275
test/dynamo/cpython/3_13/test_userdict.py
Normal file
275
test/dynamo/cpython/3_13/test_userdict.py
Normal file
|
|
@ -0,0 +1,275 @@
|
||||||
|
# ======= BEGIN Dynamo patch =======
|
||||||
|
# Owner(s): ["module: dynamo"]
|
||||||
|
|
||||||
|
# ruff: noqa
|
||||||
|
# flake8: noqa
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import torch
|
||||||
|
import torch._dynamo.test_case
|
||||||
|
import unittest
|
||||||
|
from torch._dynamo.test_case import CPythonTestCase
|
||||||
|
from torch.testing._internal.common_utils import run_tests
|
||||||
|
|
||||||
|
__TestCase = CPythonTestCase
|
||||||
|
|
||||||
|
|
||||||
|
# redirect import statements
|
||||||
|
import sys
|
||||||
|
import importlib.abc
|
||||||
|
|
||||||
|
redirect_imports = (
|
||||||
|
"test.mapping_tests",
|
||||||
|
"test.typinganndata",
|
||||||
|
"test.test_grammar",
|
||||||
|
"test.test_math",
|
||||||
|
"test.test_iter",
|
||||||
|
"test.typinganndata.ann_module",
|
||||||
|
)
|
||||||
|
|
||||||
|
class RedirectImportFinder(importlib.abc.MetaPathFinder):
|
||||||
|
def find_spec(self, fullname, path, target=None):
|
||||||
|
# Check if the import is the problematic one
|
||||||
|
if fullname in redirect_imports:
|
||||||
|
try:
|
||||||
|
# Attempt to import the standalone module
|
||||||
|
name = fullname.removeprefix("test.")
|
||||||
|
r = importlib.import_module(name)
|
||||||
|
# Redirect the module in sys.modules
|
||||||
|
sys.modules[fullname] = r
|
||||||
|
# Return a module spec from the found module
|
||||||
|
return importlib.util.find_spec(name)
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Add the custom finder to sys.meta_path
|
||||||
|
sys.meta_path.insert(0, RedirectImportFinder())
|
||||||
|
|
||||||
|
|
||||||
|
# ======= END DYNAMO PATCH =======
|
||||||
|
|
||||||
|
# Check every path through every method of UserDict
|
||||||
|
|
||||||
|
from test import mapping_tests, support
|
||||||
|
import unittest
|
||||||
|
import collections
|
||||||
|
|
||||||
|
d0 = {}
|
||||||
|
d1 = {"one": 1}
|
||||||
|
d2 = {"one": 1, "two": 2}
|
||||||
|
d3 = {"one": 1, "two": 3, "three": 5}
|
||||||
|
d4 = {"one": None, "two": None}
|
||||||
|
d5 = {"one": 1, "two": 1}
|
||||||
|
|
||||||
|
class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||||
|
type2test = collections.UserDict
|
||||||
|
|
||||||
|
def test_all(self):
|
||||||
|
# Test constructors
|
||||||
|
u = collections.UserDict()
|
||||||
|
u0 = collections.UserDict(d0)
|
||||||
|
u1 = collections.UserDict(d1)
|
||||||
|
u2 = collections.UserDict(d2)
|
||||||
|
|
||||||
|
uu = collections.UserDict(u)
|
||||||
|
uu0 = collections.UserDict(u0)
|
||||||
|
uu1 = collections.UserDict(u1)
|
||||||
|
uu2 = collections.UserDict(u2)
|
||||||
|
|
||||||
|
# keyword arg constructor
|
||||||
|
self.assertEqual(collections.UserDict(one=1, two=2), d2)
|
||||||
|
# item sequence constructor
|
||||||
|
self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
|
||||||
|
self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]),
|
||||||
|
{'dict': [('one', 1), ('two', 2)]})
|
||||||
|
# both together
|
||||||
|
self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
|
||||||
|
|
||||||
|
# alternate constructor
|
||||||
|
self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
|
||||||
|
self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
|
||||||
|
self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5)
|
||||||
|
self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5)
|
||||||
|
self.assertTrue(u1.fromkeys('one two'.split()) is not u1)
|
||||||
|
self.assertIsInstance(u1.fromkeys('one two'.split()), collections.UserDict)
|
||||||
|
self.assertIsInstance(u2.fromkeys('one two'.split()), collections.UserDict)
|
||||||
|
|
||||||
|
# Test __repr__
|
||||||
|
self.assertEqual(str(u0), str(d0))
|
||||||
|
self.assertEqual(repr(u1), repr(d1))
|
||||||
|
self.assertIn(repr(u2), ("{'one': 1, 'two': 2}",
|
||||||
|
"{'two': 2, 'one': 1}"))
|
||||||
|
|
||||||
|
# Test rich comparison and __len__
|
||||||
|
all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
|
||||||
|
for a in all:
|
||||||
|
for b in all:
|
||||||
|
self.assertEqual(a == b, len(a) == len(b))
|
||||||
|
|
||||||
|
# Test __getitem__
|
||||||
|
self.assertEqual(u2["one"], 1)
|
||||||
|
self.assertRaises(KeyError, u1.__getitem__, "two")
|
||||||
|
|
||||||
|
# Test __setitem__
|
||||||
|
u3 = collections.UserDict(u2)
|
||||||
|
u3["two"] = 2
|
||||||
|
u3["three"] = 3
|
||||||
|
|
||||||
|
# Test __delitem__
|
||||||
|
del u3["three"]
|
||||||
|
self.assertRaises(KeyError, u3.__delitem__, "three")
|
||||||
|
|
||||||
|
# Test clear
|
||||||
|
u3.clear()
|
||||||
|
self.assertEqual(u3, {})
|
||||||
|
|
||||||
|
# Test copy()
|
||||||
|
u2a = u2.copy()
|
||||||
|
self.assertEqual(u2a, u2)
|
||||||
|
u2b = collections.UserDict(x=42, y=23)
|
||||||
|
u2c = u2b.copy() # making a copy of a UserDict is special cased
|
||||||
|
self.assertEqual(u2b, u2c)
|
||||||
|
|
||||||
|
class MyUserDict(collections.UserDict):
|
||||||
|
def display(self): print(self)
|
||||||
|
|
||||||
|
m2 = MyUserDict(u2)
|
||||||
|
m2a = m2.copy()
|
||||||
|
self.assertEqual(m2a, m2)
|
||||||
|
|
||||||
|
# SF bug #476616 -- copy() of UserDict subclass shared data
|
||||||
|
m2['foo'] = 'bar'
|
||||||
|
self.assertNotEqual(m2a, m2)
|
||||||
|
|
||||||
|
# Test keys, items, values
|
||||||
|
self.assertEqual(sorted(u2.keys()), sorted(d2.keys()))
|
||||||
|
self.assertEqual(sorted(u2.items()), sorted(d2.items()))
|
||||||
|
self.assertEqual(sorted(u2.values()), sorted(d2.values()))
|
||||||
|
|
||||||
|
# Test "in".
|
||||||
|
for i in u2.keys():
|
||||||
|
self.assertIn(i, u2)
|
||||||
|
self.assertEqual(i in u1, i in d1)
|
||||||
|
self.assertEqual(i in u0, i in d0)
|
||||||
|
|
||||||
|
# Test update
|
||||||
|
t = collections.UserDict()
|
||||||
|
t.update(u2)
|
||||||
|
self.assertEqual(t, u2)
|
||||||
|
|
||||||
|
# Test get
|
||||||
|
for i in u2.keys():
|
||||||
|
self.assertEqual(u2.get(i), u2[i])
|
||||||
|
self.assertEqual(u1.get(i), d1.get(i))
|
||||||
|
self.assertEqual(u0.get(i), d0.get(i))
|
||||||
|
|
||||||
|
# Test "in" iteration.
|
||||||
|
for i in range(20):
|
||||||
|
u2[i] = str(i)
|
||||||
|
ikeys = []
|
||||||
|
for k in u2:
|
||||||
|
ikeys.append(k)
|
||||||
|
keys = u2.keys()
|
||||||
|
self.assertEqual(set(ikeys), set(keys))
|
||||||
|
|
||||||
|
# Test setdefault
|
||||||
|
t = collections.UserDict()
|
||||||
|
self.assertEqual(t.setdefault("x", 42), 42)
|
||||||
|
self.assertIn("x", t)
|
||||||
|
self.assertEqual(t.setdefault("x", 23), 42)
|
||||||
|
|
||||||
|
# Test pop
|
||||||
|
t = collections.UserDict(x=42)
|
||||||
|
self.assertEqual(t.pop("x"), 42)
|
||||||
|
self.assertRaises(KeyError, t.pop, "x")
|
||||||
|
self.assertEqual(t.pop("x", 1), 1)
|
||||||
|
t["x"] = 42
|
||||||
|
self.assertEqual(t.pop("x", 1), 42)
|
||||||
|
|
||||||
|
# Test popitem
|
||||||
|
t = collections.UserDict(x=42)
|
||||||
|
self.assertEqual(t.popitem(), ("x", 42))
|
||||||
|
self.assertRaises(KeyError, t.popitem)
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
for kw in 'self', 'other', 'iterable':
|
||||||
|
self.assertEqual(list(collections.UserDict(**{kw: 42}).items()),
|
||||||
|
[(kw, 42)])
|
||||||
|
self.assertEqual(list(collections.UserDict({}, dict=42).items()),
|
||||||
|
[('dict', 42)])
|
||||||
|
self.assertEqual(list(collections.UserDict({}, dict=None).items()),
|
||||||
|
[('dict', None)])
|
||||||
|
self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()),
|
||||||
|
[('dict', {'a': 42})])
|
||||||
|
self.assertRaises(TypeError, collections.UserDict, 42)
|
||||||
|
self.assertRaises(TypeError, collections.UserDict, (), ())
|
||||||
|
self.assertRaises(TypeError, collections.UserDict.__init__)
|
||||||
|
|
||||||
|
def test_update(self):
|
||||||
|
for kw in 'self', 'dict', 'other', 'iterable':
|
||||||
|
d = collections.UserDict()
|
||||||
|
d.update(**{kw: 42})
|
||||||
|
self.assertEqual(list(d.items()), [(kw, 42)])
|
||||||
|
self.assertRaises(TypeError, collections.UserDict().update, 42)
|
||||||
|
self.assertRaises(TypeError, collections.UserDict().update, {}, {})
|
||||||
|
self.assertRaises(TypeError, collections.UserDict.update)
|
||||||
|
|
||||||
|
def test_missing(self):
|
||||||
|
# Make sure UserDict doesn't have a __missing__ method
|
||||||
|
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
|
||||||
|
# Test several cases:
|
||||||
|
# (D) subclass defines __missing__ method returning a value
|
||||||
|
# (E) subclass defines __missing__ method raising RuntimeError
|
||||||
|
# (F) subclass sets __missing__ instance variable (no effect)
|
||||||
|
# (G) subclass doesn't define __missing__ at all
|
||||||
|
class D(collections.UserDict):
|
||||||
|
def __missing__(self, key):
|
||||||
|
return 42
|
||||||
|
d = D({1: 2, 3: 4})
|
||||||
|
self.assertEqual(d[1], 2)
|
||||||
|
self.assertEqual(d[3], 4)
|
||||||
|
self.assertNotIn(2, d)
|
||||||
|
self.assertNotIn(2, d.keys())
|
||||||
|
self.assertEqual(d[2], 42)
|
||||||
|
class E(collections.UserDict):
|
||||||
|
def __missing__(self, key):
|
||||||
|
raise RuntimeError(key)
|
||||||
|
e = E()
|
||||||
|
try:
|
||||||
|
e[42]
|
||||||
|
except RuntimeError as err:
|
||||||
|
self.assertEqual(err.args, (42,))
|
||||||
|
else:
|
||||||
|
self.fail("e[42] didn't raise RuntimeError")
|
||||||
|
class F(collections.UserDict):
|
||||||
|
def __init__(self):
|
||||||
|
# An instance variable __missing__ should have no effect
|
||||||
|
self.__missing__ = lambda key: None
|
||||||
|
collections.UserDict.__init__(self)
|
||||||
|
f = F()
|
||||||
|
try:
|
||||||
|
f[42]
|
||||||
|
except KeyError as err:
|
||||||
|
self.assertEqual(err.args, (42,))
|
||||||
|
else:
|
||||||
|
self.fail("f[42] didn't raise KeyError")
|
||||||
|
class G(collections.UserDict):
|
||||||
|
pass
|
||||||
|
g = G()
|
||||||
|
try:
|
||||||
|
g[42]
|
||||||
|
except KeyError as err:
|
||||||
|
self.assertEqual(err.args, (42,))
|
||||||
|
else:
|
||||||
|
self.fail("g[42] didn't raise KeyError")
|
||||||
|
|
||||||
|
# Decorate existing test with recursion limit, because
|
||||||
|
# the test is for C structure, but `UserDict` is a Python structure.
|
||||||
|
# test_repr_deep = support.infinite_recursion(25)(
|
||||||
|
# mapping_tests.TestHashMappingProtocol.test_repr_deep,
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_tests()
|
||||||
0
test/dynamo/cpython/3_13/typinganndata/__init__.py
Normal file
0
test/dynamo/cpython/3_13/typinganndata/__init__.py
Normal file
30
test/dynamo/cpython/3_13/typinganndata/_typed_dict_helper.py
Normal file
30
test/dynamo/cpython/3_13/typinganndata/_typed_dict_helper.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
"""Used to test `get_type_hints()` on a cross-module inherited `TypedDict` class
|
||||||
|
|
||||||
|
This script uses future annotations to postpone a type that won't be available
|
||||||
|
on the module inheriting from to `Foo`. The subclass in the other module should
|
||||||
|
look something like this:
|
||||||
|
|
||||||
|
class Bar(_typed_dict_helper.Foo, total=False):
|
||||||
|
b: int
|
||||||
|
|
||||||
|
In addition, it uses multiple levels of Annotated to test the interaction
|
||||||
|
between the __future__ import, Annotated, and Required.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Annotated, Generic, Optional, Required, TypedDict, TypeVar
|
||||||
|
|
||||||
|
|
||||||
|
OptionalIntType = Optional[int]
|
||||||
|
|
||||||
|
class Foo(TypedDict):
|
||||||
|
a: OptionalIntType
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
class FooGeneric(TypedDict, Generic[T]):
|
||||||
|
a: Optional[T]
|
||||||
|
|
||||||
|
class VeryAnnotated(TypedDict, total=False):
|
||||||
|
a: Annotated[Annotated[Annotated[Required[int], "a"], "b"], "c"]
|
||||||
62
test/dynamo/cpython/3_13/typinganndata/ann_module.py
Normal file
62
test/dynamo/cpython/3_13/typinganndata/ann_module.py
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
The module for testing variable annotations.
|
||||||
|
Empty lines above are for good reason (testing for correct line numbers)
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
__annotations__[1] = 2
|
||||||
|
|
||||||
|
class C:
|
||||||
|
|
||||||
|
x = 5; y: Optional['C'] = None
|
||||||
|
|
||||||
|
from typing import Tuple
|
||||||
|
x: int = 5; y: str = x; f: Tuple[int, int]
|
||||||
|
|
||||||
|
class M(type):
|
||||||
|
|
||||||
|
__annotations__['123'] = 123
|
||||||
|
o: type = object
|
||||||
|
|
||||||
|
(pars): bool = True
|
||||||
|
|
||||||
|
class D(C):
|
||||||
|
j: str = 'hi'; k: str= 'bye'
|
||||||
|
|
||||||
|
from types import new_class
|
||||||
|
h_class = new_class('H', (C,))
|
||||||
|
j_class = new_class('J')
|
||||||
|
|
||||||
|
class F():
|
||||||
|
z: int = 5
|
||||||
|
def __init__(self, x):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Y(F):
|
||||||
|
def __init__(self):
|
||||||
|
super(F, self).__init__(123)
|
||||||
|
|
||||||
|
class Meta(type):
|
||||||
|
def __new__(meta, name, bases, namespace):
|
||||||
|
return super().__new__(meta, name, bases, namespace)
|
||||||
|
|
||||||
|
class S(metaclass = Meta):
|
||||||
|
x: str = 'something'
|
||||||
|
y: str = 'something else'
|
||||||
|
|
||||||
|
def foo(x: int = 10):
|
||||||
|
def bar(y: List[str]):
|
||||||
|
x: str = 'yes'
|
||||||
|
bar()
|
||||||
|
|
||||||
|
def dec(func):
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
u: int | float
|
||||||
36
test/dynamo/cpython/3_13/typinganndata/ann_module2.py
Normal file
36
test/dynamo/cpython/3_13/typinganndata/ann_module2.py
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
"""
|
||||||
|
Some correct syntax for variable annotation here.
|
||||||
|
More examples are in test_grammar and test_parser.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import no_type_check, ClassVar
|
||||||
|
|
||||||
|
i: int = 1
|
||||||
|
j: int
|
||||||
|
x: float = i/10
|
||||||
|
|
||||||
|
def f():
|
||||||
|
class C: ...
|
||||||
|
return C()
|
||||||
|
|
||||||
|
f().new_attr: object = object()
|
||||||
|
|
||||||
|
class C:
|
||||||
|
def __init__(self, x: int) -> None:
|
||||||
|
self.x = x
|
||||||
|
|
||||||
|
c = C(5)
|
||||||
|
c.new_attr: int = 10
|
||||||
|
|
||||||
|
__annotations__ = {}
|
||||||
|
|
||||||
|
|
||||||
|
@no_type_check
|
||||||
|
class NTC:
|
||||||
|
def meth(self, param: complex) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
class CV:
|
||||||
|
var: ClassVar['CV']
|
||||||
|
|
||||||
|
CV.var = CV()
|
||||||
18
test/dynamo/cpython/3_13/typinganndata/ann_module3.py
Normal file
18
test/dynamo/cpython/3_13/typinganndata/ann_module3.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
"""
|
||||||
|
Correct syntax for variable annotation that should fail at runtime
|
||||||
|
in a certain manner. More examples are in test_grammar and test_parser.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def f_bad_ann():
|
||||||
|
__annotations__[1] = 2
|
||||||
|
|
||||||
|
class C_OK:
|
||||||
|
def __init__(self, x: int) -> None:
|
||||||
|
self.x: no_such_name = x # This one is OK as proposed by Guido
|
||||||
|
|
||||||
|
class D_bad_ann:
|
||||||
|
def __init__(self, x: int) -> None:
|
||||||
|
sfel.y: int = 0
|
||||||
|
|
||||||
|
def g_bad_ann():
|
||||||
|
no_such_name.attr: int = 0
|
||||||
5
test/dynamo/cpython/3_13/typinganndata/ann_module4.py
Normal file
5
test/dynamo/cpython/3_13/typinganndata/ann_module4.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# This ann_module isn't for test_typing,
|
||||||
|
# it's for test_module
|
||||||
|
|
||||||
|
a:int=3
|
||||||
|
b:str=4
|
||||||
10
test/dynamo/cpython/3_13/typinganndata/ann_module5.py
Normal file
10
test/dynamo/cpython/3_13/typinganndata/ann_module5.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Used by test_typing to verify that Final wrapped in ForwardRef works.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
name: Final[str] = "final"
|
||||||
|
|
||||||
|
class MyClass:
|
||||||
|
value: Final = 3000
|
||||||
7
test/dynamo/cpython/3_13/typinganndata/ann_module6.py
Normal file
7
test/dynamo/cpython/3_13/typinganndata/ann_module6.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Tests that top-level ClassVar is not allowed
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
|
wrong: ClassVar[int] = 1
|
||||||
22
test/dynamo/cpython/3_13/typinganndata/ann_module695.py
Normal file
22
test/dynamo/cpython/3_13/typinganndata/ann_module695.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
|
class A[T, *Ts, **P]:
|
||||||
|
x: T
|
||||||
|
y: tuple[*Ts]
|
||||||
|
z: Callable[P, str]
|
||||||
|
|
||||||
|
|
||||||
|
class B[T, *Ts, **P]:
|
||||||
|
T = int
|
||||||
|
Ts = str
|
||||||
|
P = bytes
|
||||||
|
x: T
|
||||||
|
y: Ts
|
||||||
|
z: P
|
||||||
|
|
||||||
|
|
||||||
|
def generic_function[T, *Ts, **P](
|
||||||
|
x: T, *y: *Ts, z: P.args, zz: P.kwargs
|
||||||
|
) -> None: ...
|
||||||
11
test/dynamo/cpython/3_13/typinganndata/ann_module7.py
Normal file
11
test/dynamo/cpython/3_13/typinganndata/ann_module7.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Tests class have ``__text_signature__``
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
DEFAULT_BUFFER_SIZE = 8192
|
||||||
|
|
||||||
|
class BufferedReader(object):
|
||||||
|
"""BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n--\n\n
|
||||||
|
Create a new buffered reader using the given readable raw IO object.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
10
test/dynamo/cpython/3_13/typinganndata/ann_module8.py
Normal file
10
test/dynamo/cpython/3_13/typinganndata/ann_module8.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Test `@no_type_check`,
|
||||||
|
# see https://bugs.python.org/issue46571
|
||||||
|
|
||||||
|
class NoTypeCheck_Outer:
|
||||||
|
class Inner:
|
||||||
|
x: int
|
||||||
|
|
||||||
|
|
||||||
|
def NoTypeCheck_function(arg: int) -> int:
|
||||||
|
...
|
||||||
14
test/dynamo/cpython/3_13/typinganndata/ann_module9.py
Normal file
14
test/dynamo/cpython/3_13/typinganndata/ann_module9.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Test ``inspect.formatannotation``
|
||||||
|
# https://github.com/python/cpython/issues/96073
|
||||||
|
|
||||||
|
from typing import Union, List
|
||||||
|
|
||||||
|
ann = Union[List[str], int]
|
||||||
|
|
||||||
|
# mock typing._type_repr behaviour
|
||||||
|
class A: ...
|
||||||
|
|
||||||
|
A.__module__ = 'testModule.typing'
|
||||||
|
A.__qualname__ = 'A'
|
||||||
|
|
||||||
|
ann1 = Union[List[A], int]
|
||||||
24
test/dynamo/cpython/3_13/typinganndata/mod_generics_cache.py
Normal file
24
test/dynamo/cpython/3_13/typinganndata/mod_generics_cache.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
"""Module for testing the behavior of generics across different modules."""
|
||||||
|
|
||||||
|
from typing import TypeVar, Generic, Optional, TypeAliasType
|
||||||
|
|
||||||
|
default_a: Optional['A'] = None
|
||||||
|
default_b: Optional['B'] = None
|
||||||
|
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
|
class A(Generic[T]):
|
||||||
|
some_b: 'B'
|
||||||
|
|
||||||
|
|
||||||
|
class B(Generic[T]):
|
||||||
|
class A(Generic[T]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
my_inner_a1: 'B.A'
|
||||||
|
my_inner_a2: A
|
||||||
|
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__
|
||||||
|
|
||||||
|
type Alias = int
|
||||||
|
OldStyle = TypeAliasType("OldStyle", int)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user