python / python/typing

Conditionally load stubs based on specific version of package installed

Open
#693 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

For django-stubs, we would like to be able to load stubs conditionally, like it's done for typeshed via https://mypy.readthedocs.io/en/latest/common_issues.html#python-version-and-system-platform-checks

See https://github.com/typeddjango/django-stubs/issues/262

I'd like to propose to add a special function is_package_installed for stub files:

if is_package_installed('Django>=3.0.*')
    class Django30Class: ...
else:
    class Django22Class: ...

which will evaluate according to the rules specified in PEP440 https://www.python.org/dev/peps/pep-0440/#version-specifiers

It was discussed in https://github.com/python/typeshed/issues/153, current workaround seems to be (from https://github.com/python/typeshed/issues/153#issuecomment-389198282):

release and install a separate stub package for each version of the library

I believe it's too complex, as one needs to maintain lots of separate branches for specific versions of the supported package. And versioning of the stubs package would then be PACKAGE_VERSION.STUBS_VERSION_INCREMENT?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked mypy documentation and PEP 440 version-specifier rules, then read the referenced django-stubs and typeshed discussions. The proposed behavior is a design for conditionally selecting stub declarations based on an installed package version; done requires an agreed mechanism and documented behavior for supported version checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.