Skip to content

Refactor/vendorize typings #1312

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

Closed
wants to merge 5 commits into from
Closed

Conversation

tony
Copy link

@tony tony commented Jan 8, 2022

In re: #1305

I'm having difficulty with if making switches that work between ASGIREF_33/ASGIREF_34. Apparently type annotations can't handle that since it's more of a runtime thing?

@Kludex Is this possible to do without watering the typings down too much?

  • What's done _types.py being backported to 3.3.2 and removed asgiref as a dependency
  • version checking without needing distutils/packaging

It can do branching like a _compat.py module would, but mypy considers things like:

if ASGIREF_33:

    class HTTPScope(TypedDict):
        type: Literal["http"]
        asgi: ASGISpecInfo
        http_version: str
        method: str
        scheme: str
        path: str
        raw_path: bytes
        query_string: bytes
        root_path: str
        headers: Iterable[Tuple[bytes, bytes]]
        client: Optional[Tuple[str, int]]
        server: Optional[Tuple[str, Optional[int]]]
        extensions: Dict[str, Dict[object, object]]


elif ASGIREF_34:

    class HTTPScope(TypedDict):
        type: Literal["http"]
        asgi: ASGISpecInfo
        http_version: str
        method: str
        scheme: str
        path: str
        raw_path: bytes
        query_string: bytes
        root_path: str
        headers: Iterable[Tuple[bytes, bytes]]
        client: Optional[Tuple[str, int]]
        server: Optional[Tuple[str, Optional[int]]]
        extensions: Optional[Dict[str, Dict[object, object]]]

to be duplication.

And it's also not possible to override or extend:

  class HTTPScope(TypedDict):
      type: Literal["http"]
      asgi: ASGISpecInfo
      http_version: str
      method: str
      scheme: str
      path: str
      raw_path: bytes
      query_string: bytes
      root_path: str
      headers: Iterable[Tuple[bytes, bytes]]
      client: Optional[Tuple[str, int]]
      server: Optional[Tuple[str, Optional[int]]]
      extensions: Dict[str, Dict[object, object]]

if ASGIREF_34:

    class HTTPScope(HTTPScope):
        extensions: Optional[Dict[str, Dict[object, object]]]

I suppose it's because that requires runtime that mypy isn't meant to analyze

Thoughts on this? I've allowed edits and you're welcome to fork my branch refactor/vendorize-typings

@tony tony force-pushed the refactor/vendorize-typings branch from aef35d9 to 7e4e8ba Compare January 8, 2022 18:32
@tony tony mentioned this pull request Jan 8, 2022
2 tasks
@Kludex
Copy link
Member

Kludex commented Jan 8, 2022

I was thinking more about ignoring the asgiref version, vendoring the current asgiref.typing, and testing comparing the types from the latest asigref.typing to the vendored file.

EDIT: Remember to include the copyright notice.

@tony
Copy link
Author

tony commented Jan 8, 2022

Some asgiref.typing history (for posterity):

  • 2.3.10 and 3.3.0 - 3.3.1 don't have this
  • 3.3.2 added
  • 3.4.1 - clarification of {HTTP,Websocket}Scope's extension attribute, Websocket -> WebSocket` (I didn't realize today you actually made those contributions, nice!)

testing comparing the types from the latest asigref.typing to the vendored file.

Sorry, not 100% sure what this would look like. Can you clarify?

P.S. If we were to vendorize the typings, they wouldn't necessarily hold true for 3.2.10 as asgiref.typing doesn't exist. In 3.3.4 the are different but perhaps this can be worked around for ones where the class name is different (Websocket* -> WebSocket*) Would the typings only be expected to work with the latest asgiref release?

I should also clarify: Is the use of the typing purely internal, or is it also intended to be used by people implementing the package/using it as a library in some fashion?

EDIT: Remember to include the copyright notice.

For asgiref right? In the pydoc for the file?

@tony tony force-pushed the refactor/vendorize-typings branch from 7e4e8ba to fac0b4e Compare February 11, 2022 12:57
@Kludex Kludex added this to the Version 0.1x.x milestone Apr 23, 2022
@Kludex
Copy link
Member

Kludex commented May 6, 2022

Thanks for the efforts here @tony! 🙇

Sorry for taking so long to act.

I'll be taking this over on #1476.

@Kludex Kludex closed this May 6, 2022
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.

2 participants