Skip to content

typing.py contains two doctests that are not executed, ever #112155

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

Closed
sobolevn opened this issue Nov 16, 2023 · 1 comment
Closed

typing.py contains two doctests that are not executed, ever #112155

sobolevn opened this issue Nov 16, 2023 · 1 comment
Assignees
Labels
tests Tests in the Lib/test dir topic-typing type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

sobolevn commented Nov 16, 2023

Feature or enhancement

typing.py contains at least two doctests:

cpython/Lib/typing.py

Lines 3377 to 3414 in 81ab0e8

def is_protocol(tp: type, /) -> bool:
"""Return True if the given type is a Protocol.
Example::
>>> from typing import Protocol, is_protocol
>>> class P(Protocol):
... def a(self) -> str: ...
... b: int
>>> is_protocol(P)
True
>>> is_protocol(int)
False
"""
return (
isinstance(tp, type)
and getattr(tp, '_is_protocol', False)
and tp != Protocol
)
def get_protocol_members(tp: type, /) -> frozenset[str]:
"""Return the set of members defined in a Protocol.
Example::
>>> from typing import Protocol, get_protocol_members
>>> class P(Protocol):
... def a(self) -> str: ...
... b: int
>>> get_protocol_members(P)
frozenset({'a', 'b'})
Raise a TypeError for arguments that are not Protocols.
"""
if not is_protocol(tp):
raise TypeError(f'{tp!r} is not a Protocol')
return frozenset(tp.__protocol_attrs__)

But, they are not ever executed.

I propose to add a DocTestSuite to test_typing to run these tests.

Note, that this is related but not similar to #111682

Linked PRs

@sobolevn sobolevn added type-feature A feature request or enhancement tests Tests in the Lib/test dir topic-typing labels Nov 16, 2023
@sobolevn sobolevn self-assigned this Nov 16, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 16, 2023
@AlexWaygood
Copy link
Member

The change has been applied to main. Lib/typing.py has no doctests on the 3.12 or 3.11 branches, so there's no need to backport this.

Thanks @sobolevn!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants