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 authentication.requires type #1659

Merged
merged 5 commits into from
Jun 11, 2022
Merged

Conversation

sobolevn
Copy link
Contributor

I've noticed that authentication.requires does not preserve type information about the passed function.

So, I've fixed that!

from starlette.authentication import requires


@requires(['authenticated', 'admin'])
async def dashboard(request: str) -> int:  # Don't mind these fake types here
    ...

reveal_type(dashboard)
# Before:
# note: Revealed type is "Any"

# After:
# note: Revealed type is "def (request: builtins.str) -> typing.Coroutine[Any, Any, builtins.int]"

@Kludex Kludex added the hold Don't merge it label May 31, 2022
@Kludex
Copy link
Sponsor Member

Kludex commented Jun 1, 2022

I like the enthusiasm! Thanks for the PR @sobolevn ! 🙇

But... This is not the right annotation, that's why the # type: ignore was needed. Besides, I think that's something we need to discuss first, as it opens precendece to a lot of annotations in code.

For those reasons, I'll be closing this PR.

@Kludex Kludex closed this Jun 1, 2022
@sobolevn
Copy link
Contributor Author

sobolevn commented Jun 1, 2022

But... This is not the right annotation

It looks right to me 🤔
Can you please point out what's wrong?

This is the common way of expressing "this function returns a decorator that preserves the signature".
I might be missing something 🙂

that's why the # type: ignore was needed

type: ignore is needed, because we cannot really proove to mypy that _CallableType is the same thing as decorator. Mypy cannot inference that by default. There are two ways:

  • cast() with runtime effect (small, but still)
  • # type: ignore which does not have this effect

Besides, I think that's something we need to discuss first, as it opens precendece to a lot of annotations in code.

Sure thing, I will open an issue about that.
Update: #1662

@adriangb
Copy link
Member

adriangb commented Jun 9, 2022

@Kludex I do think this is the right annotation, unless we want to be more explicit and encode Callable[[Request], Response] in there to avoid a typevar.

@Kludex Kludex reopened this Jun 10, 2022
@@ -95,7 +97,7 @@ def sync_wrapper(*args: typing.Any, **kwargs: typing.Any) -> Response:

return sync_wrapper

return decorator
return decorator # type: ignore
Copy link
Sponsor Member

@Kludex Kludex Jun 10, 2022

Choose a reason for hiding this comment

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

Can we add at least the error on ignore[<error>]? 🙏

@@ -95,7 +97,7 @@ def sync_wrapper(*args: typing.Any, **kwargs: typing.Any) -> Response:

return sync_wrapper

return decorator
return decorator # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return decorator # type: ignore
return decorator # type: ignore[return-value] # type checkers can't verify the type of the wrapper

Copy link
Sponsor Member

@Kludex Kludex Jun 10, 2022

Choose a reason for hiding this comment

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

i'm already happy with just the return-value thingy, no need the comment stuff

@Kludex Kludex removed the hold Don't merge it label Jun 11, 2022
@Kludex
Copy link
Sponsor Member

Kludex commented Jun 11, 2022

But... This is not the right annotation

It looks right to me thinking Can you please point out what's wrong?

This is the common way of expressing "this function returns a decorator that preserves the signature". I might be missing something slightly_smiling_face

that's why the # type: ignore was needed

type: ignore is needed, because we cannot really proove to mypy that _CallableType is the same thing as decorator. Mypy cannot inference that by default. There are two ways:

  • cast() with runtime effect (small, but still)
  • # type: ignore which does not have this effect

Besides, I think that's something we need to discuss first, as it opens precendece to a lot of annotations in code.

Sure thing, I will open an issue about that. Update: #1662

Thanks for the explanation! I guess I got excited closing PRs that day. 😰

I retract my words from before, and sorry for closing the PR. :)

@Kludex Kludex added this to the Version 0.21.0 milestone Jun 11, 2022
@adriangb adriangb merged commit daf2913 into encode:master Jun 11, 2022
@adriangb
Copy link
Member

Thank you for the contribution @sobolevn !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants