-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-40129: Add fake number classes in test.support. #19262
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
Changes from all commits
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 |
---|---|---|
|
@@ -121,6 +121,7 @@ | |
"run_with_tz", "PGO", "missing_compiler_executable", "fd_count", | ||
"ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST", | ||
"LOOPBACK_TIMEOUT", "INTERNET_TIMEOUT", "SHORT_TIMEOUT", "LONG_TIMEOUT", | ||
"FakeIndex", "FakeInt", "FakeFloat", "FakeComplex", "FakePath", | ||
] | ||
|
||
|
||
|
@@ -3189,22 +3190,37 @@ def with_pymalloc(): | |
return _testcapi.WITH_PYMALLOC | ||
|
||
|
||
class FakePath: | ||
"""Simple implementing of the path protocol. | ||
""" | ||
def __init__(self, path): | ||
self.path = path | ||
class Fake: | ||
def __init__(self, value): | ||
self.value = value | ||
|
||
def __repr__(self): | ||
return f'<FakePath {self.path!r}>' | ||
return f'<{self.__class__.__name__} {self.value!r}>' | ||
|
||
def __fspath__(self): | ||
if (isinstance(self.path, BaseException) or | ||
isinstance(self.path, type) and | ||
issubclass(self.path, BaseException)): | ||
raise self.path | ||
def _return(self): | ||
if (isinstance(self.value, BaseException) or | ||
isinstance(self.value, type) and | ||
issubclass(self.value, BaseException)): | ||
raise self.value | ||
else: | ||
return self.path | ||
return self.value | ||
|
||
class FakeIndex(Fake): | ||
__index__ = Fake._return | ||
|
||
class FakeInt(Fake): | ||
__int__ = Fake._return | ||
|
||
class FakeFloat(Fake): | ||
__float__ = Fake._return | ||
|
||
class FakeComplex(Fake): | ||
__complex__ = Fake._return | ||
|
||
class FakePath(Fake): | ||
"""Simple implementing of the path protocol. | ||
""" | ||
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. Should this be on the previous line? Looks weird to have it sitting on its own following a short one-line docstring like this. |
||
__fspath__ = Fake._return | ||
|
||
|
||
class _ALWAYS_EQ: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import contextlib | ||
import unittest | ||
from test import support | ||
from test.support import FakeIndex, FakeInt | ||
from itertools import permutations, product | ||
from random import randrange, sample, choice | ||
import warnings | ||
|
@@ -2510,22 +2511,9 @@ def test_memoryview_sizeof(self): | |
check(memoryview(a), vsize(base_struct + 3 * per_dim)) | ||
|
||
def test_memoryview_struct_module(self): | ||
|
||
class INT(object): | ||
def __init__(self, val): | ||
self.val = val | ||
def __int__(self): | ||
return self.val | ||
|
||
class IDX(object): | ||
def __init__(self, val): | ||
self.val = val | ||
def __index__(self): | ||
return self.val | ||
|
||
def f(): return 7 | ||
|
||
values = [INT(9), IDX(9), | ||
values = [FakeInt(9), FakeIndex(9), | ||
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. I don't quite understand the name, there's nothing really fake about these classes. Also, it's nice to see what the classes do without leaving the file. 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. I agree with both points here. When scanning through the PR on GitHub and seeing The problem could potentially be mitigated with a name that more clearly describes what the class is doing ( |
||
2.2+3j, Decimal("-21.1"), 12.2, Fraction(5, 2), | ||
[1,2,3], {4,5,6}, {7:8}, (), (9,), | ||
True, False, None, Ellipsis, | ||
|
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.