Skip to content

Add CurrentUserDefault #19

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

Merged
merged 1 commit into from
Dec 12, 2019
Merged

Conversation

noamkush
Copy link
Contributor

No description provided.

@@ -39,6 +39,10 @@ class CreateOnlyDefault(object):
def set_context(self, serializer_field: Field) -> None: ...
def __call__(self) -> Any: ...

class CurrentUserDefault(object):
def set_context(self, serializer_field: Field) -> None: ...
def __call__(self) -> Any: ...
Copy link
Member

Choose a reason for hiding this comment

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

@mkurnikov are we okay with Any in these cases?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I somehow missed it. Just add some # TODO: for later, with explanation why it's difficult to add return type now, and it's good to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I pushed a change to make it Union[AnonymousUser, User] which is what get_user returns. This assumes both AuthenticationMiddleware is used and there's no custom user model in use.

@noamkush noamkush force-pushed the current_user_default branch from f7571e9 to c0e28ce Compare July 11, 2019 12:56
@noamkush noamkush requested a review from mkurnikov November 3, 2019 06:51
@sobolevn
Copy link
Member

This should had be merged now, sorry for the long delay!

P.S. facing the same issue right now:

server/rest_app/logics/serializers.py:21: error: Name 'fields.CurrentUserDefault' is not defined

@sobolevn sobolevn merged commit c580e8a into typeddjango:master Dec 12, 2019
@noamkush noamkush deleted the current_user_default branch December 12, 2019 14:19
@sobolevn
Copy link
Member

@mkurnikov
Copy link
Member

@noamkush Sorry=)

@sobolevn Actually, it should've been Union[AnonymousUser, Model], no? Though it would create false-positives too, so I guess there's no difference...

@sobolevn
Copy link
Member

sobolevn commented Dec 12, 2019

It has actually changed is the most recent version of drf.

Now it is:

# TODO: fix when drf-stubs > 1.1 will be released
class _CurrentAppUserDefault(fields.CurrentUserDefault):  # type: ignore
    def __call__(self, serializer_field):
        return serializer_field.context['request'].user.link.profile

But again, we need to understand how to work with different versions of annotations.
Should we raise this question in mypy's bugtracker?

@mkurnikov
Copy link
Member

@sobolevn I've created one here python/typing#693

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.

3 participants