Skip to content

bpo-35581: Document @typing.type_check_only #11312

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 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,24 @@ The module defines the following classes, functions and decorators:
This wraps the decorator with something that wraps the decorated
function in :func:`no_type_check`.

.. decorator:: type_check_only

Decorator to mark a class or function to be unavailable at runtime.

This decorator is itself not available at runtime. It is mainly
Copy link
Member

Choose a reason for hiding this comment

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

TBH, I am not sure we need to document something not actually available at runtime here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bit of a meta ramble: One of the problems is that there is not central "typing documentation". Most things are described in PEP 484, the typing documentation and mypy's documentation and there is a significant overlap, but some things are only described in one of them. The typeshed CONTRIBUTING file has a bit more stuff, too. Although by now I'd describe myself as fairly experienced regarding typing, I am still always looking for the right document when I encounter a corner case or want to link someone the documentation.

In the long term we should probably have a comprehensive guide on https://docs.python.org/ and link to that from the other documentation. But until that is the case, I don't think PEP 484 should be the only place where this is documented. And while it is not available at runtime, @type_check_only is imported from typing, so its documentation is where I would look for it.

intended to mark classes that are defined in type stub files if
an implementation returns an instance of a private class::

@type_check_only
class Response: # private or not available at runtime
code: int
def get_header(self, name: str) -> str: ...

def fetch_response() -> Response: ...

Note that returning instances of private classes is not recommended.
Copy link
Member

Choose a reason for hiding this comment

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

Even if we decide that documenting this makes sense, providing an example that is not recommended is a bad idea. I would instead provide a more realistic example. IIUC a typical situation for this would be a private protocol for argument types. Protocols are not accepted yet, so maybe this should wait until they are?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my experience, returning instances of private classes is a pretty common occurence. There are multiple examples of it in the standard library, the IO classes as well as Pattern and Match being the most prominent.

It is usually preferable to make such classes public.

.. data:: Any

Special type indicating an unconstrained type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@typing.type_check_only now allows type stubs to mark functions and classes not available during runtime.