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

Improve typing for WithXPermissions fixtures #5140

Open
matthewelwell opened this issue Feb 19, 2025 · 1 comment
Open

Improve typing for WithXPermissions fixtures #5140

matthewelwell opened this issue Feb 19, 2025 · 1 comment
Labels
api Issue related to the REST API

Comments

@matthewelwell
Copy link
Contributor

The types defined here are not valid according to mypy. Currently, all usages of the functions returned by these fixtures raise the following error in mypy's type checking:

error: Too few arguments  [call-arg]

I have already tried moving to using a Protocol definition like the following:

class WithEnvironmentPermissionsCallable(Protocol):
    def __call__(
        self,
        permission_keys: Optional[list[str]] = None,
        environment_id: Optional[int] = None,
        admin: bool = False,
    ) -> UserEnvironmentPermission: ...

However, this just results in a different error:

error: Incompatible return value type (got "Callable[[list[str] | None, int | None, bool], UserEnvironmentPermission]", expected "WithEnvironmentPermissionsCallable")  [return-value]
note: "WithEnvironmentPermissionsCallable.__call__" has type "Callable[[DefaultArg(list[str] | None, 'permission_keys'), DefaultArg(int | None, 'environment_id'), DefaultArg(bool | None, 'admin')], UserEnvironmentPermission]"
@matthewelwell
Copy link
Contributor Author

One option here would be to go with the protocol above as this will allow us to remove the type: ignores on each of the uses of the fixtures, and just add a single ignore on the return statement of the fixture (e.g. here). A similar option would be to use cast on the return to force mypy to believe it's correct.

Both of these feel like workarounds though so I'm keen to find a better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

No branches or pull requests

2 participants