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

Type Hints for Supabase-py #50

Closed
J0 opened this issue Sep 29, 2021 · 19 comments · Fixed by #96
Closed

Type Hints for Supabase-py #50

J0 opened this issue Sep 29, 2021 · 19 comments · Fixed by #96
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest Hacktoberfest 2022

Comments

@J0
Copy link
Collaborator

J0 commented Sep 29, 2021

It would be nice if we could get type hints on the library so that we can allow users to sanity check their code.

Thank you @dreinon for proposing this!

@J0 J0 added enhancement New feature or request good first issue Good for newcomers hacktoberfest Hacktoberfest 2022 labels Sep 29, 2021
@SwarajBaral
Copy link

Hey @J0 can I work on this ?

@dreinon
Copy link
Contributor

dreinon commented Oct 1, 2021

@SwarajBaral Sure 😄

@SwarajBaral
Copy link

Thank you @dreinon ,I will give it a go. Just a question before I begin. Should I add type hinting to a single file and create a PR first ? Just to check if I am going on the right path or not ?

@J0
Copy link
Collaborator Author

J0 commented Oct 1, 2021

@dreinon beat me to it and yes, go for it!

If you'd like, you can add hints to a single file and then request a review before moving on to the rest of the files.

@SwarajBaral
Copy link

Thank you for the reply @J0. I am on it.

@anand2312
Copy link
Contributor

I can help with this as well; @SwarajBaral wanna split work? 😄

@aantn
Copy link

aantn commented Oct 21, 2021

This is just a recommendation, but Pydantic supports autogenerating Python models from OpenAPI specs: https://pydantic-docs.helpmanual.io/datamodel_code_generator/

Might help with this.

@aantn
Copy link

aantn commented Oct 21, 2021

On second thought, I think I'm misinterpreting this issue :)

The pydantic code generation is useful if you want to add type hints for the user's tables in Supabase, not to add typehints to the Supabase codebase itself.

@dreinon
Copy link
Contributor

dreinon commented Oct 21, 2021

Right @aantn.

Nevertheless, it's a good point what you are saying, since it would help in the job of keeping updated the code if there is any change in the database.

For instance, we could have the models generated from the OpenAPI of the db and use them when we do an insert, first instancing the Pydantic model to validate the format is okay, and then passing to the insert method instance_of_model.dict().

Do we have OpenAPI specs ready from postgrest?

Maybe we can open a discussion for this 😊

@aantn
Copy link

aantn commented Oct 21, 2021

@dreinon exactly. Yes, there are already OpenAPI specs from Postgrest. You can use them to generate types for the typescript bindings in a similar manner: https://supabase.io/docs/reference/javascript/generating-types

@dreinon
Copy link
Contributor

dreinon commented Oct 28, 2021

@aantn @J0 It would be nice to open an issue for a tutorial on how to create pydantic models from OpenAPI specs, which could also be referenced in the FastAPI tutorial since it uses pydantic models to validate and parse bodies of requests.

@dreinon
Copy link
Contributor

dreinon commented Oct 28, 2021

Ping @SwarajBaral. How are you doing with this issue? Maybe you can split the work with @anand2312 or even let him go forth with the issue in case you didn't have time to do much. Thanks! 😊

@aantn
Copy link

aantn commented Oct 28, 2021

@dreinon I can write a blog post for that if you like and publish it on my personal blog.

@dreinon
Copy link
Contributor

dreinon commented Oct 28, 2021

@dreinon I can write a blog post for that if you like and publish it on my personal blog.

@J0 is there an exact place where tutorials have to go? Or can they be written anywhere and then we can reference them in the docs?

@J0
Copy link
Collaborator Author

J0 commented Oct 30, 2021

@aantn , go for it! It would be great if you could post a link here once you're done so that we can link to it

@dreinon Don't think we have docs yet :( There is an examples page but it is still WIP We can definitely link to tutorials once we have a docs page though

@dreinon
Copy link
Contributor

dreinon commented Nov 15, 2021

@leynier @anand2312 feel free to take this issue and split the work if you want since we got no response from @SwarajBaral yet.

@joeriddles
Copy link
Contributor

joeriddles commented Dec 8, 2021

Many modules and functions in this code base look to already be using type hints (typed).

Here are the functions I see that are not typed or are missing some type hints (so far):

  • client.py
    • DEFAULT_OPTIONS
    • def storage
      • does not have an explicit return type.
    • def table
      • does not have an explicit return type.
    • def from_
      • does not have an explicit return type.
    • def rpc
      • function arguments are not typed.
      • does not have an explicit return type.
    • async def _close_subscription
      • function arguments are not typed.
      • does not have an explicit return type.
    • def get_subscriptions
      • does not have an explicit return type.
    • def create_client
      • **options could use the same type hint as DEFAULT_OPTIONS
  • request_builder.py
  • lib/storage_client.py
    • def __init__
      • function arguments are not typed.
    • def StorageFileAPI
      • function arguments are not typed.
      • does not have an explicit return type.
  • lib/realtime_client.py
    • def get_payload_records
      • payload argument uses Any. Perhaps a better type could be used instead.
      • does not have an explicit return type.

...

@joeriddles
Copy link
Contributor

joeriddles commented Dec 8, 2021

I've been manually looking at files for missing type hints. Trying to use mypy to do this automatically. Here are my results so far:

> mypy -p supabase
pyproject.toml: Only all lowercase booleans allowed (line 25 column 1 char 733)
supabase\request_builder.py:1: error: Cannot find implementation or library stub for module named "httpx"
supabase\request_builder.py:2: error: Cannot find implementation or library stub for module named "postgrest_py.request_builder"
supabase\lib\storage\storage_file_api.py:3: error: Cannot find implementation or library stub for module named "httpx"
supabase\lib\realtime_client.py:3: error: Cannot find implementation or library stub for module named "realtime_py.connection"
supabase\lib\realtime_client.py:4: error: Cannot find implementation or library stub for module named "realtime_py.transformers"
supabase\lib\query_builder.py:3: error: Cannot find implementation or library stub for module named "httpx"
supabase\lib\query_builder.py:5: error: Cannot find implementation or library stub for module named "postgrest_py.client"
supabase\lib\query_builder.py:6: error: Cannot find implementation or library stub for module named "postgrest_py.request_builder"
supabase\lib\auth_client.py:3: error: Cannot find implementation or library stub for module named "gotrue"
supabase\lib\storage\storage_bucket_api.py:8: error: Cannot find implementation or library stub for module named "httpx"
supabase\lib\storage\storage_bucket_api.py:109: error: Return value expected
supabase\client.py:3: error: Cannot find implementation or library stub for module named "postgrest_py"
supabase\client.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
supabase\client.py:51: error: "object" has no attribute "update"
supabase\client.py:56: error: Incompatible types in assignment (expression has type "object", variable has type "str")
supabase\client.py:62: error: Argument 3 to "_init_supabase_auth_client" of "Client" has incompatible type "**Dict[str, object]"; expected "bool"
supabase\client.py:62: error: Argument 3 to "_init_supabase_auth_client" of "Client" has incompatible type "**Dict[str, object]"; expected "Dict[str, Any]"
supabase\client.py:62: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
supabase\client.py:62: note: Consider using "Mapping" instead, which is covariant in the value type
supabase\client.py:62: error: Argument 3 to "_init_supabase_auth_client" of "Client" has incompatible type "**Dict[str, object]"; expected "Dict[str, str]"
supabase\client.py:73: error: Argument 3 to "_init_postgrest_client" of "Client" has incompatible type "**Dict[str, object]"; expected "Dict[str, str]"
supabase\client.py:153: error: Missing positional argument "table_name" in call to "SupabaseRealtimeClient"
supabase\client.py:154: error: Argument 2 to "SupabaseRealtimeClient" has incompatible type "Dict[str, Dict[str, str]]"; expected "str"
Found 20 errors in 7 files (checked 12 source files)

This is using mypy will the following config options added to pyproject.toml

[mypy]
disallow_any_unimported = True
disallow_any_expr = True
disallow_any_decorated = True
disallow_any_explicit = True
disallow_any_generics = True
disallow_subclassing_any = True

disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True

warn_redundant_casts = True
warn_unused_ignores = True

@anand2312
Copy link
Contributor

@joeriddles Thanks for compiling this list! Helps a lot 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers hacktoberfest Hacktoberfest 2022
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants