typing
typing copied to clipboard
Conditionally load stubs based on specific version of package installed
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
?
I am also interested in how stubs resolution / merge works in this case.
For example:
- what if new functions / classes are added to a new version?
- what if their types are changed?
- what if something is removed from the future version?