Skip to main content

PEP 561 type stubs for django-oscar

Project description

django-oscar-stubs

PEP 561 type stubs for django-oscar.

Includes a custom mypy plugin that resolves the return types of oscar's dynamic class loading functions (get_model, get_class, get_classes) to the actual concrete types, including support for forked apps.

Installation

pip install django-oscar-stubs

Configuration

Add mypy_oscar_plugin to your mypy plugins, alongside mypy_django_plugin.main from django-stubs:

# mypy.ini
[mypy]
plugins =
    mypy_django_plugin.main,
    mypy_oscar_plugin

[mypy.plugins.django-stubs]
django_settings_module = myproject.settings

The oscar plugin reads django_settings_module from the django-stubs config section (or the DJANGO_SETTINGS_MODULE environment variable) to detect forked apps. Plugin ordering does not matter, but both plugins must be listed.

What it does

Type-safe dynamic loading

Oscar uses get_model and get_class to dynamically load models and classes. Without the plugin, these return untyped type objects. With the plugin, mypy resolves them to the actual types:

from oscar.core.loading import get_model, get_class, get_classes

# Without plugin: type[Model]
# With plugin:    type[oscar.apps.catalogue.models.Product]
Product = get_model("catalogue", "Product")

# Without plugin: type
# With plugin:    type[oscar.apps.address.forms.UserAddressForm]
UserAddressForm = get_class("address.forms", "UserAddressForm")

# Without plugin: list[type]
# With plugin:    tuple[type[BasketLineForm], type[SavedLineForm]]
BasketLineForm, SavedLineForm = get_classes(
    "basket.forms", ["BasketLineForm", "SavedLineForm"]
)

Forked app support

When your project forks an oscar app, the plugin detects this from INSTALLED_APPS and resolves to your forked types instead of oscar's defaults:

# settings.py
INSTALLED_APPS = [
    ...
    "myproject.catalogue.apps.CatalogueConfig",  # forked from oscar
    "oscar.apps.basket.apps.BasketConfig",        # not forked
    ...
]
# myproject/catalogue/models.py
from oscar.apps.catalogue.abstract_models import AbstractProduct

class Product(AbstractProduct):
    custom_field = models.CharField(max_length=100)
# Resolves to type[myproject.catalogue.models.Product] (with custom_field!)
Product = get_model("catalogue", "Product")
Product.objects.filter(custom_field="foo")  # type-safe

The plugin falls back to oscar's default types for any app that hasn't been forked, or when literal string arguments aren't used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_oscar_stubs-4.1.0b2.tar.gz (122.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_oscar_stubs-4.1.0b2-py3-none-any.whl (124.2 kB view details)

Uploaded Python 3

File details

Details for the file django_oscar_stubs-4.1.0b2.tar.gz.

File metadata

  • Download URL: django_oscar_stubs-4.1.0b2.tar.gz
  • Upload date:
  • Size: 122.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for django_oscar_stubs-4.1.0b2.tar.gz
Algorithm Hash digest
SHA256 5f9bf4d5bb3ab1637a14359cd142ef26a4400f59e1119ed3b97611f35f6c8e71
MD5 473393fed6e6ae64e1bb5d07e6b04d05
BLAKE2b-256 b7c0ad1e8bd36bdff657cf0304e0a00d396e7935ff07d1e00d238de64dc670a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_oscar_stubs-4.1.0b2.tar.gz:

Publisher: .gitlab-ci.yml on thelabnyc/django-oscar-stubs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_oscar_stubs-4.1.0b2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_oscar_stubs-4.1.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 801be33f828c92e4489caec9673e0f5ff20fc4aa1ab1906507307a893d5eb55f
MD5 9472b52dfb185ec58da97262f7eeff39
BLAKE2b-256 ebc9392dab444f03c7b78ba5e07a799a49aa8578655e07023e2265a27bccc69a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_oscar_stubs-4.1.0b2-py3-none-any.whl:

Publisher: .gitlab-ci.yml on thelabnyc/django-oscar-stubs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page