Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_StrPromise on a TypedDict #1804

Closed
michjnich opened this issue Oct 29, 2023 · 4 comments
Closed

_StrPromise on a TypedDict #1804

michjnich opened this issue Oct 29, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@michjnich
Copy link

Bug report

What's wrong

If I define a TypedDict

class CustomItem(TypedDict):
    name: str
    value: str

And a dict that uses this:

from django.utils.translation import gettext_lazy as _

MYDICT: dict[str, CustomItem] = {
    "test": {"name": _("testing"), "value": "this"}
}

I get the resulting message:

Incompatible types (expression has type "_StrPromise", TypedDict item "name" has type "str")

I am currently using the django_stubs_ext.StrOrPromise instead here, but the README asks us to report if we come across a _StrPromise issue while using Django.

How is that should be

I should be able to type a gettext_lazy value as str

System information

  • OS: WSL2 on Win11
  • python version: 3.11.6
  • django version: 4.2.6
  • mypy version: 1.6.1
  • django-stubs version: 4.2.6
  • django-stubs-ext version: 4.2.5
@michjnich michjnich added the bug Something isn't working label Oct 29, 2023
@michjnich michjnich changed the title StrPromise on a TypedDict _StrPromise on a TypedDict Oct 29, 2023
@flaeppe
Copy link
Member

flaeppe commented Oct 29, 2023

I should be able to type a gettext_lazy value as str

This has been brought up previously, or a slightly different version of it. Which you'll probably find if you start off somewhere around here: #689 (comment). Which is some discussion(s) and reasoning from when _StrPromise was introduced.

The short version is something like: while the promise returned by gettext_lazy in many cases behaves like a str, it doesn't always. And we want to be very careful spoofing the type checker.

@michjnich
Copy link
Author

Ah, OK. Sorry if I've re-raised a previous discussion - I did do a search first but obviously missed those specific comments.

So for this specific case the correct solution (and will remain the correct solution) is to use StrOrPromise from django-stubs-ext? Is that right? No problem if that's the case - I'd just got the impression from the README comment that it was kind of a workaround until these things could be standardises in core.

And if you close this one, at least there's an "obvious" issue that will pop up in a search for it in the event somebody else is looking for the same thing :)

@intgr
Copy link
Collaborator

intgr commented Oct 29, 2023

Yep, str and StrPromise are distinct types. If you need to typehint it in your own code, use the StrOrPromise type exported by django-stubs-ext:

from django_stubs_ext import StrOrPromise

class CustomItem(TypedDict):
    name: StrOrPromise
    value: str

@flaeppe
Copy link
Member

flaeppe commented Oct 30, 2023

No worries. As you said, now we hopefully have something that is more searchable. I'm gonna close this off for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants