Skip to content

Flexible module availability checks #596

Closed

Description

@srittau

Currently to mark a package or module to be available in a certain Python version, it has to be moved to a specially named directory (e.g. stdlib/3.4). This has a few shortcomings:

  • Module removals can't be expressed (like macpath in 3.8)
  • Additions of submodules is awkward and usually has to duplicate multiple files
  • Platform-availability can't be expressed

I suggest to add a marker inside stub files based on the existing sys.version_info/sys.platform checks to mark the unavailability of a module, in addition to the existing path-based system. This could, for example, look like this:

if sys.version_info >= (3, 8):
    raise NotImplementedError()
if sys.platform != "win32":
    raise NotImplementedError()

This might prove hard to implement "correctly", i.e. refuse imports of unavailable modules, but an easy workaround is to print a warning when such a construct is encountered while parsing a file and possibly stop parsing the stub at this point.

Activity

  1. JelleZijlstra commented on Dec 4, 2018

    @JelleZijlstra
    Member

    Mypy landed a similar feature recently (python/mypy#5894).

  2. srittau commented on Dec 4, 2018

    @srittau
    CollaboratorAuthor

    Huh, Guido must be psychic. This basically fulfills the use case above, except it can't determine that a module can't be imported at all. But in practice this should not matter. This seems like a feature that would be a good fit for the type stub PEP.

  3. srittau commented on Nov 4, 2021

    @srittau
    CollaboratorAuthor

    This is mostly moot due to the new typeshed layout, where the VERSIONS file specifies available versions. Only open issue are modules unavailable in some OSes, but that sounds more of a typeshed issue anyway.

  4. added
    topic: featureDiscussions about new features for Python's type annotations
    on Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions