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

Started typing codebase #1299

Closed
wants to merge 24 commits into from
Closed

Started typing codebase #1299

wants to merge 24 commits into from

Conversation

WisdomPill
Copy link

Fixes #893

First of all, I want to point out that this is a process and maybe it will not be completed in every piece of code right away with just one pull request.
I started adding typing only on functions to the project but I have some questions about some parts of the code.

  1. The user is always a Union[User, AnonymousUser]?

  2. Is the message in Consumers always a dictionary?
    I have a problem with AsyncHttpConsumer that throws a strange exception

    TypeError: 'type' object is not subscriptable
    
  3. Is it mandatory to put the output if there's no return value?

  4. The code in close method of web sockets can be a number or a bool, not anything else?

By the way this is my first pull request to the project, so if there's anything else I should do

channels/auth.py Outdated Show resolved Hide resolved
channels/generic/http.py Outdated Show resolved Hide resolved
@WisdomPill
Copy link
Author

Now only the last two points need some clarification.

  1. Is it mandatory to put the output if there's no return value?

  2. The code in close method of web sockets can be a number or a bool, not anything else?

channels/auth.py Outdated
@@ -20,7 +22,7 @@


@database_sync_to_async
def get_user(scope):
def get_user(scope: Dict[str, Any]) -> Awaitable[Union[User, AnonymousUser]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

would it not make more sense to make the database_sync_to_async be generic

def database_sync_to_async(method: Callable[SomeGenericVar...]) -> Callable[ Awaitable[TheGenericReturnOfTHeCallable]]
....

Copy link
Author

Choose a reason for hiding this comment

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

It's not that easy, database_sync_to_async inherits from SyncToAsync class from asgiref library.
Are you suggesting that I should inherit the call method to then type it like you wrote?

Copy link
Contributor

Choose a reason for hiding this comment

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

good point, no lets just leave that as is.

channels/auth.py Outdated Show resolved Hide resolved
channels/sessions.py Outdated Show resolved Hide resolved
channels/auth.py Outdated Show resolved Hide resolved
@WisdomPill
Copy link
Author

We'll see, in the mean time I have to keep in mind compatibility with python 3.5.

How should I deal with import sorting? Is there a tool for sorting them before a commit or a configuration in the editor to enable?

@WisdomPill
Copy link
Author

Never mind I figured out how to use black and isort

@WisdomPill WisdomPill marked this pull request as ready for review June 22, 2019 14:51
@WisdomPill
Copy link
Author

I'm waiting for someone to review the PR.

@matthiask @carltongibson @hishnash anyone?

Can you please give some feedback or just merge the PR?

@carltongibson
Copy link
Member

HI @WisdomPill. I'm currently working on an update for Daphne. Then I'll cut back to channels. I'll have a look at this then.

Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

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

Hi @WisdomPill.

This looks good. Thank you for the effort.

Can I ask a question? Is there any shorthand for this:

Dict[str, Any]

Can we define an alias so we can just pass, say StrDict — that's rubbish but you get the idea?

(Like 99% of Dicts are Dict[str, Any] so it seems quite boilerplatey...)

@WisdomPill
Copy link
Author

@carltongibson done!

@carltongibson
Copy link
Member

Hi @WisdomPill. That was quick. 🙂 Still not sure about StrDict per se but yes.

I'm looking at releases here for Sept, so will do something with this for then. Good work!

Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

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

HI @WisdomPill.

Sorry for the slow follow-up here. This has been bugging me greatly, and I haven't had a chance to get to it. Anyhow...

Please create a types or typing module — What do Python folks do here? —

Then create proper type alias wherever you have something like Optional[Tuple[str, Tuple[str, Any], StrDict]]. I simply don't want to look at that. 🙂 Instead I want to see RouteMatch or such...

Then we'll hopefully get something meaningful. i.e. a scope is an AsgiScope (or just Scope), which if I'm interested I'll go and see is a Dict[Str: Any] — but only certain keys are allowed, so can we tighten that? (if we can't tighten it now, maybe we will over time. Maybe we'll get asgiref to declare that type, and so on...)

Liberal use of type aliases is the key to success here. (As they do in Haskell and friends.)

Make sense?

Thanks!

channels/auth.py Outdated Show resolved Hide resolved
channels/utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

fix the merge error plz

@WisdomPill
Copy link
Author

@carltongibson is there anything left in order to merge the pull request?

@carltongibson
Copy link
Member

@WisdomPill Just me getting another chance to review it. 🙂 Thank you for your efforts here!

@carltongibson carltongibson self-requested a review October 1, 2019 16:48
@WisdomPill WisdomPill requested a review from auvipy November 17, 2019 08:34
@nscozzaro
Copy link

Stumbled upon this issue because I was looking to see the status of type hinting in Django, so I'd love to see these changes pulled!

Copy link
Author

@WisdomPill WisdomPill left a comment

Choose a reason for hiding this comment

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

I should have fixed everything in the following commits, closing che changes request.

Sorry I just discovered this feature, I'm a noob in open source software

@WisdomPill
Copy link
Author

@carltongibson just a friendly reminder to review the changes and to point out that I alligned the changes with channels/master.

@carltongibson
Copy link
Member

Hi @WisdomPill. Thanks for your efforts here. I'm just waiting to see what happens with the discussion on Django before coming back here.

Base automatically changed from master to main March 3, 2021 18:20
@carltongibson
Copy link
Member

Gonna close this for now. Further discussion on #1819.

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.

Add type hints
6 participants