Skip to content

mypy type checker fails on 5.4.0 ProjectFileManager raw method #3104

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

Closed
kesmik opened this issue Jan 28, 2025 · 7 comments · Fixed by #3106
Closed

mypy type checker fails on 5.4.0 ProjectFileManager raw method #3104

kesmik opened this issue Jan 28, 2025 · 7 comments · Fixed by #3106

Comments

@kesmik
Copy link

kesmik commented Jan 28, 2025

Description of the problem, including code/CLI snippet

After update to 5.4.0 I have noticed that overloaded methods break my file download raw method type checking for both: tempfile and regular file stream.

Is this something I am doing something wrong, or 36d9b24 commit break this and needs to be addressed?

Expected Behavior

Passing mypy

Actual Behavior

        with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
            self._tmp_file = f
            project = self._api.projects.get(file_info.prj_id, lazy=True)
            project.files.raw(
                file_path=file_info.path,
                ref=file_info.ref,
                streamed=True,
                action=f.write,
            )

error: No overload variant of "raw" of "ProjectFileManager" matches argument types "str", "str", "bool", overloaded function  [call-overload]
        with open("test.file", "wb") as f:
            self._tmp_file = f
            project = self._api.projects.get(file_info.prj_id, lazy=True)
            project.files.raw(
                file_path=file_info.path,
                ref=file_info.ref,
                streamed=True,
                action=f.write,
            )

error: No overload variant of "raw" of "ProjectFileManager" matches argument types "str", "str", "bool", "Callable[[Buffer], int]"  [call-overload]

Specifications

  • python-gitlab version: 5.4.0
  • Gitlab server version (or gitlab.com): v17.4.2
@nejch
Copy link
Member

nejch commented Jan 28, 2025

Thanks for the report @kesmik. Seems like we're not fully addressing all the variants of action that can be passed. I'll take a quick look at this and re-release after merge if necessary.

@nejch
Copy link
Member

nejch commented Jan 28, 2025

@JohnVillalovos @igorp-collabora this was my fear with typing overloads, I think we have to provide all the variations of preceding arguments (str or None) as well as the action which in this case is Callable[[Buffer], int] for f.write.

There's probably more elegant ways to overload hopefully at this point, as long as it's an option for us in the versions we support.

Edit: sorry ok it's easier than this, I think. WIll have a PR soon.

@kesmik
Copy link
Author

kesmik commented Jan 28, 2025

hi, @nejch thanks for update if you guys are busy, I can create a pull request, I am just not sure what is the proper way to fix it. I would just use in overload: [Callable[..., Any]], but not sure if it is ok.

@igorp-collabora
Copy link
Contributor

igorp-collabora commented Jan 28, 2025

action= can take any callable that accepts a single bytes argument. The return is not used at all and can be set to Any.

I need to test if Callable[[bytes], Any] would accept the f.write method or if a protocol would be needed.

@nejch
Copy link
Member

nejch commented Jan 28, 2025

action= can take any callable that accepts a single bytes argument. The return is not used at all and can be set to Any.

I need to test if Callable[[bytes], int] would accept the f.write method or if a protocol would be needed.

Thanks @igorp-collabora I was also just looking at this and looks like that would work, though maybe the new Buffer protocol from 3.12 would be better in the long run. I was wondering why we had that specific signature from #2211. I've added this in #3106 for now.

@igorp-collabora
Copy link
Contributor

@nejch @JohnVillalovos using Callable[[bytes], Any] fixes this issue.

I created this typing file:

from gitlab import Gitlab
import tempfile


def test(api: Gitlab, prj_id: int, suffix: str, path: str, ref: str) -> None:
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
        project = api.projects.get(prj_id, lazy=True)
        project.files.raw(
            file_path=path,
            ref=ref,
            streamed=True,
            action=f.write,
        )


def test2(api: Gitlab, prj_id: int, suffix: str, path: str, ref: str) -> None:
    with open("test.file", "wb") as f:
        project = api.projects.get(prj_id, lazy=True)
        project.files.raw(
            file_path=path,
            ref=ref,
            streamed=True,
            action=f.write,
        )

Before it would raise the same type checking errors.

After this patch there are no errors:

diff --git a/gitlab/v4/objects/files.py b/gitlab/v4/objects/files.py
index ce2193c2..e1f7b229 100644
--- a/gitlab/v4/objects/files.py
+++ b/gitlab/v4/objects/files.py
@@ -308,7 +308,7 @@ class ProjectFileManager(CreateMixin, UpdateMixin, DeleteMixin, RESTManager):
         file_path: str,
         ref: Optional[str] = None,
         streamed: Literal[True] = True,
-        action: Optional[Callable[[bytes], None]] = None,
+        action: Optional[Callable[[bytes], Any]] = None,
         chunk_size: int = 1024,
         *,
         iterator: Literal[False] = False,
Success: no issues found in 1 source file

@nejch
Copy link
Member

nejch commented Jan 28, 2025

I triggered a new release in case this is affecting version bumps out in the wild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants