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

[Fix] Allow StrPromise to be used in exceptions #297

Merged
merged 5 commits into from
Jan 4, 2023

Conversation

XF-FW
Copy link
Contributor

@XF-FW XF-FW commented Nov 18, 2022

I have made things!

Related issues

I didn't create an issue, but it's the mirror of typeddjango/django-stubs#1137, for this repo. There might be more places where the same should be applied, but this resolves my issue.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please add new ignores

@XF-FW
Copy link
Contributor Author

XF-FW commented Nov 24, 2022

Done. I'm getting a error however, which seems to be related to encode/django-rest-framework#8727. I'm unsure on how to proceed.

@@ -15,7 +16,7 @@ class ErrorDetail(str):
code: str | None
def __new__(cls, string: str, code: str | None = ...): ...

_Detail: TypeAlias = str | list[Any] | dict[str, Any]
_Detail: TypeAlias = str | _StrPromise | list[Any] | dict[str, Any]
Copy link
Contributor

@intgr intgr Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't import private types from django-stubs if it can be avoided. Use StrPromise from django_stubs_ext.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I notice we currently don't have an explicit dependency on django-stubs-ext in setup.py. IMO that should that be added, thoughts @sobolevn?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this could use the StrOrPromise type alias:

Suggested change
_Detail: TypeAlias = str | _StrPromise | list[Any] | dict[str, Any]
_Detail: TypeAlias = StrOrPromise | list[Any] | dict[str, Any]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

django-stubs-ext is a dependency of django-stubs. I don't think we need to duplicate it 🤔

Copy link

@mschoettle mschoettle Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to hijack this discussion but I can't find any specific documentation anywhere about this (I am digging through the issues to figure out new mypy errors after upgrading django-stubs). Just want to confirm that when I have code that uses gettext lazy strings I should use either StrPromise or StrOrPromise (depending on the specific case) from django-stubs-ext.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not getting back to you earlier.

StrOrPromise accepts both str and StrPromise.

StrPromise only allows StrPromse, but not regular str.

In nearly all cases StrOrPromise is the correct one.

@intgr
Copy link
Contributor

intgr commented Dec 2, 2022

The CI isssue will be hopefully fixed in #303

@intgr
Copy link
Contributor

intgr commented Dec 3, 2022

I rebased this and the CI now passes.

@XF-FW
Copy link
Contributor Author

XF-FW commented Dec 22, 2022

I seem to have missed the notifications on this PR.

I'll make the changes that @intgr has suggested, so please hold off merging for a bit.

To clarify, it's replacing str | _StrPromise with StrOrPromise, is that correct?

@intgr
Copy link
Contributor

intgr commented Dec 22, 2022

Yep 👍

@@ -4,6 +4,7 @@ from typing import Any
from typing_extensions import TypeAlias

from django.http import HttpRequest, JsonResponse
from django.utils.functional import _StrPromise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my, looks like I have confused _StrPromise with _StrOrPormise as well 🤦

Naming!

@mschoettle
Copy link

mschoettle commented Dec 30, 2022

Thanks! I ran into the same issue when doing the following:

raise serializers.ValidationError(_('Foo'))

This PR fixes this issue.

Question regarding the use of django-stubs-ext: Does this require django-stubs-ext to be added to the dependencies in setup.py? (based on this review in another MR this is necessary: #301 (comment))

@intgr
Copy link
Contributor

intgr commented Jan 4, 2023

Question regarding the use of django-stubs-ext: Does this require django-stubs-ext to be added to the dependencies in setup.py? (based on this review in another MR this is necessary: #301 (comment))

I don't have strong feelings about that. In some other PR comments, sobolevn felt that it's not necessary. django-stubs aready depends on django-stubs-ext and as long as that remains, this will work. Let's leave it be.

@intgr intgr changed the title [Fix] Allow _StrPromise to be used in exceptions [Fix] Allow StrPromise to be used in exceptions Jan 4, 2023
@intgr intgr merged commit 728da9c into typeddjango:master Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants