-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-143959 Make _datetime,_types optional for test_types #144042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a3b9317
e17be43
52b0e16
9fcb7a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ | |
| import collections.abc | ||
| from collections import namedtuple, UserDict | ||
| import copy | ||
| import _datetime | ||
| import gc | ||
| import inspect | ||
| import pickle | ||
|
|
@@ -22,6 +21,14 @@ | |
| import weakref | ||
| import typing | ||
| import re | ||
| try: | ||
| import _datetime | ||
| except ModuleNotFoundError: | ||
| _datetime = None | ||
| try: | ||
| import _types | ||
| except ModuleNotFoundError: | ||
| _types = None | ||
|
|
||
| c_types = import_fresh_module('types', fresh=['_types']) | ||
| py_types = import_fresh_module('types', blocked=['_types']) | ||
|
|
@@ -40,15 +47,10 @@ def clear_typing_caches(): | |
|
|
||
| class TypesTests(unittest.TestCase): | ||
|
|
||
| def test_names(self): | ||
| def check_types_names(self, module_types, *, c_extension=False): | ||
| c_only_names = {'CapsuleType', 'LazyImportType'} | ||
| ignored = {'new_class', 'resolve_bases', 'prepare_class', | ||
| 'get_original_bases', 'DynamicClassAttribute', 'coroutine'} | ||
|
|
||
| for name in c_types.__all__: | ||
| if name not in c_only_names | ignored: | ||
| self.assertIs(getattr(c_types, name), getattr(py_types, name)) | ||
|
|
||
| all_names = ignored | { | ||
| 'AsyncGeneratorType', 'BuiltinFunctionType', 'BuiltinMethodType', | ||
| 'CapsuleType', 'CellType', 'ClassMethodDescriptorType', 'CodeType', | ||
|
|
@@ -61,8 +63,24 @@ def test_names(self): | |
| 'NotImplementedType', 'SimpleNamespace', 'TracebackType', | ||
| 'UnionType', 'WrapperDescriptorType', | ||
| } | ||
| self.assertEqual(all_names, set(c_types.__all__)) | ||
| self.assertEqual(all_names - c_only_names, set(py_types.__all__)) | ||
| expected = all_names if c_extension else all_names - c_only_names | ||
| self.assertEqual(expected, set(module_types.__all__)) | ||
|
|
||
| @unittest.skipUnless(_types, "requires C _types module") | ||
| def test_c_types_names(self): | ||
| self.check_types_names(c_types, c_extension=True) | ||
|
|
||
| def test_py_types_names(self): | ||
| self.check_types_names(py_types, c_extension=False) | ||
|
|
||
| @unittest.skipUnless(_types, "requires C _types module") | ||
| def test_types_names_consistency(self): | ||
| c_only_names = {'CapsuleType', 'LazyImportType'} | ||
| ignored = {'new_class', 'resolve_bases', 'prepare_class', | ||
| 'get_original_bases', 'DynamicClassAttribute', 'coroutine'} | ||
| for name in c_types.__all__: | ||
| if name not in c_only_names | ignored: | ||
| self.assertIs(getattr(c_types, name), getattr(py_types, name)) | ||
|
|
||
| def test_truth_values(self): | ||
| if None: self.fail('None is true instead of false') | ||
|
|
@@ -692,6 +710,7 @@ def test_traceback_and_frame_types(self): | |
| self.assertIsInstance(exc.__traceback__, types.TracebackType) | ||
| self.assertIsInstance(exc.__traceback__.tb_frame, types.FrameType) | ||
|
|
||
| @unittest.skipUnless(_types and _datetime, "requires _datetime module") | ||
| def test_capsule_type(self): | ||
| self.assertIsInstance(_datetime.datetime_CAPI, types.CapsuleType) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why these changes were needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current test only runs meaningful when
_typesoptional module is implemented.Usually Python unittest includes conditional handling to run tests under proper environment, but this test was exposed without it.
Adding checking
_typessplit the test and verify the behavior regardless the environment has_typesor not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you test this?
Note that
import_fresh_module()returns None when one of the "fresh" modules can not be imported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's exactly the cause of the problem. The test doesn't work when they are None.
local patch
btw, I am sorry for the late response.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deleted
_typesto check the changed tests pass.Also edited capsule test to be skipped when _types doesn't exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like that
c_only_namesandignoredare defined in several places.Would not be easier to simply skip tests that require
c_typesif it is not available?And since
c_typesandpy_typesare only used in this test, I would import them locally.