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

[BUG] auth/AuthProvider type hint incorrect #1776

Closed
2 tasks done
huwcbjones opened this issue Oct 3, 2023 · 5 comments · Fixed by #1777 or #1791
Closed
2 tasks done

[BUG] auth/AuthProvider type hint incorrect #1776

huwcbjones opened this issue Oct 3, 2023 · 5 comments · Fixed by #1777 or #1791
Assignees
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior

Comments

@huwcbjones
Copy link
Contributor

Checklist

Describe the bug
I believe the type hint for AuthProvider is incorrect.
The definition of AuthProvider uses a TypeVar for the data argument to get and set.
I think the intended behaviour was to mandate that the types passed to get and set are the same for a given AuthProvider.

To Reproduce
Steps to reproduce the behavior:

  1. Implement an Auth Provider (see example below):
from typing import Any

import schemathesis
from schemathesis import Case
from schemathesis.auths import AuthProvider, AuthContext

@schemathesis.auth
class Provider(AuthProvider):
    def get(self, context: AuthContext) -> dict[str, Any]:
        return {"access_token": "foo"}
    def set(self, case: Case, data: dict[str, Any], context: AuthContext) -> None:
        reveal_type(data)
  1. Run mypy mwe.py
  2. Get errors about liskov subsitution policy
$ mypy mwe.py
mwe.py:11: error: Return type "Dict[str, Any]" of "get" incompatible with return type "Optional[Auth]" in supertype "AuthProvider"  [override]
mwe.py:14: error: Argument 2 of "set" is incompatible with supertype "AuthProvider"; supertype defines the argument type as "Auth"  [override]
mwe.py:14: note: This violates the Liskov substitution principle
mwe.py:14: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
mwe.py:15: note: Revealed type is "builtins.dict[builtins.str, Any]"
Found 2 errors in 1 file (checked 1 source file)

Expected behavior
No mypy errors

Environment (please complete the following information):

  • OS: Linux or macOS
  • Python version: 3.9.2
  • Schemathesis version: 3.17.9
  • Spec version: N/A

Additional context
If I add Generic[Auth] to the AuthProvider class definition, the errors go away.

--- a/src/schemathesis/auths.py
+++ b/src/schemathesis/auths.py
@@ -32,7 +32,7 @@


 @runtime_checkable
-class AuthProvider(Protocol):
+class AuthProvider(Protocol, Generic[Auth]):
     """Get authentication data for an API and set it on the generated test cases."""

     def get(self, context: AuthContext) -> Optional[Auth]:
@huwcbjones huwcbjones added Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior labels Oct 3, 2023
huwcbjones added a commit to huwcbjones/schemathesis that referenced this issue Oct 3, 2023
huwcbjones added a commit to huwcbjones/schemathesis that referenced this issue Oct 3, 2023
huwcbjones added a commit to huwcbjones/schemathesis that referenced this issue Oct 3, 2023
Stranger6667 pushed a commit to huwcbjones/schemathesis that referenced this issue Oct 3, 2023
@huwcbjones
Copy link
Contributor Author

Apologies, didn't quite fully fix this one.

New error is now

mwe.py:7:2: error: Argument 1 to "__call__" of "FilterableApplyAuth" has incompatible type "Type[Provider]"; expected "Callable[..., None]"  [arg-type]
mwe.py:12:21: note: Revealed type is "builtins.dict[builtins.str, Any]"
Found 1 error in 1 file (checked 1 source file)

huwcbjones added a commit to huwcbjones/schemathesis that referenced this issue Oct 5, 2023
@Stranger6667
Copy link
Member

Oh, what mypy version are you using?

@huwcbjones
Copy link
Contributor Author

Workaround is to use @schemathesis.auth.register() instead of @schemathesis.auth()

@huwcbjones
Copy link
Contributor Author

Oh, what mypy version are you using?

1.0.1 as it's the current version in Debian (stable) Bookworm

@huwcbjones
Copy link
Contributor Author

Can still reproduce the above mypy error on 1.5.1 (latest as of writing)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior
Projects
None yet
2 participants