Skip to content

Why do enum labels have type _StrOrPromise rather than _StrPromise?Β #2887

@rchen152

Description

@rchen152

Hi there! First of all, thank you for creating django-stubs - I'm a maintainer of a type checker called pyrefly, and we've been building support for django on top of django-stubs.

I had a question about some of the asserted types in tests/assert_type/db/models/test_enums.py. The gettext_lazy function is annotated as returning _StrPromise, but calls to gettext_lazy have asserted types of _StrOrPromise, e.g.:

class Suit(IntegerChoices):
    DIAMOND = 1, _("Diamond")
    SPADE = 2, _("Spade")
    HEART = 3, _("Heart")
    CLUB = 4, _("Club")
assert_type(Suit.CLUB.label, _StrOrPromise)  # why is this not _StrPromise?

class Vehicle(IntegerChoices):
    CAR = 1, "Carriage"
    TRUCK = 2
    JET_SKI = 3

    __empty__ = _("(Unknown)")
assert_type(Vehicle.__empty__, _StrOrPromise)  # why is this not _StrPromise?

Is there some Django magic I'm not aware of that is widening these types? Or is mypy inferring types that are less precise than they could be?

I'm pretty unfamiliar with Django, so apologies if this is a silly question. Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions