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

Add a py.typed file #560

Open
robertsiipola opened this issue Aug 4, 2024 · 1 comment
Open

Add a py.typed file #560

robertsiipola opened this issue Aug 4, 2024 · 1 comment
Labels
good first issue Good for newcomers

Comments

@robertsiipola
Copy link

Add a py.typed file

Describe the chore

The library is missing a py.typed file. This adds friction when using it with a type checker like mypy as the type-checker will not recognise the types of the imported classes and methods.

Additional context

I'm using supabase_auth to implement authentication for my python app:

from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from supabase_auth import User
from supabase_auth.errors import AuthApiError
from supabase_auth.types import UserResponse

from supabase import Client, create_client
from myproject.config import get_supabase_settings


def get_client() -> Client:
    settings = get_supabase_settings()
    client = create_client(
        supabase_url=settings.supabase_url,
        supabase_key=settings.supabase_service_role_key,
    )

    return client


async def get_current_user(
    credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer()),
) -> User:
    token = credentials.credentials
    client = get_client()
    try:
        user_response: UserResponse = client.auth.get_user(token)
        return user_response.user
    except AuthApiError as e:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=f"Invalid authentication credentials: {e}",
        )
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"An error occurred while authenticating: {e}",
        )

When I run mypy with the following settings in my mypy.ini file:

[mypy]
disallow_untyped_defs = True
disallow_any_unimported = True
no_implicit_optional = True
check_untyped_defs = True
warn_return_any = True
warn_unused_ignores = True
show_error_codes = True

I get the following error:

mypy src/
src/myproject/auth.py:3: error: Skipping analyzing "supabase_auth": module is installed, but missing library stubs or py.typed marker  [import-untyped]
src/myproject/auth.py:4: error: Skipping analyzing "supabase_auth.errors": module is installed, but missing library stubs or py.typed marker  [import-untyped]
src/myproject/auth.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
src/myproject/auth.py:5: error: Skipping analyzing "supabase_auth.types": module is installed, but missing library stubs or py.typed marker  [import-untyped]
src/myproject/auth.py:21: error: Return type becomes "Coroutine[Any, Any, Any]" due to an unfollowed import  [no-any-unimported]
src/myproject/auth.py:27: error: Type of variable becomes "Any" due to an unfollowed import  [no-any-unimported]
Found 5 errors in 1 file (checked 12 source files)

I'm trying to work around this by autogenerating the types for mypy with stubgen, but that seems to fail as well.

@robertsiipola
Copy link
Author

I managed to get it to work with mypy by setting the disallow_any_unimported option to False and adding the # type: ignore comments whenever importing anything from supabase_auth.

@J0 J0 added the good first issue Good for newcomers label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants