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

Setup types for Robyn #192

Merged
merged 5 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove generic type stubs
  • Loading branch information
sansyrox committed May 8, 2022
commit 386ebe86a829e4ba3bf7650042c9d6d7f2e7351d
56 changes: 17 additions & 39 deletions robyn/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import os
import asyncio
import multiprocessing as mp
import os
from inspect import signature
from typing import Callable

from multiprocess import Process
from watchdog.observers import Observer

from robyn.events import Events
from .robyn import Server, SocketHeld

from .argument_parser import ArgumentParser
from .responses import jsonify, static_file
from .dev_event_handler import EventHandler
from .processpool import spawn_process
from .log_colors import Colors
from .ws import WS
from .processpool import spawn_process
from .responses import jsonify, static_file
from .robyn import Server, SocketHeld
from .router import MiddlewareRouter, Router, WebSocketRouter
from multiprocess import Process
from watchdog.observers import Observer

from typing import Callable

from .ws import WS

class Robyn:
"""This is the python wrapper for the Robyn binaries."""

def __init__(self, file_object: str) -> None:
...

def __init__(self, file_object: str) -> None: ...
def before_request(self, endpoint: str) -> Callable[..., None]:
"""
The @app.before_request decorator to add a get route
Expand All @@ -31,7 +30,6 @@ class Robyn:
"""

...

def after_request(self, endpoint: str): # -> (handler: Unknown) -> None:
"""
[The @app.after_request decorator to add a get route]
Expand All @@ -40,28 +38,17 @@ class Robyn:
"""

...

def add_directory(
self,
route: str,
directory_path: str,
index_file: str = ...,
show_files_listing: str = ...,
):
...

def add_header(self, key: str, value: str) -> None:
...

def add_web_socket(self, endpoint: str, ws: WS) -> None:
...

def startup_handler(self, handler: Callable) -> None:
...

def shutdown_handler(self, handler: Callable) -> None:
...

): ...
def add_header(self, key: str, value: str) -> None: ...
def add_web_socket(self, endpoint: str, ws: WS) -> None: ...
def startup_handler(self, handler: Callable) -> None: ...
def shutdown_handler(self, handler: Callable) -> None: ...
def start(self, url: str = ..., port: int = ...) -> None:
"""
Starts the server
Expand All @@ -70,23 +57,20 @@ class Robyn:
"""

...

def get(self, endpoint: str) -> Callable[..., None]:
"""
The @app.get decorator to add a get route

:param endpoint str: endpoint to server the route
"""
...

def post(self, endpoint: str) -> Callable[..., None]:
"""
The @app.post decorator to add a get route

:param endpoint str: endpoint to server the route
"""
...

def put(self, endpoint: str) -> Callable[..., None]:
"""
The @app.put decorator to add a get route
Expand All @@ -95,7 +79,6 @@ class Robyn:
"""

...

def delete(self, endpoint: str) -> Callable[..., None]:
"""
The @app.delete decorator to add a get route
Expand All @@ -104,7 +87,6 @@ class Robyn:
"""

...

def patch(self, endpoint: str) -> Callable[..., None]:
"""
The @app.patch decorator to add a get route
Expand All @@ -113,7 +95,6 @@ class Robyn:
"""

...

def head(self, endpoint: str) -> Callable[..., None]:
"""
The @app.head decorator to add a get route
Expand All @@ -122,7 +103,6 @@ class Robyn:
"""

...

def options(self, endpoint: str) -> Callable[..., None]:
"""
The @app.options decorator to add a get route
Expand All @@ -131,15 +111,13 @@ class Robyn:
"""

...

def connect(self, endpoint: str) -> Callable[..., None]:
"""
The @app.connect decorator to add a get route

:param endpoint str: endpoint to server the route
"""
...

def trace(self, endpoint: str) -> Callable[..., None]:
"""
The @app.trace decorator to add a get route
Expand Down
17 changes: 4 additions & 13 deletions robyn/argument_parser.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import argparse


class ArgumentParser(argparse.ArgumentParser):

def __init__(self) -> None:
...

def num_processes(self):
...

def workers(self):
...

def is_dev(self):
...
def __init__(self) -> None: ...
def num_processes(self): ...
def workers(self): ...
def is_dev(self): ...
11 changes: 3 additions & 8 deletions robyn/dev_event_handler.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from watchdog.events import FileSystemEventHandler, FileSystemEvent

from watchdog.events import FileSystemEvent, FileSystemEventHandler

class EventHandler(FileSystemEventHandler):
def __init__(self, file_name: str) -> None:
...

def start_server_first_time(self) -> None:
...

def __init__(self, file_name: str) -> None: ...
def start_server_first_time(self) -> None: ...
def on_any_event(self, event: FileSystemEvent) -> None:
"""
[This function is a callback that will start a new server on every even change]
Expand Down
11 changes: 4 additions & 7 deletions robyn/processpool.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from numbers import Number
from robyn.ws import WS
from typing import Dict, Tuple

from robyn.events import Events
from robyn.robyn import SocketHeld
from robyn.router import Route
from robyn.ws import WS

from typing import Dict, Tuple, Callable

Route = Tuple[str, str, Callable, bool, int]
Directory = Tuple[str, str, str, str]
Header = Tuple[str, str]


def spawn_process(
directories: Tuple[Directory, ...],
headers: Tuple[Header, ...],
Expand All @@ -34,5 +33,3 @@ def spawn_process(
:param process_name string: This is the name given to the process to identify the process
:param workers number: This is the name given to the process to identify the process
"""

pass
2 changes: 0 additions & 2 deletions robyn/responses.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any


def static_file(file_path: str) -> dict[str, Any]:
"""
[This function will help in serving a static_file]
Expand All @@ -10,7 +9,6 @@ def static_file(file_path: str) -> dict[str, Any]:

pass


def jsonify(input_dict: dict) -> str:

Choose a reason for hiding this comment

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

Even if the type arguments for a generic type don't matter in that context you should still use Any or object in its place. This is because in strict mode, pyright will report an error if a type annotation is unknown (i.e. missing type arguments).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you @RobertCraigie! I will include it in the next release! :D

"""
[This function serializes input dict to a json string]
Expand Down
12 changes: 1 addition & 11 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
from __future__ import annotations
from typing import Callable, Optional, Tuple

from typing import Callable, Optional, Tuple

class SocketHeld:
def __init__(self, url: str, port: int):
pass

def try_clone(self) -> SocketHeld:
pass


class Server:
def __init__(self):
pass

def add_directory(
self,
route: str,
Expand All @@ -22,10 +19,8 @@ class Server:
show_files_listing: bool,
):
pass

def add_header(self, key: str, value: str):

Choose a reason for hiding this comment

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

Even if the method just returns None it's generally preferable to be explicit about that to avoid any potential differences in inference between type checkers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it! I will change it in the next release! :D

pass

def add_route(
self,
route_type: str,
Expand All @@ -35,7 +30,6 @@ class Server:
number_of_params: int,
):
pass

def add_middleware_route(
self,
route_type: str,
Expand All @@ -45,13 +39,10 @@ class Server:
number_of_params: int,
):
pass

def add_startup_handler(self, handler: Callable, is_async: bool):
pass

def add_shutdown_handler(self, handler: Callable, is_async: bool):
pass

def add_web_socket_route(
self,
route: str,
Expand All @@ -60,6 +51,5 @@ class Server:
message_route: Tuple[Callable, bool, int],
):
pass

def start(self, socket: SocketHeld, workers: int):
pass
19 changes: 5 additions & 14 deletions robyn/router.pyi
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
from abc import ABC, abstractmethod
from typing import Callable, Tuple

from robyn.ws import WS
from typing import Callable

Route = Tuple[str, str, Callable, bool, int]

class BaseRouter(ABC):
@abstractmethod
def add_route(*args) -> None:
pass


class Router(BaseRouter):
def __init__(self) -> None:
pass

def add_route(self, route_type: str, endpoint: str, handler: Callable) -> None:
pass

def get_routes(self) -> list:
def get_routes(self) -> list[Route]:
pass


class MiddlewareRouter(BaseRouter):
def __init__(self) -> None:
pass

def add_route(self, route_type: str, endpoint: str, handler: Callable) -> None:
pass

def add_after_request(self, endpoint: str) -> Callable[..., None]:
pass

def add_before_request(self, endpoint: str) -> Callable[..., None]:
pass

def get_routes(self) -> list:
def get_routes(self) -> list[Route]:
pass


class WebSocketRouter(BaseRouter):
def __init__(self) -> None:
pass

def add_route(self, endpoint: str, web_socket: WS) -> None:
pass

def get_routes(self) -> dict[str, WS]:
pass
6 changes: 2 additions & 4 deletions robyn/ws.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from robyn import Robyn


class WS:
"""This is the python wrapper for the web socket that will be used here.
"""
"""This is the python wrapper for the web socket that will be used here."""

def __init__(self, robyn_object: Robyn, endpoint: str) -> None:
pass

def on(self, type: str):
pass