Skip to content

Classes available during type checking only #597

Closed
@srittau

Description

@srittau

This came up in srittau/peps#21: Stubs may need to define classes that are not available at runtime. Best examples are the return types of re.compile() and re.match(), although those are handled specially in typing.

One solution we came up with is to add a class decorator to typing, for example @type_check_only. While type checking this is a no-op, during runtime it mangles the name of the class, so that it is not available in the module namespace under its original name.

This is mostly intended for type stubs, but could be used in implementations (starting in Python 3.8). We would add it to typeshed without a version check, so it can be used in stubs immediately. Alternatively, type checkers could special case the import. The documentation should also recommend to make the real class available in own code, instead of using the decorator,

@rchen152 @Solumin

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions