Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-71339: Add additional assertion methods for unittest #128707

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Spell "With" instead of "with".
  • Loading branch information
serhiy-storchaka committed Jan 11, 2025
commit a54e69396f78c116e538f83c99e9d470d8bfbeb3
24 changes: 12 additions & 12 deletions 24 Doc/library/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1224,17 +1224,17 @@ Test cases
| <TestCase.assertCountEqual>` | elements in the same number, | |
| | regardless of their order. | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertStartswith(a, b) | ``a.startswith(b)`` | 3.14 |
| <TestCase.assertStartswith>` | | |
| :meth:`assertStartsWith(a, b) | ``a.startswith(b)`` | 3.14 |
| <TestCase.assertStartsWith>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertNotStartswith(a, b) | ``not a.startswith(b)`` | 3.14 |
| <TestCase.assertNotStartswith>` | | |
| :meth:`assertNotStartsWith(a, b) | ``not a.startswith(b)`` | 3.14 |
| <TestCase.assertNotStartsWith>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertEndswith(a, b) | ``a.endswith(b)`` | 3.14 |
| <TestCase.assertEndswith>` | | |
| :meth:`assertEndsWith(a, b) | ``a.endswith(b)`` | 3.14 |
| <TestCase.assertEndsWith>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertNotEndswith(a, b) | ``not a.endswith(b)`` | 3.14 |
| <TestCase.assertNotEndswith>` | | |
| :meth:`assertNotEndsWith(a, b) | ``not a.endswith(b)`` | 3.14 |
| <TestCase.assertNotEndsWith>` | | |
+---------------------------------------+--------------------------------+--------------+
| :meth:`assertHasAttr(a, b) | ``hastattr(a, b)`` | 3.14 |
| <TestCase.assertHasAttr>` | | |
Expand Down Expand Up @@ -1310,17 +1310,17 @@ Test cases

.. versionadded:: 3.2

.. method:: assertStartswith(s, prefix, msg=None)
.. method:: assertNotStartswith(s, prefix, msg=None)
.. method:: assertStartsWith(s, prefix, msg=None)
.. method:: assertNotStartsWith(s, prefix, msg=None)
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved

Test that the unicode or byte string *s* starts (or does not start)
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
with a *prefix*.
*prefix* can also be a tuple of strings to try.

.. versionadded:: next

.. method:: assertEndswith(s, suffix, msg=None)
.. method:: assertNotEndswith(s, suffix, msg=None)
.. method:: assertEndsWith(s, suffix, msg=None)
.. method:: assertNotEndsWith(s, suffix, msg=None)

Test that the unicode or byte string *s* ends (or does not end)
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
with a *suffix*.
Expand Down
8 changes: 4 additions & 4 deletions 8 Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,10 @@ unittest
- :meth:`~unittest.TestCase.assertIsSubclass` and
:meth:`~unittest.TestCase.assertNotIsSubclass` check whether the object
is a subclass of a particular class, or of one of a tuple of classes.
- :meth:`~unittest.TestCase.assertStartswith`,
:meth:`~unittest.TestCase.assertNotStartswith`,
:meth:`~unittest.TestCase.assertEndswith` and
:meth:`~unittest.TestCase.assertNotEndswith` check whether the unicode
- :meth:`~unittest.TestCase.assertStartsWith`,
:meth:`~unittest.TestCase.assertNotStartsWith`,
:meth:`~unittest.TestCase.assertEndsWith` and
:meth:`~unittest.TestCase.assertNotEndsWith` check whether the unicode
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
or byte string starts or ends with particular string(s).

(Contributed by Serhiy Storchaka in :gh:`71339`.)
Expand Down
6 changes: 3 additions & 3 deletions 6 Lib/test/test_gdb/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def gdb_has_frame_select():
"Python was compiled with optimizations")
class PyListTests(DebuggerTests):
def assertListing(self, expected, actual):
self.assertEndswith(actual, expected)
self.assertEndsWith(actual, expected)

def test_basic_command(self):
'Verify that the "py-list" command works'
Expand Down Expand Up @@ -103,15 +103,15 @@ def test_down_at_bottom(self):
'Verify handling of "py-down" at the bottom of the stack'
bt = self.get_stack_trace(script=SAMPLE_SCRIPT,
cmds_after_breakpoint=['py-down'])
self.assertEndswith(bt,
self.assertEndsWith(bt,
'Unable to find a newer python frame\n')

@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
def test_up_at_top(self):
'Verify handling of "py-up" at the top of the stack'
bt = self.get_stack_trace(script=SAMPLE_SCRIPT,
cmds_after_breakpoint=['py-up'] * 5)
self.assertEndswith(bt,
self.assertEndsWith(bt,
'Unable to find an older python frame\n')

@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Expand Down
4 changes: 2 additions & 2 deletions 4 Lib/test/test_importlib/resources/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_read_text(self):
),
'\x00\x01\x02\x03',
)
self.assertEndswith( # ignore the BOM
self.assertEndsWith( # ignore the BOM
resources.read_text(
self.anchor01,
'utf-16.file',
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_open_text(self):
'utf-16.file',
errors='backslashreplace',
) as f:
self.assertEndswith( # ignore the BOM
self.assertEndsWith( # ignore the BOM
f.read(),
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
errors='backslashreplace',
Expand Down
110 changes: 55 additions & 55 deletions 110 Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,96 +1426,96 @@ def test_variadic_class_repr_is_correct(self):
class A(Generic[*Ts]): pass
class B(Generic[Unpack[Ts]]): pass

self.assertEndswith(repr(A[()]), 'A[()]')
self.assertEndswith(repr(B[()]), 'B[()]')
self.assertEndswith(repr(A[float]), 'A[float]')
self.assertEndswith(repr(B[float]), 'B[float]')
self.assertEndswith(repr(A[float, str]), 'A[float, str]')
self.assertEndswith(repr(B[float, str]), 'B[float, str]')

self.assertEndswith(repr(A[*tuple[int, ...]]),
self.assertEndsWith(repr(A[()]), 'A[()]')
self.assertEndsWith(repr(B[()]), 'B[()]')
self.assertEndsWith(repr(A[float]), 'A[float]')
self.assertEndsWith(repr(B[float]), 'B[float]')
self.assertEndsWith(repr(A[float, str]), 'A[float, str]')
self.assertEndsWith(repr(B[float, str]), 'B[float, str]')

self.assertEndsWith(repr(A[*tuple[int, ...]]),
'A[*tuple[int, ...]]')
self.assertEndswith(repr(B[Unpack[Tuple[int, ...]]]),
self.assertEndsWith(repr(B[Unpack[Tuple[int, ...]]]),
'B[typing.Unpack[typing.Tuple[int, ...]]]')

self.assertEndswith(repr(A[float, *tuple[int, ...]]),
self.assertEndsWith(repr(A[float, *tuple[int, ...]]),
'A[float, *tuple[int, ...]]')
self.assertEndswith(repr(A[float, Unpack[Tuple[int, ...]]]),
self.assertEndsWith(repr(A[float, Unpack[Tuple[int, ...]]]),
'A[float, typing.Unpack[typing.Tuple[int, ...]]]')

self.assertEndswith(repr(A[*tuple[int, ...], str]),
self.assertEndsWith(repr(A[*tuple[int, ...], str]),
'A[*tuple[int, ...], str]')
self.assertEndswith(repr(B[Unpack[Tuple[int, ...]], str]),
self.assertEndsWith(repr(B[Unpack[Tuple[int, ...]], str]),
'B[typing.Unpack[typing.Tuple[int, ...]], str]')

self.assertEndswith(repr(A[float, *tuple[int, ...], str]),
self.assertEndsWith(repr(A[float, *tuple[int, ...], str]),
'A[float, *tuple[int, ...], str]')
self.assertEndswith(repr(B[float, Unpack[Tuple[int, ...]], str]),
self.assertEndsWith(repr(B[float, Unpack[Tuple[int, ...]], str]),
'B[float, typing.Unpack[typing.Tuple[int, ...]], str]')

def test_variadic_class_alias_repr_is_correct(self):
Ts = TypeVarTuple('Ts')
class A(Generic[Unpack[Ts]]): pass

B = A[*Ts]
self.assertEndswith(repr(B), 'A[typing.Unpack[Ts]]')
self.assertEndswith(repr(B[()]), 'A[()]')
self.assertEndswith(repr(B[float]), 'A[float]')
self.assertEndswith(repr(B[float, str]), 'A[float, str]')
self.assertEndsWith(repr(B), 'A[typing.Unpack[Ts]]')
self.assertEndsWith(repr(B[()]), 'A[()]')
self.assertEndsWith(repr(B[float]), 'A[float]')
self.assertEndsWith(repr(B[float, str]), 'A[float, str]')

C = A[Unpack[Ts]]
self.assertEndswith(repr(C), 'A[typing.Unpack[Ts]]')
self.assertEndswith(repr(C[()]), 'A[()]')
self.assertEndswith(repr(C[float]), 'A[float]')
self.assertEndswith(repr(C[float, str]), 'A[float, str]')
self.assertEndsWith(repr(C), 'A[typing.Unpack[Ts]]')
self.assertEndsWith(repr(C[()]), 'A[()]')
self.assertEndsWith(repr(C[float]), 'A[float]')
self.assertEndsWith(repr(C[float, str]), 'A[float, str]')

D = A[*Ts, int]
self.assertEndswith(repr(D), 'A[typing.Unpack[Ts], int]')
self.assertEndswith(repr(D[()]), 'A[int]')
self.assertEndswith(repr(D[float]), 'A[float, int]')
self.assertEndswith(repr(D[float, str]), 'A[float, str, int]')
self.assertEndsWith(repr(D), 'A[typing.Unpack[Ts], int]')
self.assertEndsWith(repr(D[()]), 'A[int]')
self.assertEndsWith(repr(D[float]), 'A[float, int]')
self.assertEndsWith(repr(D[float, str]), 'A[float, str, int]')

E = A[Unpack[Ts], int]
self.assertEndswith(repr(E), 'A[typing.Unpack[Ts], int]')
self.assertEndswith(repr(E[()]), 'A[int]')
self.assertEndswith(repr(E[float]), 'A[float, int]')
self.assertEndswith(repr(E[float, str]), 'A[float, str, int]')
self.assertEndsWith(repr(E), 'A[typing.Unpack[Ts], int]')
self.assertEndsWith(repr(E[()]), 'A[int]')
self.assertEndsWith(repr(E[float]), 'A[float, int]')
self.assertEndsWith(repr(E[float, str]), 'A[float, str, int]')

F = A[int, *Ts]
self.assertEndswith(repr(F), 'A[int, typing.Unpack[Ts]]')
self.assertEndswith(repr(F[()]), 'A[int]')
self.assertEndswith(repr(F[float]), 'A[int, float]')
self.assertEndswith(repr(F[float, str]), 'A[int, float, str]')
self.assertEndsWith(repr(F), 'A[int, typing.Unpack[Ts]]')
self.assertEndsWith(repr(F[()]), 'A[int]')
self.assertEndsWith(repr(F[float]), 'A[int, float]')
self.assertEndsWith(repr(F[float, str]), 'A[int, float, str]')

G = A[int, Unpack[Ts]]
self.assertEndswith(repr(G), 'A[int, typing.Unpack[Ts]]')
self.assertEndswith(repr(G[()]), 'A[int]')
self.assertEndswith(repr(G[float]), 'A[int, float]')
self.assertEndswith(repr(G[float, str]), 'A[int, float, str]')
self.assertEndsWith(repr(G), 'A[int, typing.Unpack[Ts]]')
self.assertEndsWith(repr(G[()]), 'A[int]')
self.assertEndsWith(repr(G[float]), 'A[int, float]')
self.assertEndsWith(repr(G[float, str]), 'A[int, float, str]')

H = A[int, *Ts, str]
self.assertEndswith(repr(H), 'A[int, typing.Unpack[Ts], str]')
self.assertEndswith(repr(H[()]), 'A[int, str]')
self.assertEndswith(repr(H[float]), 'A[int, float, str]')
self.assertEndswith(repr(H[float, str]), 'A[int, float, str, str]')
self.assertEndsWith(repr(H), 'A[int, typing.Unpack[Ts], str]')
self.assertEndsWith(repr(H[()]), 'A[int, str]')
self.assertEndsWith(repr(H[float]), 'A[int, float, str]')
self.assertEndsWith(repr(H[float, str]), 'A[int, float, str, str]')

I = A[int, Unpack[Ts], str]
self.assertEndswith(repr(I), 'A[int, typing.Unpack[Ts], str]')
self.assertEndswith(repr(I[()]), 'A[int, str]')
self.assertEndswith(repr(I[float]), 'A[int, float, str]')
self.assertEndswith(repr(I[float, str]), 'A[int, float, str, str]')
self.assertEndsWith(repr(I), 'A[int, typing.Unpack[Ts], str]')
self.assertEndsWith(repr(I[()]), 'A[int, str]')
self.assertEndsWith(repr(I[float]), 'A[int, float, str]')
self.assertEndsWith(repr(I[float, str]), 'A[int, float, str, str]')

J = A[*Ts, *tuple[str, ...]]
self.assertEndswith(repr(J), 'A[typing.Unpack[Ts], *tuple[str, ...]]')
self.assertEndswith(repr(J[()]), 'A[*tuple[str, ...]]')
self.assertEndswith(repr(J[float]), 'A[float, *tuple[str, ...]]')
self.assertEndswith(repr(J[float, str]), 'A[float, str, *tuple[str, ...]]')
self.assertEndsWith(repr(J), 'A[typing.Unpack[Ts], *tuple[str, ...]]')
self.assertEndsWith(repr(J[()]), 'A[*tuple[str, ...]]')
self.assertEndsWith(repr(J[float]), 'A[float, *tuple[str, ...]]')
self.assertEndsWith(repr(J[float, str]), 'A[float, str, *tuple[str, ...]]')

K = A[Unpack[Ts], Unpack[Tuple[str, ...]]]
self.assertEndswith(repr(K), 'A[typing.Unpack[Ts], typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndswith(repr(K[()]), 'A[typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndswith(repr(K[float]), 'A[float, typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndswith(repr(K[float, str]), 'A[float, str, typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K), 'A[typing.Unpack[Ts], typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[()]), 'A[typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[float]), 'A[float, typing.Unpack[typing.Tuple[str, ...]]]')
self.assertEndsWith(repr(K[float, str]), 'A[float, str, typing.Unpack[typing.Tuple[str, ...]]]')

def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVarTuple'):
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.