FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

mypy type checker fails on 5.4.0 ProjectFileManager raw method · Issue #3104 · python-gitlab/python-gitlab · GitHub

Repository navigation

mypy type checker fails on 5.4.0 ProjectFileManager raw method #3104

Description

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

Activity

  1. nejch commented on Jan 28, 2025

    Member

    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.

  2. nejch commented on Jan 28, 2025

    Member

    @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.

  3. kesmik commented on Jan 28, 2025

    Author

    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.

  4. igorp-collabora commented on Jan 28, 2025

    Contributor

    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.

  5. nejch commented on Jan 28, 2025

    Member

    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.

  6. igorp-collabora commented on Jan 28, 2025

    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
    
  7. nejch commented on Jan 28, 2025

    Member

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

  8. locked as resolved and limited conversation to collaborators on Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL