authzed / authzed-py

Official SpiceDB client library for Python
https://docs.authzed.com/reference/api
Apache License 2.0
33 stars 13 forks source link

Add type hint support #140

Closed intentionally-left-nil closed 6 months ago

intentionally-left-nil commented 7 months ago

The authzed library already ships with most of the necessary type hints in *.pyi files. However, mypy can't use this because:

  1. There's no py.typed file in the package so these types can't be used at all
  2. Even if the file were there, there's no types for __init__.py
  3. In __init__.py the type for the Client is wrong/ really hard to implement because it could either be the Async* versions or the Sync versions

E.g. depending on whether you're in an async loop, the correct definition is either

class Client(SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub, WatchServiceStub):
  ...

OR

class Client(SchemaServiceAsyncStub, PermissionsServiceAsyncStub, ExperimentalServiceAsyncStub, WatchServiceAsyncStub):
  ...

So, largely we need to solve the __init__.py file before we can continue. I have one concrete suggestion: Make a separate AsyncClient which has the latter type signature. Then, make a new SyncClient which is just an alias to the existing Client. That will allow the type hints to be straightforward.

Then, if we add the type hints directly to the __init__.py file for the init method, we wouldn't need a separate __init__.pyi file since the init file would have all of the necessary type hints