8000 gh-111681: Fix doctests in `typing.rst` and remove unused imports by sobolevn · Pull Request #111682 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111681: Fix doctests in typing.rst and remove unused imports #111682

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

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 5 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ to this is that a list of types can be used to substitute a :class:`ParamSpec`::
>>> Z[int, [dict, float]]
__main__.Z[int, [dict, float]]

>>> 1 + 2
4
Copy link
Member
@AlexWaygood AlexWaygood Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't actually doctested, I don't think, since you don't have the .. doctest:: directive above this block. (I've just pushed to your branch to add it.)

Copy link
Member Author
@sobolevn sobolevn Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a doctest, clearly:

» ./python.exe -m doctest Doc/library/typing.rst
**********************************************************************
File "Doc/library/typing.rst", line 651, in typing.rst
Failed example:
    1 + 2
Expected:
    4
Got:
    3
**********************************************************************
1 items had failures:
   1 of  48 in typing.rst
***Test Failed*** 1 failures.

It works the same way with and without .. doctest::.
The problem might that Sphinx does not recognize doctests without this directive, but this is clearly a Sphinx's own issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was what I meant, sorry for not being clear: that it wouldn't be picked up by sphinx's doctest job :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to use doctest_test_doctest_blocks from https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to use doctest_test_doctest_blocks from https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html

We can try enabling that, sure, but that should be a separate issue/PR. This PR is about fixing broken doctests in typing.rst, not changing the way we determine in CI which doctests should be run. The majority of these that need to be tested already are tested, using the .. doctest:: directive.


Classes generic over a :class:`ParamSpec` can also be created using explicit
inheritance from :class:`Generic`. In this case, ``**`` is not used::

Expand Down Expand Up @@ -1954,7 +1957,7 @@ without the dedicated syntax, as documented below.

.. doctest::

>>> from typing import ParamSpec
>>> from typing import ParamSpec, get_origin
Copy link
Member
8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the change you're making here. But the reason why this doesn't currently fail when Sphinx runs our doctests in CI is because of this, which is run before every doctest block:

.. testsetup:: *
import typing
from dataclasses import dataclass
from typing import *

>>> P = ParamSpec("P")
>>> get_origin(P.args) is P
True
Expand Down Expand Up @@ -3065,8 +3068,7 @@ Introspection helpers
>>> class P(Protocol):
... def a(self) -> str: ...
... b: int
>>> get_protocol_members(P)
frozenset({'a', 'b'})
>>> assert get_protocol_members(P) == frozenset({'a', 'b'})

Raise :exc:`TypeError` for arguments that are not Protocols.

Expand Down
17 changes: 14 additions & 3 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import pickle
import re
import sys
import warnings
from unittest import TestCase, main, skipUnless, skip
import os
import doctest
from unittest import TestCase, main, skip
from unittest.mock import patch
from copy import copy, deepcopy

Expand Down Expand Up @@ -45,7 +46,7 @@
import weakref
import types

from test.support import import_helper, captured_stderr, cpython_only
from test.support import captured_stderr, cpython_only, REPO_ROOT
from test import mod_generics_cache
from test import _typed_dict_helper

Expand Down Expand Up @@ -9465,5 +9466,15 @@ def test_is_not_instance_of_iterable(self):
self.assertNotIsInstance(type_to_test, collections.abc.Iterable)


def load_tests(loader, tests, pattern):
tests.addTests(doctest.DocFileSuite(
os.path.join(REPO_ROOT, 'Doc/library/typing.rst'),
module_relative=False,
# Some tests in `typing.rst` pretend to be executed in `__main__`:
globs={'__name__': '__main__'},
))
return tests


if __name__ == '__main__':
main()
0