aliev / aioauth

Asynchronous OAuth 2.0 provider for Python 3
https://aliev.me/aioauth
MIT License
214 stars 19 forks source link

fix: mypy errors on custom models #66

Closed aliev closed 2 years ago

aliev commented 2 years ago

see the issue #63

while inheriting from models (Token, Client, AuthorizationCode), and using them in aioauth, mypy throws an error like:

error: Argument 1 of "create_authorization_code" is incompatible with supertype "BaseStorage"; supertype defines the argument type as "Request"

this PR fixes the above bug by adding additional TToken, TClient and TAuthorizationCode parameters to the BaseModel generic.

usage example:

from dataclasses import dataclass
from aioauth_fastapi.router import get_oauth2_router
from aioauth.storage import BaseStorage
from aioauth.requests import BaseRequest
from aioauth.models import AuthorizationCode, Client, Token
from aioauth.config import Settings
from aioauth.server import AuthorizationServer
from fastapi import FastAPI

app = FastAPI()

@dataclass
class User:
    """Custom user model"""
    first_name: str
    last_name: str

@dataclass
class Request(BaseRequest[Query, Post, User]):
    """"Custom request"""

class Storage(BaseStorage[Token, Client, AuthorizationCode, Request]):
    """
    Storage methods must be implemented here.
    """

storage = Storage()
authorization_server = AuthorizationServer[Request, Storage](storage)

# NOTE: Redefinition of the default aioauth settings
# INSECURE_TRANSPORT must be enabled for local development only!
settings = Settings(
    INSECURE_TRANSPORT=True,
)

# Include FastAPI router with oauth2 endpoints.
app.include_router(
    get_oauth2_router(authorization_server, settings),
    prefix="/oauth2",
    tags=["oauth2"],
)
codecov-commenter commented 2 years ago

Codecov Report

Merging #66 (f05823e) into master (da68dbf) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master       #66   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines          648       659   +11     
  Branches       117       118    +1     
=========================================
+ Hits           648       659   +11     
Impacted Files Coverage Δ
aioauth/errors.py 100.00% <100.00%> (ø)
aioauth/grant_type.py 100.00% <100.00%> (ø)
aioauth/models.py 100.00% <100.00%> (ø)
aioauth/requests.py 100.00% <100.00%> (ø)
aioauth/response_type.py 100.00% <100.00%> (ø)
aioauth/server.py 100.00% <100.00%> (ø)
aioauth/storage.py 100.00% <100.00%> (ø)
aioauth/utils.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

pedropregueiro commented 2 years ago

hey @aliev, is this one releasable now or you wanna wait to test some more?